Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort XML nodes in alphabetical order using XSL

I am trying to figure out how to sort the XML list of employees alphabetically by lastname using XSL. Right now it just displays the XML information in the same order as it is in the XML. I don't think I fully understand how to use the <xsl:sort> function as I am new to XSL. I also tried putting order-by="+ Lastname" in with <xsl:for-each> and I couldn't get that to work either.

Heres my xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Company1.xsl"?>
<Company>
    <Employee>
        <Firstname>John</Firstname>
        <Lastname>Smith</Lastname>
        <ssn>635-35-7463</ssn>
        <doh>February 3, 2011</doh>
        <Age>34</Age>
    </Employee>
    <Employee>
        <Firstname>Brad</Firstname>
        <Lastname>Roberts</Lastname>
        <ssn>789-65-4568</ssn>
        <doh>February 13, 2012</doh>
        <Age>25</Age>
    </Employee>
    <Employee>
        <Firstname>Karen</Firstname>
        <Lastname>Smith</Lastname>
        <ssn>369-12-7415</ssn>
        <doh>March 24, 2011</doh>
        <Age>28</Age>
    </Employee>
    <Employee>
        <Firstname>Eli</Firstname>
        <Lastname>Smith</Lastname>
        <ssn>489-32-8525</ssn>
        <doh>September 14, 2010</doh>
        <Age>38</Age>
    </Employee>
    <Employee>
        <Firstname>Bill</Firstname>
        <Lastname>Joel</Lastname>
        <ssn>689-67-7634</ssn>
        <doh>February 29, 2012</doh>
        <Age>24</Age>
    </Employee>
    <Employee>
        <Firstname>Kelly</Firstname>
        <Lastname>Greene</Lastname>
        <ssn>927-82-6873</ssn>
        <doh>December 3, 2010</doh>
        <Age>34</Age>
    </Employee>
</Company>

And heres my XSL:

<?xml version="1.0" ?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <HTML>
    <HEAD>
    <TITLE>Company Employees</TITLE> 
    </HEAD>
    <BODY>
    <H2>Company Employees</H2> 

    <xsl:for-each select="Company/Employee">
        <xsl:sort select="Employee/Lastname" data-type="text" order="ascending"/>
        <xsl:sort select="Employee/Firstname" data-type="text" order="ascending"/>

        <SPAN STYLE="font-weight:bold">FirstName: </SPAN>
            <xsl:value-of select="Lastname" />
        <BR />
        <SPAN STYLE="font-weight:bold">LastName: </SPAN>
            <xsl:value-of select="Firstname" />
        <BR /> 
        <SPAN STYLE="font-weight:bold">SSN: </SPAN> 
        <xsl:value-of select="ssn" /> 
        <BR /> 
        <SPAN STYLE="font-weight:bold">Date of Hire: </SPAN> 
        <xsl:value-of select="doh" /> 
        <BR /> 
        <SPAN STYLE="font-weight:bold">Age: </SPAN> 
        <xsl:value-of select="Age" /> 
        <P/>
    </xsl:for-each>
    </BODY>
    </HTML>
    </xsl:template>
</xsl:stylesheet>
like image 671
Jmh2013 Avatar asked Mar 22 '12 02:03

Jmh2013


People also ask

Which syntax is used for sort in xsl document?

XSLT <xsl:sort> The <xsl:sort> element is used to sort the output. Note: <xsl:sort> is always within <xsl:for-each> or <xsl:apply-templates>.

How do I sort an XML output?

The XSLT <xsl:sort> element is used to specify a sort criteria on the nodes. It displays the output in sorted form. The <xml:sort> element is added inside the <xsl:for-each> element in the XSL file, to sort the output.

What is xsl parser?

The XSL based XML DOM Parser enables Tivoli® Directory Integrator to parse XML documents in any format using the XSL supplied by the user, into attribute value pairs, stored in the entry object. The XSL based parser is required to facilitate reading of any kind of XML format.

How do I substring in XSLT?

Substring is primarily used to return a section of a string or truncating a string with the help of the length provided. XSLT is incorporated with XPath while manipulating strings of text when XSLT access. The Substring function takes a string as an argument with the numbers one or two and an optional length.


1 Answers

At a glance,

    <xsl:sort select="Employee/Lastname" data-type="text" order="ascending"/>
    <xsl:sort select="Employee/Firstname" data-type="text" order="ascending"/>

should be

    <xsl:sort select="Lastname" data-type="text" order="ascending"/>
    <xsl:sort select="Firstname" data-type="text" order="ascending"/>

for-each sets the context node for the select, so the expression is evaluated against the Employee nodes.

Also, text and ascending are defaults, so you could just write

    <xsl:sort select="Lastname"/>
    <xsl:sort select="Firstname"/>
like image 86
harpo Avatar answered Oct 21 '22 14:10

harpo