Search code examples
xslt-2.0

xsl string tokenize duplicate value


I have String as O, T, A, F, M, I, S, R, A (Hedged), V, PFT when try to tokenize with comma separted I'm getting result as O T A F M I S R A Hedged V PFT with A getting duplicated and which is incorrect it should have A (Hedged) as one token.

I tried below xsl: XML node will have value as O, T, A, F, M, I, S, R, A (Hedged), V, PFT

XML:

<?xml version="1.0" encoding="UTF-8"?>
<path>
<some>O, T, A, F, M, I, S, R, A (Hedged), V, PFT</some>
</path>

 <xsl:variable name="val" select="//path/some" />
<xsl:for-each select="str:tokenize($val, ', ')">
  <xsl:variable name="tokVal" select="."/>
<h2><xsl:value-of select="$tokVal"/></h2>
</xsl:for-each>

Expected output is O T A F M I S R A (Hedged) V PFT


Solution

  • Using XSLT 2.0 and your finally posted XML input I can't reproduce the problem at http://xsltransform.net/bEzjRKJ, input is

    <path>
    <some>O, T, A, F, M, I, S, R, A (Hedged), V, PFT</some>
    </path>
    

    minimal XSLT is

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    
        <xsl:template match="/">
          <html>
            <head>
              <title>Test</title>
            </head>
            <xsl:apply-templates/>
          </html>
        </xsl:template>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="some">
            <xsl:for-each select="tokenize(., '\s*,\s*')">
                <h2>
                    <xsl:value-of select="."/>
                </h2>
            </xsl:for-each>
        </xsl:template>
    </xsl:transform>
    

    output is

    <!DOCTYPE html
      PUBLIC "XSLT-compat">
    <html>
       <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <title>Test</title>
       </head>
       <path>
    
          <h2>O</h2>
          <h2>T</h2>
          <h2>A</h2>
          <h2>F</h2>
          <h2>M</h2>
          <h2>I</h2>
          <h2>S</h2>
          <h2>R</h2>
          <h2>A (Hedged)</h2>
          <h2>V</h2>
          <h2>PFT</h2>
    
       </path>
    </html>
    

    rendering as

    O

    T

    A

    F

    M

    I

    S

    R

    A (Hedged)

    V

    PFT