Solved

Dynamic XSL using attribute values for sort

Posted on 2004-08-26
3
943 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

695 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