Xsl: variable contains nodeet. How to output nth node variable?

I am converting an XML document. There is the @prettydate attribute, which is a string like "Friday May 7, 2010." I want to break this line and add links for month and year. I use the exslt:strings module, and I can add any other required EXSLT module.

This is my code:

 <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 verified by running $ prettyparts through <xml:for-each/> that I get the expected set of nodes:

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

But no matter how I try to refer directly to a specific <token> (not in foreach), I get nothing or various errors related to invalid types. Here are 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 should 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 as follows:

 <xsl:variable name="prettyparts" select="str:split(@prettydate,', ')" /> 
+4
source share
1 answer

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

Example:

 <xsl:value-of select="$prettyparts[position()=2]"/> 
+1
source

Source: https://habr.com/ru/post/1309142/


All Articles