user2342875
user2342875

Reputation: 239

Extract parts of string with XPath

I have the following tag in xml:

<GameESRB> Mature10+ </GameESRB>

I want to get all the characters that are not lowercase using XPath 1.0 and XSLT 1.0. Thus the result string for the above would be:

 M10+

How would I go about into doing this?

Upvotes: 2

Views: 213

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

Use (with GameESRB as the initial context node):

translate(., 'abcdefghijklmnopqrstuvwxyz', '')

XSLT - based verification:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
     <xsl:copy-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<GameESRB> Mature10+ </GameESRB>

the XPath expression is evaluated, and the result of this evaluation is copied to the output:

 M10+ 

Upvotes: 2

Related Questions