Solved

Sorting in XSLT

Posted on 2004-10-12
7
218 Views
Last Modified: 2013-11-18
Hi,

I am having problem sorting on child-nodes (Article elements).
I would like to sort Article elements instead of Document elements
(below), by state-attribute. State is an integer.

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

       <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

       <xsl:template match="/">
               <Documents>
                       <xsl:apply-templates select="Documents/Document">
                               <xsl:sort select="@state" order="ascending"/>
                       </xsl:apply-templates>
               </Documents>
       </xsl:template>

       <xsl:template match="/Documents/Document">
               <xsl:copy>
                               <!-- I need sorting on this level -->
                               <xsl:copy-of select="@* | Article[@info = 'food']"/>
               </xsl:copy>
       </xsl:template>
</xsl:stylesheet>

<xsl:sort select="@title" order="ascending"/>

-xrow
0
Comment
Question by:xrow
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:J_Mak
ID: 12286173
Can you please post your XML file. Could you please just elaborate more on the specifics of what you want to sort, thanks.
0
 

Author Comment

by:xrow
ID: 12286237
Beside the filtering part already done in the XSLT,
what I want to sort is elements that contain attribute state. I would like to sort Article elements by state-attribute in the acesending order.

XML file:

<Documents>
       <Document title="1" chapter="1" href="file1.xml" filter="food">
               <Article title="1.1" info="sub" filter="food" state="1"/>
               <Article title="1.2" info="main" filter="food" state="3"/>
       </Document>
       <Document title="2" chapter="2" href="file2.xml" filter="drink">
               <Article title="2.1" info="sub" filter="drink" state="4"/>
               <Article title="2.2" info="main" filter="food" state="2"/>
       </Document>
       <Document title="3" chapter="3" href="file2.xml" filter="">
               <Article title="3.1" info="sub" filter="drink" state=""/>
               <Article title="3.2" info="child" filter="" state="8"/>
       </Document>
</Documents>
0
 
LVL 7

Expert Comment

by:J_Mak
ID: 12286386
Did you want to output an XML file or just plain text like so:

<Article title="2.2" info="main" filter="food" state="2"/>
<Article title="1.2" info="main" filter="food" state="3"/>
<Article title="2.1" info="sub" filter="drink" state="4"/>
<Article title="1.1" info="sub" filter="food" state="1"/>
<Article title="3.1" info="sub" filter="drink" state="7"/>
<Article title="3.2" info="child" filter="food" state="8"/>

Also, did you want to do 2 sorts? Becaues in your question you indicate that you want to sort by title attributes:

<xsl:sort select="@title" order="ascending"/>

And on another line you indicate this:

<!-- I need sorting on this level -->
<xsl:copy-of select="@* | Article[@info = 'food']"/>

And then on another line:

<xsl:sort select="@state" order="ascending"/>
0
 

Author Comment

by:xrow
ID: 12287203

<!-- I need sorting on this level -->
<xsl:copy-of select="@* | Article[@info = 'food']"/>

all other functions except sorting are supposed to be left there. i need to add sorting on Article level.
dissregard sorting on Document level. it is not necessary. Only Article level (sort-by state) is needed.

thanx.

-mike
0
 
LVL 6

Accepted Solution

by:
PeterCiuffetti earned 50 total points
ID: 12434833
This XSLT will sort the articles by @state.  I do it in a for-each loop that selects all articles.  If you do it inside an apply-templates, then it will only sort the children of the current element.  If you want to sort all the articles, it have to grab all of them in a single pass.

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="Documents">
       <Documents>
           <xsl:for-each select="Document/Article">
                        <xsl:sort select="@state" order="ascending"/>
                        <xsl:apply-templates select="."/>
           </xsl:for-each>
       </Documents>
</xsl:template>

<xsl:template match="Article">
        <xsl:copy>
                 <xsl:copy-of select="@* | Article[@info = 'food']"/>
        </xsl:copy>
</xsl:template>

</xsl:stylesheet>


Produces...

<Documents>
  <Article title="3.1" info="sub" filter="drink" state=""></Article>
  <Article title="1.1" info="sub" filter="food" state="1"></Article>
  <Article title="2.2" info="main" filter="food" state="2"></Article>
  <Article title="1.2" info="main" filter="food" state="3"></Article>
  <Article title="2.1" info="sub" filter="drink" state="4"></Article>
  <Article title="3.2" info="child" filter="" state="8"></Article>
</Documents>

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now