jbrehr
jbrehr

Reputation: 815

XSLT 2.0 tokenising delimiters within delimiters

In XSLT 2.0 I have long string (parameter) with a delimiter (;) inside a delimiter (~), more specifically a triplet inside a delimiter.

Data is organized like so:

<parameter>qrsbfs;qsvsv;tfgz~dknk;fvtea;gtvath~pksdi;ytbdi;oiunhu</parameter>

The first tokenize($mystring,'~') in a for-each produces :

qrsbfs;qsvsv;tfgz
dknk;fvtea;gtvath
pksdi;ytbdi;oiunhu

Within that tokenization, I need to treat it by looping again:

qrsbfs
qsvsv
tfgz

dknk
fvtea
gtvath

pksdi
ytbdi
oiunhu

I can do intensive string manipulation to get there using concat, string-length, and substring-before/substring-after, but I wondered if there wasn't a more elegant solution that my neophyte mind wasn't overlooking?

EDIT, adding nested tokenize that returned incorrect results:

        <xsl:for-each select="tokenize($myparameter,'~')">
            <xsl:for-each select="tokenize(.,';')">
                <xsl:if test="position()=1">
                    <xsl:value-of select="."/>
                </xsl:if>
                <xsl:if test="position()=2">
                    <xsl:value-of select="."/>
                </xsl:if>
                <xsl:if test="position()=3">
                    <xsl:value-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>

Upvotes: 0

Views: 424

Answers (3)

Jtakeda
Jtakeda

Reputation: 19

If you wanted a one line solution, you could do something like this, using nested for-in-return statements:

 <xsl:sequence select="for $n in tokenize(.,'~') return concat(string-join(tokenize($n,';'),'&#xa;'),'&#xa;&#xa;')"/>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167716

For what it's worth, the code in https://xsltfiddle.liberty-development.net/pPqsHUe uses

  <xsl:template match="parameter">
      <xsl:for-each select="tokenize(., '~')">
          <xsl:value-of select="tokenize(., ';')" separator="&#10;"/>
          <xsl:text>&#10;&#10;</xsl:text>
      </xsl:for-each>
  </xsl:template>

and with output method text produces

qrsbfs
qsvsv
tfgz

dknk
fvtea
gtvath

pksdi
ytbdi
oiunhu

Upvotes: 0

Wyatt Shipman
Wyatt Shipman

Reputation: 1789

If you don't need to tokenize them separately, you could replace the ~ with ; and tokenize all 9 elements at the same time:

tokenize(replace(parameter,'~',';'),';')

Upvotes: 0

Related Questions