XSLT 2.0: Tokenize does not work on period character (full stop / dot)

StackOverflow https://stackoverflow.com/questions/22251636

  •  11-06-2023
  •  | 
  •  

سؤال

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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top