XSLT's tokenize function is not working as I expected when tokenizing on a period character '.'
--
Here is an example of what happens when I tokenize over a comma character:
XML file:
<value>a,b,c</value>
XSLT file:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<firsttoken><xsl:value-of select="tokenize(/value,',')[1]" /></firsttoken>
</xsl:template>
</xsl:stylesheet>
Output:
<firsttoken>a</firsttoken>
--
Here is the same example but tokenizing over a period character:
XML file:
<value>a.b.c</value>
XSLT file:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<firsttoken><xsl:value-of select="tokenize(/value,'.')[1]" /></firsttoken>
</xsl:template>
</xsl:stylesheet>
Output:
<firsttoken/>
--
I can't work out why it works for commas but not for period.
.
is a special character both in XSLT (shorthand for the context node) and in a regular expression (any alphanumerical character).
If you'd like to match a literal "." you need to escape it with a backslash in front.
I can't work out why it works for commas but not for period.
The tokenization does work for periods. It is just that .
is not interpreted as a period in the first place.
Stylesheet
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<firsttoken><xsl:value-of select="tokenize(/value,'\.')[1]" /></firsttoken>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="UTF-8"?><firsttoken>a</firsttoken>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With