Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT sort by attribute value

Tags:

html

xml

xslt

I have a question about how to sort based on attribute value.

I have the following source document and I would like to sort the track items by the value of the title class value.

Hopefully someone can help with this.

 <trackList>

    <track>
        <location>http://localhost/vmydoc</location>
        <title class="STD">Data Two</title>
        </track>
    <track>
        <location>http://localhost/vmydoc</location>
        <title class="SH">Data Three</title>

    </track>
    <track>
        <location>http://localhost/vmydoc</location>
        <title class="STD">Data Four</title>

    </track>
    <track>
        <location>http://localhost/vmydoc</location>
        <title class="SH">Data Five</title>

    </track>
</trackList>

The final output should look like this:

<trackList>

    <track>
        <location>http://localhost/vmydoc</location>
        <title class="SH">Data Three</title>

    </track>

    <track>
        <location>http://localhost/vmydoc</location>
        <title class="SH">Data Five</title>

    </track>

    <track>
        <location>http://localhost/vmydoc</location>
        <title class="STD">Data Four</title>

    </track>
    <track>
        <location>http://localhost/vmydoc</location>
        <title class="STD">Data Two</title>
    </track> 
</trackList>

I have tried the following but it does not work.

<xsl:for-each-group select="title" group-by="@class">

    <xsl:for-each select="current-group()">
        <xsl:value-of select="@class" />
    </xsl:for-each>

</xsl:for-each-group>

Thanks.

like image 924
ManUO Avatar asked Jul 18 '13 02:07

ManUO


People also ask

How do I sort numbers in XSLT?

XSLT's xsl:sort instruction lets you sort a group of similar elements. Attributes for this element let you add details about how you want the sort done -- for example, you can sort using alphabetic or numeric ordering, sort on multiple keys, and reverse the sort order.

How do you sort data in XML?

USAGE: sortxml.exe [options] infile [outfile] infile The name of the file to sort, etc. outfile The name of the file to save the output to.

How do you use variables in XSLT?

XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

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.


1 Answers

You can do this as follows:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="trackList">
    <xsl:copy>
      <xsl:apply-templates select="track">
        <xsl:sort select="title/@class"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

When run on your sample input, the result is:

<trackList>
  <track>
    <location>http://localhost/vmydoc</location>
    <title class="SH">Data Three</title>

  </track>
  <track>
    <location>http://localhost/vmydoc</location>
    <title class="SH">Data Five</title>

  </track>
  <track>
    <location>http://localhost/vmydoc</location>
    <title class="STD">Data Two</title>
  </track>
  <track>
    <location>http://localhost/vmydoc</location>
    <title class="STD">Data Four</title>

  </track>
</trackList>
like image 76
JLRishe Avatar answered Nov 16 '22 00:11

JLRishe