Monday 7 May 2012

How to Match special characters in XSLT?


QUESTION

User wants to retrieve the value in between the [ ] i.e. 1(in this case)

INPUT.XML

<CarSet SOAP-ENC:arrayType="Car-Set[1]&quot;>

ANSWER:

For RegExp you must use XSLT 2.0 with the following code:

<xsl:template match="CarSet">
<xsl:analyze-string select="@arrayType" regex="([\w]*-[\w]*)(\[)([\d]*)(\])">
<xsl:matching-substring>
<xsl:value-of select="regex-group(3)"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>


With ([\w]*-[\w]*)(\[)([\d]*)(\]) code I have divided the coming input into four parts:

1. ([\w]*-[\w]*) = Car-Set
2. (\[) = [
3. ([\d]*) = 1 (number)
4. (\]) = ]

And than print the third group only, which is '1' as you expected.

No comments: