2011-01-11 92 views
2

我有下面的XMLXSLT類似的功能,SQL「WHERE LIKE」

 <Answer> 
      <Label>FCVCMileage</Label> 
      <Value>3258</Value> 
      <Iteration>0</Iteration> 
      <DataType>NUMBER</DataType> 
     </Answer> 

,並需要獲得里程標籤的下方,價值。然而,Mileage的4字母前綴可以是8個不同前綴中的任何一個。

我知道我可以通過測試每個組合使用xsl:if或xsl:choose來找到值,但是有沒有更好的方法可以像下面的SQL一樣工作,也不需要更改代碼如果添加了其他前綴。

WHERE label LIKE '%Mileage' 

NB。只會有1個標籤元素包含單詞Mileage

乾杯!

+0

XSLT聽起來不像正確的XML技術。 XPATH更適合你想要做的事情。 – 2011-01-11 09:49:06

回答

3
I know I could find the value by testing for every combination using xsl:if or xsl:choose but is there a more elegant way that would work similar to the following SQL and also wouldn't need changes to code being made if other prefixes were added. 

    WHERE label LIKE '%Mileage' 

在XPath 2.0有一個標準的函數ends-with()

在XPath 1.0使用

/*[Label[substring(., string-length(.) - 6) = 'Mileage']]/value 

contains(Label, 'Mileage') 

不等於上述以上的SQL子句。例如,它將選擇此節點:

<Label>xxx Mileage yyy</Label> 
3
Answer[contains(Label, 'Mileage')]/Value 
+0

我沒有意識到我可以在這種情況下使用包含。乾杯! – Fishcake 2011-01-11 10:52:12