Forum Moderators: open

Message Too Old, No Replies

Help Sorting w/ XSL

         

tjhorne

7:21 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



I need help sorting my XML file with XSL (file is below), I can't seem to get it to work properly. I am trying to sort on the User tag and the "Name" attribute. I am trying to sort an XML file that is like:

<Alert>
<Item>
<From>
<User></User>
</From>
<To>
<User Name="c: jsmith"></User>
</To>
</Item>
<Item>
<From>
<User></User>
</From>
<To>
<User Name="c: tstone"></User>
</To>
</Item>

</Alerts>

Here is my XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Alert/Item/To">
<xsl:for-each select="User">
<xsl:sort select="@Name" />
<xsl:for-each select="@Name">
<xsl:value-of select="." />
</xsl:for-each>&#160;
<a>
<xsl:attribute name="href">mailto:
<xsl:for-each select="@Email">
<xsl:value-of select="." />
</xsl:for-each>
</xsl:attribute>
<xsl:value-of select="@Email"/>
</a>
&#160;
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

johnl

11:37 pm on Jan 6, 2006 (gmt 0)

10+ Year Member



hi,
first, your xml-example starts with <alert> and ends with </alerts>, which does not fit.
second, it is not clear, what kind of output you want. if it is html, you could add some html and write:


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html><head><title>WebmasterWorld26_279</title></head><body>
<xsl:for-each select="Alert/Item/To/User">
<xsl:sort select="@Name" />
User:
<xsl:value-of select="@Name" />
<br />
</xsl:for-each>
</body></html>
</xsl:template>
</xsl:stylesheet>

third, you might consider using xsl:apply-templates instead of xsl:for-each.

good luck!