Subject: Sorting some nodes by attribute value
Posted By: acw274 Post Date: 6/30/2008 11:26:29 PM
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" data-type="string" order="ascending" />-->
         <xsl:sort select="@*[name(.) = $sortfield]" data-type="string" order="ascending" />

Thanks in advance for the help,

ACW274
Reply By: Volder Reply Date: 7/1/2008 1:51:01 AM
your requirments are not very clear about this ascending/descending variations.
Can you specify what result are you expecting on the following data input:
    <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"/>


Reply By: acw274 Reply Date: 7/1/2008 8:07:43 AM
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

Reply By: Volder Reply Date: 7/1/2008 9:48:43 AM
<?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>


Reply By: acw274 Reply Date: 7/1/2008 10:52:45 PM
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,

Reply By: Volder Reply Date: 7/1/2008 11:19:15 PM
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:

<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>
Reply By: acw274 Reply Date: 7/2/2008 12:00:11 AM
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





Reply By: Volder Reply Date: 7/2/2008 1:11:13 AM
Please, give the desired output for the input I mentioned in my last post.

PS
in your last post
...
<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?
Reply By: Volder Reply Date: 7/2/2008 1:22:00 AM
probably you're looking for smth like this:

<?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


Go to topic 72444

Return to index page 1