p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 1st, 2008, 12:26 AM
Registered User
 
Join Date: Jul 2008
Location: , , .
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 1st, 2008, 02:51 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
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"/>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old July 1st, 2008, 09:07 AM
Registered User
 
Join Date: Jul 2008
Location: , , .
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old July 1st, 2008, 10:48 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old July 1st, 2008, 11:52 PM
Registered User
 
Join Date: Jul 2008
Location: , , .
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,

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old July 2nd, 2008, 12:19 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old July 2nd, 2008, 01:00 AM
Registered User
 
Join Date: Jul 2008
Location: , , .
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





Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old July 2nd, 2008, 02:11 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #9 (permalink)  
Old July 2nd, 2008, 02:22 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 06:51 AM
passing attribute info while traversing nodes Ashley XML 1 June 20th, 2003 08:42 AM



All times are GMT -4. The time now is 07:56 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc