Ogbechi
Ogbechi

Reputation: 53

How to get the last character of string in XSLT

I have the following input

<items>
    <item id="1" name="aaa" old_level="5,4,3,2,1" new_level="5,4,3,2,1,0"/>
    <item id="2" name="bbb" old_level="3,2,1" new_level="3,2,1"/>
    <item id="3" name="ccc" old_level="4,3,2" new_level="4,3,2"/>
</items>

i need to check whether the last char of new_level is zero or not. How can i do this with xslt?

Upvotes: 2

Views: 12047

Answers (2)

smj
smj

Reputation: 1284

If you have XSLT/XPath 2.0, you can use an XPath like this (or adapt for your specific need):

//item[ends-with(@new_level,'0')]

If you only have XSLT/XPath 1.0, you could do something like:

<xsl:template match="item">
    <xsl:variable name="length" select="string-length(@new_level)"/>
    <xsl:if test="substring(@new_level,$length) = '0'">
        <!-- do something -->
    </xsl:if>
</xsl:template>

Upvotes: 6

Martin Honnen
Martin Honnen

Reputation: 167581

With XSLT 2.0 it is easy: <xsl:template match="item[tokenize(@new_level, ',')[last()] eq '0']">..</xsl:template>.

Upvotes: 2

Related Questions