Noob question

P

Pravljica

Hello,

Now I have this:
<xsl:for-each select="A">
<xsl:sort select="B" />
</xsl:for-each>

Now I would like to get first node from this sorted list (which has the
smallest value). But if I write:
<xsl:value-of select="A[1]/B" />, I get first node from unordered list
(the first node in xml).
How can I get the first node from this sort?

Thanks
 
M

Martin Honnen

Pravljica said:
Now I have this:
<xsl:for-each select="A">
<xsl:sort select="B" />
</xsl:for-each>

Now I would like to get first node from this sorted list (which has the
smallest value). But if I write:
<xsl:value-of select="A[1]/B" />, I get first node from unordered list
(the first node in xml).
How can I get the first node from this sort?

With XSLT 1.0 you would need to sort into a variable and then make use
of an extension function to convert the variable value, a result tree
fragment, into a node set.
Or you need to chain two stylesheets where the first stylesheet sorts
the input XML and the second works with the sorted result of the first.
 
P

Pravljica

Hey,

I didn't understood a bit of your answer :)
How do you sort into variable? Also - what is an extension function?
Can you give me a link or something?

I need this to get smallest date from my xml, I thought to do it this
way. I sort on date, and the first node is smallest date, the last node
is biggest date. Maybe I can do this easier?

Thanks
 
M

Martin Honnen

Pravljica wrote:

I didn't understood a bit of your answer :)
How do you sort into variable? Also - what is an extension function?
Can you give me a link or something?

Here is a thread that has both an XSLT 1.0 example as well as an XSLT
2.0 example.
I need this to get smallest date from my xml, I thought to do it this
way. I sort on date, and the first node is smallest date, the last node
is biggest date. Maybe I can do this easier?

Depends on the date format. If you have e.g.

<date-list>
<date>20060101</date>
<date>20050101</date>
<date>20060527</date>
<date>20041231</date>
</date-list>

then the XPath expression

/date-list/date[not(. > ../date)]

gives you the minimum date (e.g. 20041231).


For your earlier example you might also want to try

<xsl:for-each select="A">
<xsl:sort select="B" />
<xsl:if test="position() = 1">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,258
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top