Solved

Dynamic XSL using attribute values for sort

Posted on 2004-08-26
3
940 Views
Last Modified: 2013-11-19
I am trying to sort XML with XSL using the xsl:sort

This works fine when I use static text as the "select" and "order" but when I use Attributes or Variables it does not work correctly? Is it possible to use variables or attributes as the values for sort?

I.E.
<xsl:sort select="col1" order="ascending"/> works fine
<xsl:sort select="$column" order="$direction"/> does not work

Any help would be greatly appreciated.

here is my XSLT code:
<xsl:template match="ROOT">

<fieldset>
<legend>Vendor Check</legend>
<table border="0" cellspacing="1" cellpadding="3">
<tr>
      <th>Vendor</th>
      <th># Mos. Inactive</th>
      <th>Duplications</th>
      <th>Is Employee?</th>
</tr>

<xsl:for-each select="//Vendors">
<xsl:sort select="@orderBy" order="@Direction"/>
<tr>
      <xsl:if test="(position() mod 2 = 1)">
            <xsl:attribute name="bgcolor">#EFEFEF</xsl:attribute>
      </xsl:if>

      <td><xsl:value-of select="VendorName"/></td>
      <td><xsl:value-of select="MoInactive"/></td>
      <td><xsl:value-of select="dupCount"/></td>
      <td><xsl:value-of select="isEmployee"/></td>
</tr>
</xsl:for-each>

</table>

</fieldset>

</xsl:template>
</xsl:stylesheet>


here is the XML:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<ROOT orderBy="VendorName" Direction="descending">
      <Vendors>
            <VendorName>ACCESS LOCK & KEY</VendorName>
            <MoInactive>30</MoInactive>
      </Vendors>
      <Vendors>
            <VendorName>ACCORD TRANSPORTATION INC.</VendorName>
            <MoInactive>26</MoInactive>
      </Vendors>
      <Vendors>
            <VendorName>ACETO CORPORATION</VendorName>
            <MoInactive>19</MoInactive>
      </Vendors>
      <Vendors>
            <VendorName>ADDISON ENTERPRISES</VendorName>
            <MoInactive>19</MoInactive>
      </Vendors>
      <Vendors>
            <VendorName>ADT SECURITY SYSTEMS</VendorName>
            <MoInactive>24</MoInactive>
      </Vendors>
</ROOT>


0
Comment
Question by:NaderYacoub
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
conorj earned 500 total points
ID: 11903302
chenage your xsl:sort to

    <xsl:sort select="$column" order="{$direction}"/>

and it will work fine.

rgds,
Conor
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 11903768
No, it doesn't work that way. You can't evaluate a variable in a select, and use it's value in the XPath directly.  This is due to how XSLT is compiled.  He needs:

<xsl:for-each select="Vendors">
    <xsl:sort select="*[local-name() = ancestor::ROOT/@orderBy]" order="ancestor::ROOT/@Direction"/>


The order can take the variable or attribute directly.  Also, note the "//" is not needed before the Vendors in select="Vendors"

Finally, this element:

         <VendorName>ACCESS LOCK & KEY</VendorName>
 
is not well formed.  It needs to be:

         <VendorName>ACCESS LOCK &amp; KEY</VendorName>

Regards,
Mike Sharp
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 11903790
By the way, I prefer to control sorting by setting parameters in the transformation.  Here's a client-side example, but can be used server-side as well:

http://rdcpro.com/Members/rdcpro/snippets/filterandsort/

Regards,
Mike Sharp
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to count occurrences of each item in an array.
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 …

808 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