Solved

XML Sort on attribute using xsl:sort

Posted on 2009-07-04
6
392 Views
Last Modified: 2013-11-19
Hi,
I need to sort an xml document by an attribute. I am unable to do this by using the attached xsl stylesheet. Please help.

I need to sort the below document (dataField Nodea) by it's Table_Id attribute

As I said the above xslt does not work for sorting on Table_Id attribute, although the same thing works for Node_Id attribute if I just replace "DataField/@Table_Id" with "DataField/@Node_Id" in xslt. I am completely baffled by it's behavior. I am using VB6 (I think that should not matter).

I am very concerned as to why it works with one field and not the other.

Any help with some explaination is greatly appreciated.

Thanks in advance
My xml document

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

<SubCompetency Node_Id = "3" Node_Name = "SubComp1">

    <DataField

        Table_Id = "14"

        Node_Id = "13"

        Node_Name = "Data1"

        ReqToCmp = "1"

        Def_Val = "NULL">

        <HtmField Node_Id = "16" Node_Name = "Field1">

            <HtmGrpField

                Node_Id = "79"

                Node_Name = "Grp1"

                Node_Val = "5"/>

            <HtmGrpField

                Node_Id = "80"

                Node_Name = "Grp2"

                Node_Val = "4"/>

            <HtmGrpField

                Node_Id = "81"

                Node_Name = "Grp3"

                Node_Val = "3"/>

            <HtmGrpField

                Node_Id = "82"

                Node_Name = "Grp4"

                Node_Val = "2"/>

            <HtmGrpField

                Node_Id = "83"

                Node_Name = "Grp5"

                Node_Val = "1"/>

            <HtmGrpField

                Node_Id = "84"

                Node_Name = "Grp6"

                Node_Val = "0"/>

        </HtmField>

    </DataField>

    <DataField

        Table_Id = "8"

        Node_Id = "25"

        Node_Name = "Data2"

        ReqToCmp = "1"

        Def_Val = "NULL">

        <HtmField Node_Id = "28" Node_Name = "Field2">

            <HtmGrpField

                Node_Id = "151"

                Node_Name = "Grp1"

                Node_Val = "5"/>

            <HtmGrpField

                Node_Id = "152"

                Node_Name = "Grp1"

                Node_Val = "4"/>

            <HtmGrpField

                Node_Id = "153"

                Node_Name = "Grp1"

                Node_Val = "3"/>

            <HtmGrpField

                Node_Id = "154"

                Node_Name = "Grp4"

                Node_Val = "2"/>

            <HtmGrpField

                Node_Id = "155"

                Node_Name = "Grp5"

                Node_Val = "1"/>

            <HtmGrpField

                Node_Id = "156"

                Node_Name = "Grp6"

                Node_Val = "0"/>

        </HtmField>

    </DataField>

    <DataField

        Table_Id = "3"

        Node_Id = "26"

        Node_Name = "Data3"

        ReqToCmp = "1"

        Def_Val = "NULL">

        <HtmField Node_Id = "29" Node_Name = "Field3">

            <HtmGrpField

                Node_Id = "157"

                Node_Name = "Grp1"

                Node_Val = "5"/>

            <HtmGrpField

                Node_Id = "158"

                Node_Name = "Grp2"

                Node_Val = "4"/>

            <HtmGrpField

                Node_Id = "159"

                Node_Name = "Grp3"

                Node_Val = "3"/>

            <HtmGrpField

                Node_Id = "160"

                Node_Name = "Grp4"

                Node_Val = "2"/>

            <HtmGrpField

                Node_Id = "161"

                Node_Name = "Grp5"

                Node_Val = "1"/>

            <HtmGrpField

                Node_Id = "162"

                Node_Name = "Grp6"

                Node_Val = "0"/>

        </HtmField>

    </DataField>

</SubCompetency>

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

My Xsl stylesheet

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

<?xml version="1.0" encoding="UTF-8"?>"

    <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="SubCompetency|@*">"

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

    <xsl:sort select="DataField/@Table_Id" data-type="number" order="descending"/>"

    </xsl:apply-templates></xsl:copy></xsl:template>"

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

    </xsl:copy></xsl:template></xsl:stylesheet>"

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

Open in new window

0
Comment
Question by:Bagur
  • 4
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24779667
I think that it should not work with @Node_Id either
You are nesting one element too deep
<xsl:sort select="DataField/@Table_Id"
should be
<xsl:sort select="@Table_Id"
I think that nodes are incidently preorderd on Node_Id, because it should not have worked

There are some issues with ambiguous templates for @*,
I rearranged you stylesheet a bit, see below
<?xml version="1.0" encoding="UTF-8"?>

<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="SubCompetency">

        <xsl:copy>

            <xsl:copy-of select="@*"/>

            <xsl:apply-templates select="node()">

                <xsl:sort select="@Table_Id" data-type="number" order="descending"/>

            </xsl:apply-templates>

        </xsl:copy>

    </xsl:template>

    <xsl:template match="node()">

        <xsl:copy>

            <xsl:copy-of select="@*"/>

            <xsl:apply-templates select="node()"/>

        </xsl:copy>

    </xsl:template>

</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:Bagur
ID: 24781371
Hi Gertone,

The solution worked, gr8.., but I still need to understand 2 more things

a) The @Node_Id was not ordered incidentally, I made sure of that and the sort works for @Node_Id

b) Why do you say that it is ordered one element deep? I am naive to this, so please explain how. I thought since we are matching subCompetency node, the DataField/@Table_Id falls directly underneath subCompetency Node and it should work fine. Anyway, the @Table_Id is an attribute of DataField, so what's wrong with mentioning DataField/@Table_Id? I know it does not work like that, but please explain how/why.

Thanks.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 24781379
You are right that the template is on subCompentency,
in there you state
            <xsl:apply-templates select="node()">
                <xsl:sort select="@Table_Id" data-type="number" order="descending"/>
            </xsl:apply-templates>
The node() in this case is the child elements (so one level deep), including DataField
So you are pushing the DataField nodes to the templates and those DataField nodes you want to sort.
The sort select XPath starts from the nodes mentioned in the select on the apply-templates
So, what you really want is sorting on the @Table_id of the DataField
... you want to sort
DataField on
subCompetency/DataField/@Table_id
and not on
subCompetency/DataField/DataField/@Table_id
that was your error.
You lost sight of the context awareness of the XPath
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24781385
about your a)
 DataField/@Node_Id can not work,
likely you had put
 @Node_Id
0
 

Author Closing Comment

by:Bagur
ID: 31599861
Thank you very much for the detailed explaination!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24781398
welcome
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Using YubiKey with REST API application 2 108
JQuery tracking event. 3 57
read an xml file in perl 2 15
Shredding xml into an oracle 11g Database 2 31
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

861 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

30 Experts available now in Live!

Get 1:1 Help Now