Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting with XSLT

UPDATE - New code at the bottom

I'm trying to figure out how to use the sort function to pull the most recent record from some XML data. I'm very new to using XSLT and am running into a bunch of problems. Here's an example of my data...

<content date="1/13/2011 1:21:00 PM">
    <collection vo="promotion">
        <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
        <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
    </collection>
</content>

What I want to do is sort the data by promotionid in decsending order and then ONLY ouput via HTML the promotionid that is greatest. Here is along the lines of what I was trying

UPDATE - This is the latest version of the code that is still experiencing problems.

<html><body>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"  encoding="UTF-8" />
    <xsl:template match="content/collection/data">
        <xsl:apply-templates>
            <xsl:sort select="promotionid" order="descending" data-type="number" />
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="content/collection">
        <xsl:value-of select="data/@promotionid" />
    </xsl:template> 
</xsl:stylesheet>
</body></html>

While this does return results what I am getting back is '64526' and NOT '64646'.

Can anyone help? Also I've seen examples online where you can sort by multiple fields. It may be worth noting now, rather then asking later, that we may want to end up sorting by startdate rather than promotionid. I have managed to come up with code to break out the date by YYYY, MM, and DD, but have no idea how I would even begin to use that aside from using those as my select paramater of the sort, but I don't know if that actually works or not.

Year
<xsl:value-of select="substring(substring-after(substring-after(data/@startdate,'/'),'/'),1,4)" />

Month
<xsl:value-of select="substring-before(data/@startdate,'/')" />

Day
<xsl:value-of select="substring-before(substring-after(data/@startdate,'/'),'/')" />

Thank in advance and I appologize my less than novice XSLT skills.

------------------------------------------------------

After some help here the code has changed, but is still not working as intended. Here is the code...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"  encoding="UTF-8" />
    <xsl:template match="content/collection/">
            <xsl:apply-templates>
                <xsl:sort select="@promotionid" order="descending" data-type="number" />
            </xsl:apply-templates>
        </xsl:template>

 <xsl:template match="content/collection/data">
        <xsl:if test="position()=1">
                   <xsl:value-of select="@promotionid"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

And I am still seeing the lesser value ouput rather than the greater. Perhaps there is another way to do this with out sorting? As I am open to that possibility as well.

1/14/11 10:37 Update *-------------------------------------------------------------------* Okay using this code now does indeed sort the data and output the highest promotionid number. Thanks a Ton!

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="collection">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="data">
         <xsl:sort select="@promotionid" data-type="number" order="descending"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
<xsl:template match="content/collection/data">
        <xsl:if test="position()=1">
                   <xsl:value-of select="@promotionid"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Ignoring the promtionid now can you show me how I would sort, descending, by JUST the date? I tried removing Unfortunately I know the dates should have a static length, but we have no control of the data we receive :-(

Also can you recommend a book to start with to really can some better understanding of all this? You've been a tremendous help!

like image 531
dscl Avatar asked Jan 13 '11 20:01

dscl


People also ask

What is sorting in XML?

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.

Which of the following will code sort the books on Title parameter in XSLT?

The result of substring-after(Title, ' ') will be everything after the first space in the Title element. As it iterates through the elements, the XSLT processor will be sorting this set: and Peace. Bostonians.

How do you sort tags in XML?

The xsl:sort does sort the file elements before applying the templates with the sort key ref and the implied default order ascending . Now pass both files (the XML and the XSLT stylesheet) to an XSLT processor of your choice and you'll get the desired (sorted) output.

How do I apply a template in XSLT?

The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a "select" attribute to the <xsl:apply-templates> element, it will process only the child elements that matches the value of the attribute.


1 Answers

<xsl:sort select="promotionid" order="descending" data-type="number"

/>

There is an obvious error here: promotionid is an attribute, not an element.

Solution:

select="@promotionid" order="descending" data-type="number" />

Another error:

<xsl:template match="content/collection/data">
    <xsl:apply-templates>
        <xsl:sort select="promotionid" order="descending" data-type="number" />
    </xsl:apply-templates>
</xsl:template>

The <xsl:apply-templates> and sorting is performed too-late.

You want:

<xsl:template match="content/collection/">
            <xsl:apply-templates>
                <xsl:sort select="@promotionid" order="descending" data-type="number" />
            </xsl:apply-templates>
        </xsl:template>

and

    <xsl:template match="content/collection/data">
        <xsl:if test="position()=1">
                   <xsl:value-of select="@promotionid"/>
        </xsl:if>
    </xsl:template>

As for your expanded question:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="collection">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="data">
         <xsl:sort select="@promotionid" data-type="number" order="descending"/>
         <xsl:sort select=
          "concat(
             substring-after(substring-after(substring-before(@startdate,' ')
                                             ,'/'
                                             ),
                               '/'
                               ),
             substring-before(substring-after(substring-before(@startdate,' ')
                                             ,'/'
                                             ),
                              '/'
                            ),
             substring-before(substring-after(substring-before(@startdate,' ')
                                             ,'/'
                                             ),
                               '/'
                               )
                )"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

when applied to the provided XML document:

<content date="1/13/2011 1:21:00 PM">
    <collection vo="promotion">
        <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
        <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
    </collection>
</content>

produces the wanted result:

<content date="1/13/2011 1:21:00 PM">

   <collection vo="promotion">
      <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
      <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
   </collection>

</content>

However, note that this well not handle the variable length for the date components. It is better to use fixed length format: mm/dd/yyyy

like image 159
Dimitre Novatchev Avatar answered Nov 15 '22 10:11

Dimitre Novatchev