Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 30th, 2008, 11:26 PM
Registered User
 
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorting some nodes by attribute value

Helow all,

Does anyone know a way to sort on elements by attribute values, except for 1 or two elements, where I want them to stay in the order they already are in?

For example I need to sort all QC nodes in alpha order by @Name attribute, except for categories "Support" and "Support Tools", where I want "Support Tools before Support".


<QC @Name='Instructor'>
<QC @Name='Comments'>
<QC @Name='Job Impact'>
<QC @Name='Support>
<QC @Name='Support Tools'>

I'm trying this so far, but not getting what I want

<xsl:variable name="sortfield">Support Tools</xsl:variable>


     <xsl:apply-templates select="//QC" mode="OverallCats">

         <xsl:sort select="@*[name(.) = $sortfield]" data-type="string" order="ascending" />

Thanks in advance for the help,

ACW274
 
Old July 1st, 2008, 01:51 AM
Authorized User
 
Join Date: May 2008
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

your requirments are not very clear about this ascending/descending variations.
Can you specify what result are you expecting on the following data input:
Code:
    <QC Name="Instructor"/>
    <QC Name="Comments"/>
    <QC Name="Job Impact"/>
    <QC Name="Support"/>
    <QC Name="Supprot Not Tools"/>
    <QC Name="Support Tools"/>
    <QC Name="Z"/>


 
Old July 1st, 2008, 08:07 AM
Registered User
 
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I want the results (string datatype as @Name is straight text) in ascending order.


Further information for you though below.

All <QC> nodes have children like below, but I want all QC nodes sorted in alpha ascending order except I want Support Tools before Support as Support Tools has FTID=1 and Support has FTID=2

So, those two I want sorted in ascending order (number datatype) by @FTID and all the rest bye @Name (datatype of string)

<QC Name="Comments">
       <Q>
           <A>
              <FT @FTID =1/>
               <FT @FTID =2/>
               <FT @FTID =3/>
         </A>
       </Q>
 </QC>

<QC Name="Instructor">
      <Q>
           <A>
              <FT @FTID =1/>
               <FT @FTID =2/>
               <FT @FTID =3/>
         </A>
       </Q>
     </QC>
<QC Name="Job Impact">
      <Q>
           <A>
              <FT @FTID =1/>
               <FT @FTID =2/>
               <FT @FTID =3/>
         </A>
       </Q>
     </QC>

<QC Name="Support">
   <Q>
           <A>
              <FT @FTID =2>
           </A>
       </Q>
     </QC>

<QC Name="Support Tools">
      <Q>
           <A>
              <FT @FTID =1>
           </A>
       </Q>
     </QC>



I want the following results:

Comments
Instructor
Job Impact
Support Tools
Support


Thanks in advance for your help

ACW274

 
Old July 1st, 2008, 09:48 AM
Authorized User
 
Join Date: May 2008
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="QC">
                <xsl:sort select="Q/A/FT/@FTID" order="ascending"/>
                <xsl:sort select="name()" order="ascending"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>
    <xsl:template match="QC">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>
 
Old July 1st, 2008, 10:52 PM
Registered User
 
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This worked for you. It did not work for me.
All QC nodes came in @FTID order, but I only want Support Tools and Support to come in @FTID, the other QC nodes to come in alpha order by @Name.

Thanks,

 
Old July 1st, 2008, 11:19 PM
Authorized User
 
Join Date: May 2008
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:It did not work for me.
please, provide sample input data that you faced problems with.
on input data you provided before - the result is as you wished.

PS
to make it clear - please, provide the expected output for the following input data:

Code:
<root>
    <QC Name="Support">
        <Q>
            <A>
                <FT FTID="2"/>
            </A>
        </Q>
    </QC>
    <QC Name="Support AAA">
        <Q>
            <A>
                <FT FTID="1"/>
                <FT FTID="2"/>
                <FT FTID="3"/>
            </A>
        </Q>
    </QC>
    <QC Name="Support Tools">
        <Q>
            <A>
                <FT FTID="1"/>
            </A>
        </Q>
    </QC>
</root>
 
Old July 2nd, 2008, 12:00 AM
Registered User
 
Join Date: Jun 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The following document order comes in as @FTID order for all QC nodes but I only want Support Tools before Support, and the rest in alpha order by @Namee

<QC Name="Instructor">
       <Q>
           <A>
               <FT @FTID =2/>
               <FT @FTID =3/>
         </A>
       </Q>
 </QC>

<QC Name="Comments">
      <Q>
           <A>
              <FT @FTID =1/>
               <FT @FTID =2/>

         </A>
       </Q>
     </QC>
<QC Name="Business Results">
      <Q>
           <A>
              <FT @FTID =1/>
                       </A>
       </Q>
     </QC>

<QC Name="Job Impact">
   <Q>
           <A>
              <FT @FTID =2>
           </A>
       </Q>
     </QC>

<QC Name="Support">
      <Q>
           <A>
              <FT @FTID =1>
           </A>
       </Q>
     </QC
<QC Name="Support Tools">
      <Q>
           <A>
              <FT @FTID =1>
           </A>
       </Q>
     </QC>

This is the desired output:

Business Results
Comments
Instructor
Job Impact
Support Tools
Support
-----------------------

Not this:

Instructor
Support Tools
Comments
Business Results
Job Impact
Support





 
Old July 2nd, 2008, 01:11 AM
Authorized User
 
Join Date: May 2008
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please, give the desired output for the input I mentioned in my last post.

PS
in your last post
Code:
...
<QC Name="Support">
      <Q>
           <A>
              <FT @FTID =1>
           </A>
       </Q>
     </QC
<QC Name="Support Tools">
      <Q>
           <A>
              <FT @FTID =1>
           </A>
       </Q>
     </QC>
@FTID have the same value for both: 'Support' and 'Support Tools', so why 'Support Tools' should appear first?
 
Old July 2nd, 2008, 01:22 AM
Authorized User
 
Join Date: May 2008
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

probably you're looking for smth like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="root">
        <root>
            <xsl:apply-templates select="QC">
                <xsl:sort select="substring(@Name,1,7)" order="ascending"/>
                <xsl:sort select="Q/A/FT/@FTID" order="ascending"/>
            </xsl:apply-templates>
        </root>
    </xsl:template>
    <xsl:template match="QC">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>
but it's only a guess






Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding and Sorting nodes georgemeng XSLT 4 December 1st, 2008 12:13 PM
How can I go through all child nodes & attribute.. vishnu108mishra C# 1 November 13th, 2007 05:00 AM
XPath - Selecting nodes based on attribute values billy_bob_the_3rd XML 4 December 1st, 2004 06:12 PM
HELP! More complicate sorting nodes question kevin_in_black XSLT 2 April 13th, 2004 05:51 AM
passing attribute info while traversing nodes Ashley XML 1 June 20th, 2003 07:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.