Php 在多个XML文件中搜索匹配项

Php 在多个XML文件中搜索匹配项,php,javascript,html,xml,xpath,Php,Javascript,Html,Xml,Xpath,这是我的general.xml文件: 这是我的product.xml文件: <childrens> <child_4893 entity_id="4893" value="Gujarat" parent_id="4823"> <child_4894 entity_id="4894" value="Ahmedabad" parent_id="4893"/> <child_4895 entity_id="4895" v

这是我的
general.xml
文件:

这是我的
product.xml
文件:

<childrens>
    <child_4893 entity_id="4893" value="Gujarat" parent_id="4823">
        <child_4894 entity_id="4894" value="Ahmedabad" parent_id="4893"/>
        <child_4895 entity_id="4895" value="Anand" parent_id="4893"/>
        <child_4896 entity_id="4896" value="Bharuch (Broach)" parent_id="4893"/>
        <child_4897 entity_id="4897" value="Bhavnagar" parent_id="4893"/>
        <child_4898 entity_id="4898" value="Bhuj" parent_id="4893"/>
        <child_4899 entity_id="4899" value="Gandhidham" parent_id="4893"/>
        <child_4900 entity_id="4900" value="Gandhinagar" parent_id="4893"/>
        <child_4901 entity_id="4901" value="Godhra" parent_id="4893"/>
        <child_4902 entity_id="4902" value="Jamnagar" parent_id="4893"/>
        <child_4903 entity_id="4903" value="Junagadh" parent_id="4893"/>
        <child_4904 entity_id="4904" value="Morvi" parent_id="4893"/>
        <child_4905 entity_id="4905" value="Nadiad" parent_id="4893"/>
        <child_4906 entity_id="4906" value="Navsari" parent_id="4893"/>
        <child_4907 entity_id="4907" value="Patan" parent_id="4893"/>
        <child_4908 entity_id="4908" value="Porbandar" parent_id="4893"/>
        <child_4909 entity_id="4909" value="Rajkot" parent_id="4893"/>
        <child_4910 entity_id="4910" value="Surat" parent_id="4893"/>
        <child_4911 entity_id="4911" value="Surendranagar" parent_id="4893"/>
        <child_4912 entity_id="4912" value="Vadodara (Baroda)" parent_id="4893"/>
        <child_4913 entity_id="4913" value="Vejalpur" parent_id="4893"/>
        <child_4914 entity_id="4914" value="Veraval" parent_id="4893"/>
    </child_4893>
</childrens>
<products>
    <product_id value="1">
        <tab_id>
            <tab_name value="test1" />
            <dist_region value="4909"/>
            <dist_region value="4909"/>
            <dist_region value="4909"/>
        </tab_id>
    </product_id>
</products>

解释
  • general.xml
    文件中有一个名为
    的节点元素,它是一个
    商店城市
    名称
  • 检查
    region.xml
    文件中是否存在此城市名称,复制相应的
    实体id
  • 检查此
    实体\u id
    是否存在于
    product.xml
    文件中,如果存在,则返回相应的
    产品\u id
  • 例如:
    • general.xml
    • Rajkot
      存在于
      区域中。xml
      和相应的
      实体id
      4909
    • 实体id
      4909
      存在于
      product.xml
      中,对应的
      product\u id
      属性值为
      1
    • 返回
      product\u id
      value
      1

    使用PHP simplexml执行以下操作:-->请参阅实时演示:

    /$g包含general.xml、$r region.xml、$p product.xml
    $general=simplexml\u load\u字符串($g);
    $region=simplexml\u load\u字符串($r);
    $product=simplexml\u load\u字符串($p);
    $name=(字符串)$general->result->name;
    $name='Rajkot'//xpath(“//*[@value='$name']/@entity_id”);
    $entity=(字符串)$entity;
    如果(strlen(trim($entity))==0)退出('entity_id not found');
    list($prid)=$product->xpath(//dist_region[@value='$entity']/祖先::product_id/@value”);
    $prid=(字符串)$prid;
    echo“name:$name,entity\u id:$entity,product\u id:$prid”;
    
    解释:在
    general.xml
    中,
    =
    Rājkot
    ,在
    区域.xml
    Rajkot
    ,没有特殊的
    a
    -字符。为了使代码正常工作,我必须将
    $name
    设置为
    Rajkot


    您需要找到解决方案。

    我不太明白您想要什么,请重新表述您的问题。“出口”是什么意思?你在找什么?Javascript Xpath的帮助?您应该自己编写实际的脚本逻辑……您的Javascript如何获取XML文档(内容)?通过Ajax?内联的?不知道?
    <products>
        <product_id value="1">
            <tab_id>
                <tab_name value="test1" />
                <dist_region value="4909"/>
                <dist_region value="4909"/>
                <dist_region value="4909"/>
            </tab_id>
        </product_id>
    </products>
    
    // $g holds general.xml, $r region.xml, $p product.xml 
    
    $general = simplexml_load_string($g);
    $region = simplexml_load_string($r);
    $product = simplexml_load_string($p);
    
    $name = (string)$general->result->name;
    $name = 'Rajkot' // <--- see explanation below!
    
    if (strlen(trim($name))==0) exit('name not found');
    
    list($entity) = $region->xpath("//*[@value='$name']/@entity_id");
    $entity=(string)$entity;
    
    if (strlen(trim($entity))==0) exit('entity_id not found');
    
    list($prid) = $product->xpath("//dist_region[@value='$entity']/ancestor::product_id/@value");
    $prid=(string)$prid;
    
    echo "name: $name, entity_id: $entity, product_id: $prid";