views:

279

answers:

1

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,', ')" />
+1  A: 

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

Example:

<xsl:value-of select="$prettyparts[position()=2]"/>
DevNull
`position()=x` was the key but wasn't sufficient. The final working select statement was `select="exsl:node-set($prettyparts)[position()=2]"`
dnagirl
Glad you got it working.
DevNull