开发者

XSLT Similar functionality to SQL "WHERE LIKE"

I have the following XML

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

And need to get the Value underneath the Mileage label. However the 4 letter prefix to Mileage could be any 1 of 8 different prefixes.

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 o开发者_StackOverflow社区ther prefixes were added.

WHERE label LIKE '%Mileage'

NB. There will only ever be 1 label element containing the word Mileage

Cheers!


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'

In XPath 2.0 there is a standard function ends-with()

In XPath 1.0 use:

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

Note:

  contains(Label, 'Mileage')

is not equivalent to the SQL clause above. For example, it will select this node:

<Label>xxx Mileage yyy</Label>


Answer[contains(Label, 'Mileage')]/Value
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜