Ashish Jaiswal
Ashish Jaiswal

Reputation: 1

Split url string in XSLT

How do we split some string based on some character/separator in given url.

Given url like: www.example.com/content/index.html

Desired output like: Index

Code:

<span transId="gadget_{position()}"><xsl:value-of select="www.example.com/content/index.html" /> </span>

Upvotes: 0

Views: 352

Answers (1)

Amrendra Kumar
Amrendra Kumar

Reputation: 1816

You can use tokenize and substring-before in build function to achive your desired ouput:

EXAMPLE INPUT:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <exp>www.example.com/content/index.html</exp>
</body>

EXAMPLE XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />


    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="exp">
        <span transId="gadget_{position()}">
            <xsl:value-of select="substring-before(tokenize(., '/')[last()],'.')" />
        </span>
    </xsl:template>

</xsl:transform>

EXAMPLE OUTPUT:

<?xml version="1.0" encoding="UTF-8"?>
<body>
    <span transId="gadget_2">index</span>
</body>

Upvotes: 1

Related Questions