Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xpath获取多个父节点_Xpath - Fatal编程技术网

使用xpath获取多个父节点

使用xpath获取多个父节点,xpath,Xpath,我有以下示例xml。根据制造商id,我希望得到与之对应的所有产品名称 <root> <Component ID="88" BusinessID="ABC"> <Product_Name>Apple iPhone 4 16GB Black</Product_Name> <Product_Description>Apple iPhone 4 8GB Black</Product_Description>

我有以下示例xml。根据制造商id,我希望得到与之对应的所有产品名称

<root>
  <Component ID="88" BusinessID="ABC">  
    <Product_Name>Apple iPhone 4 16GB Black</Product_Name>  
    <Product_Description>Apple iPhone 4 8GB Black</Product_Description>  
    <Manufacturer ID="1122" xsi:type="MobileHandset_Manufacturer"> 
      <Name>Apple</Name>  
      <Description>Apple</Description> 
    </Manufacturer>       
  </Component>
  <Component ID="98" BusinessID="LMN">  
    <Product_Name>Apple iPhone 4 16GB White</Product_Name>  
    <Product_Description>Apple iPhone 4 8GB White</Product_Description>  
    <Manufacturer ID="1122" xsi:type="MobileHandset_Manufacturer"> 
      <Name>Apple</Name>  
      <Description>Apple</Description> 
    </Manufacturer>       
  </Component>
  <Component ID="77" BusinessID="XYZ">  
    <Product_Name>Samsung Galaxy 16GB Green</Product_Name>  
    <Product_Description>Samsung Galaxy 16GB Green</Product_Description>  
    <Manufacturer ID="1177" xsi:type="MobileHandset_Manufacturer"> 
      <Name>Samsung</Name>  
      <Description>Samsung</Description> 
    </Manufacturer>       
  </Component>
</root>
因此,使用ID 1122,我希望得到以下产品名称作为回报: 苹果iPhone 4 16GB黑色 苹果iPhone4 8GB白色


您可以帮助我构建xpath查询吗?

您可以使用以下查询:

//Component[Manufacturer[@ID='1122']]/Product_Name/text()
或者,根据您对结果所做的操作,您可能希望将/text位保留在末尾

如果只需要获取唯一值,并且可以使用XPath 2.0,则可以将上面的地址包装到distinct values函数中:

distinct-values(//Component[Manufacturer[@ID='1122']]/Product_Name/text())