Xsl: sorting using templates without sorting

I have a fairly large XSL document for a job that does a number of things. It is almost complete, but I missed the requirement that it be sorted, and I cannot get it to work. There is an SSCCE of what is happening.

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Root Document --> <xsl:template match="/"> <html> <body> <xsl:apply-templates select="staff"> <xsl:sort select="member/last_name" /> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="member"> <xsl:value-of select="first_name" />&#160;<xsl:value-of select="last_name" /> <br/> </xsl:template> </xsl:stylesheet> 

The XML file is as follows:

 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="sort.xsl"?> <staff> <member> <first_name>Joe</first_name> <last_name>Blogs</last_name> </member> <member> <first_name>John</first_name> <last_name>Smith</last_name> </member> <member> <first_name>Steven</first_name> <last_name>Adams</last_name> </member> </staff> 

I expected employees to be listed by last name, but they are not sorted. Please keep in mind that I am very inexperienced in XSLT.

+7
source share
2 answers
  <xsl:apply-templates select="staff"> <xsl:sort select="member/last_name" /> </xsl:apply-templates> 

selects staff members and sorts them, but there is only one staff member, so this does not work.

Change to

  <xsl:apply-templates select="staff/member"> <xsl:sort select="last_name" /> </xsl:apply-templates> 

which selects all member elements and sorts them.

+20
source

There is no template for staff compliance or a change in the corresponding template for a member, as in this

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Root Document --> <xsl:template match="/"> <html> <body> <xsl:apply-templates select="staff/member"> <xsl:sort select="last_name" /> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="member"> <xsl:value-of select="first_name" />&#160;<xsl:value-of select="last_name" /> <br/> </xsl:template> </xsl:stylesheet> 
+3
source

All Articles