Eliseo D'Annunzio
Eliseo D'Annunzio

Reputation: 592

Tokenizing a string on literal slash

I have the following code for a XSL id attribute setting for an anchor link:

        <xsl:attribute name="id">
            <xsl:variable name="articleUrl" select="concat(substring-before(substring-after(substring-after(document/documentinfo/uri/@path,$ps-group-path), 'content'),'psml'),'html')" as="xs:string*"/>
            <xsl:variable name="articleArr" select="tokenize($articleUrl,'//')" />
            <xsl:variable name="articleIndex" select="count($articleArr)" as="xs:integer" />
            <xsl:value-of select="$articleArr[$articleIndex]" as="xs:string" />
        </xsl:attribute>

The value of the select in the first xsl:variable in this case is /news/2018/AT04651-article.html. I want to split articleUrl by the literal slash /, and then do some other mumbo jumbo with the array to extract the last part of the array (AT04651-article.html), and then eventually lop off the -article.html part to access the value AT04651...

Only problem right now is that when I tried tokenizing the string by a slash, the value returned by the id value, I ended up getting the original string, /news/2018/AT04651-article.html not currently AT04651-article.html, and the value of $articleIndex comes back as 1... it's as though the tokenize function doesn't appear to work... Can anyone tell me where I've gone wrong?

I'm working with XSLT 2.0 in this instance...

Upvotes: 0

Views: 436

Answers (1)

Johnson
Johnson

Reputation: 461

If you are wanting to split the string on a single literal slash, why are you tokenizing on a double slash? I think if you change that you will solve your problem.

I tested this hard-coding the input variable you supplied, then just outputting each item after tokenizing along with its index.

Code

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml"/>

    <xsl:template match="/">
        <items>
            <xsl:variable name="articleUrl" select="'/news/2018/AT04651-article.html'" />
            <xsl:variable name="articleArr" select="tokenize($articleUrl,'/')" />
            <xsl:variable name="articleIndex" select="count($articleArr)"  />
            <xsl:for-each select="$articleArr">
                <item index="{position()}"><xsl:value-of select="." /></item>
            </xsl:for-each>
        </items>
    </xsl:template>

</xsl:stylesheet>

Output

<?xml version="1.0" encoding="UTF-8"?>
<items>
   <item index="1"/>
   <item index="2">news</item>
   <item index="3">2018</item>
   <item index="4">AT04651-article.html</item>
</items>

Upvotes: 1

Related Questions