2011-01-11 50 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