Get item out of array

mrduckers
mrduckers used Ask the Experts™
on
Hi,

I have a quick question regarding xslt.

This code returns my path as page node ids
<xsl:value-of select="$currentPage/@path"/>
in the format
-1,1240,1530,1974,2291,2716

How can return 1 specific value? eg the 3rd one form the left?

Thanks.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can use an index with square brackets for a single value.

<xsl:value-of select="$currentPage/@path[4]"/>
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
no, that will not work
[4] will give you the fourth item from the nodeset @page, and since that is only one attribute path $currentPage can have,
it will return nothing.
So the above answer has no chance of doing what you want

In XSLT2, it is easy, you can use tokenize() to break up the 'array' into a sequence

with this example
<currentPage path="-1,1240,1530,1974,2291,2716"/>

try the below XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="2.0">
    <xsl:template match="currentPage">
        <xsl:value-of select="tokenize(@path,',')[3]"></xsl:value-of>
    </xsl:template>
</xsl:stylesheet>

Open in new window

Information Architect
Top Expert 2006
Commented:
If you can't do this using XSLT2, you will need a recursive function,
see below example

don't hesitate to ask for an explanation, if you need one
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
    <xsl:template match="currentPage">
        <xsl:call-template name="get-item-from-array">
            <xsl:with-param name="str" select="@path"/>
            <xsl:with-param name="item-no" select="3"/>
            <xsl:with-param name="seperator" select="','"/>
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="get-item-from-array">
        <xsl:param name="str"/>
        <xsl:param name="item-no"/>
        <xsl:param name="seperator"/>
        <xsl:choose>
            <xsl:when test="$item-no &lt;= 1">
                <xsl:value-of select="substring-before($str, $seperator)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="get-item-from-array">
                    <xsl:with-param name="str" select="substring-after($str, $seperator)"/>
                    <xsl:with-param name="item-no" select="$item-no - 1"/>
                    <xsl:with-param name="seperator" select="$seperator"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
</xsl:stylesheet>

Open in new window

Author

Commented:
Hi
The first example gives me an error and won't let me save.
the recursive one returns no result - am I missing something?
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
the first example giving you an error means that you are not using an XSLT2 processor

both are tested an work on the input XML I added to my comment
you might have an error in the XPath for the with-param

please add full XSLT and real XML source, so I can check the XSLT

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial