2011-02-03 23 views
1

我有以下XML文件:如何使用XPATH獲取此列表中的所有非排除項目?

<items> 
    <item> 
     <name>Suede Shoe</name> 
     <category>Footwear</category> 
     <price>£90.00</price> 
     <tags> 
      <footwear /> 
      <shoes /> 
     </tags> 
    </item> 
    <item> 
     <name>Leather Shoe</name> 
     <category>footwear</category> 
     <price>£90.00</price> 
     <tags> 
      <footwear /> 
     </tags> 
    </item> 
    <item> 
     <name>Canvas Shoe</name> 
     <category>Footwear</category> 
     <price>£90.00</price> 
     <tags> 
      <footwear exclude="true"/> 
      <shoes/> 
     </tags> 
    </item> 
    <item> 
     <name>Toecap Shoe</name> 
     <category>Footwear</category> 
     <price>£90.00</price> 
     <tags> 
      <footwear exclude="true"/> 
     </tags> 
    </item> 
</items> 

我想選擇所有包含我的選擇標籤中的一個項目,但如果標籤被排除在外不帶回來的結果。

例子:

標籤:鞋類
返回:1,2

標籤:鞋
返回1,3

標籤:鞋類|鞋
返回1,2,3

請幫忙嗎?

包括「排除」,要求此XPath是工作之前:

//tags[implode('|', $tags)]/parent::item 

標籤「鞋類」,即不排除。

+0

目前還不清楚你到底要XPath表達式返回 - 「1,2」是什麼意思? – 2011-02-03 14:56:01

回答

1

爲製鞋|鞋子的XPath可能看起來像

 
//tags[footwear[not(@exclude='true')]|shoes[not(@exclude='true')]]/parent::item 

這將顯示1,2,3

+0

Ahhhh我沒有意識到你不能(x)。非常感謝! – alex 2011-02-03 15:15:47

0

此XPath 1.0表達式:

/items/item[ 
    tags/*[ 
     not(@exclude='true') 
    ][ 
     contains(
     concat('|',$tags,'|'), 
     concat('|',name(),'|') 
    ) 
    ] 
] 

隨着$標籤變量集as 'footwear'字符串選擇:

<item> 
    <name>Suede Shoe</name> 
    <category>Footwear</category> 
    <price>£90.00</price> 
    <tags> 
     <footwear /> 
     <shoes /> 
    </tags> 
</item> 
<item> 
    <name>Leather Shoe</name> 
    <category>footwear</category> 
    <price>£90.00</price> 
    <tags> 
     <footwear /> 
    </tags> 
</item> 

隨着$標籤變量設置爲'shoes'串選擇:

<item> 
    <name>Suede Shoe</name> 
    <category>Footwear</category> 
    <price>£90.00</price> 
    <tags> 
     <footwear /> 
     <shoes /> 
    </tags> 
</item> 
<item> 
    <name>Canvas Shoe</name> 
    <category>Footwear</category> 
    <price>£90.00</price> 
    <tags> 
     <footwear exclude="true" /> 
     <shoes /> 
    </tags> 
</item> 

隨着$標籤變量設置爲'footwear|shoes'串選擇:

<item> 
    <name>Suede Shoe</name> 
    <category>Footwear</category> 
    <price>£90.00</price> 
    <tags> 
     <footwear /> 
     <shoes /> 
    </tags> 
</item> 
<item> 
    <name>Leather Shoe</name> 
    <category>footwear</category> 
    <price>£90.00</price> 
    <tags> 
     <footwear /> 
    </tags> 
</item> 
<item> 
    <name>Canvas Shoe</name> 
    <category>Footwear</category> 
    <price>£90.00</price> 
    <tags> 
     <footwear exclude="true" /> 
     <shoes /> 
    </tags> 
</item> 
相關問題