Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl:variable contains nodeset. How to output nth node of variable?

I am transforming an XML document. There is an attribute @prettydate that is a string similar to "Friday, May 7, 2010". I want to split that string and add links to the month and the year. I am using the exslt:strings module and I can add any other necessary EXSLT module.

This is my code so far:

<xsl:template match="//calendar">
    <xsl:variable name="prettyparts">
        <xsl:value-of select="str:split(@prettydate,', ')"/>
    </xsl:variable>

    <table class='day'>
        <thead>
            <caption><xsl:value-of select="$prettyparts[1]"/>, 
                    <a>
                        <xsl:attribute name='href'><xsl:value-of select="$baseref"/>?date=<xsl:value-of select="@highlight"/>&amp;per=m</xsl:attribute>
                        <xsl:value-of select='$prettyparts[2]'/>
                    </a> 
                    <xsl:value-of select='$prettyparts[3]'/>,  
                    <a>
                        <xsl:attribute name='href'><xsl:value-of select="$baseref"/>?date=<xsl:value-of select="@highlight"/>&amp;per=y</xsl:attribute>
                        <xsl:value-of select='$prettyparts[4]'/>
                    </a> 
            </caption>
<!--etcetera-->

I have verified, by running $prettyparts through a <xml:for-each/> that I am getting the expected nodeset:

<token>Friday</token>
<token>May</token>
<token>7</token>
<token>2010</token>

But no matter which way I attempt to refer to a particular <token> directly (not in a foreach) I get nothing or various errors to do with invalid types. Here's some of the syntax I've tried:

<xsl:value-of select="$prettyparts[2]"/>
<xsl:value-of select="$prettyparts/token[2]"/>
<xsl:value-of select="exsl:node-set($prettyparts/token[2])"/>
<xsl:value-of select="exsl:node-set($prettyparts/token)[2]"/>

Any idea what the expression ought to be?

ETA: Thanks to @DevNull's suggestion, the correct expression is:

<xsl:value-of select="exsl:node-set($prettyparts)[position()=2]"/>

and, I have to set the variable this way:

<xsl:variable name="prettyparts" select="str:split(@prettydate,', ')" />
like image 796
dnagirl Avatar asked Nov 15 '22 10:11

dnagirl


1 Answers

Try using [position()=2] instead of [2] in your predicates.

Example:

<xsl:value-of select="$prettyparts[position()=2]"/>
like image 97
Daniel Haley Avatar answered Dec 05 '22 14:12

Daniel Haley