【问题标题】:Using xpath fetch multiple parent nodes使用 xpath 获取多个父节点
【发布时间】:2013-11-05 18:07:09
【问题描述】:

我有以下示例 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 黑色 苹果 iPhone 4 8GB 白色

你能帮我构建 xpath 查询吗?

【问题讨论】:

    标签: xpath


    【解决方案1】:

    你可以使用这个查询:

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

    或者根据您对结果的处理方式,您可能希望将“/text()”位留在末尾。

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

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

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多