Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamic XSL using attribute values for sort

Posted on 2004-08-26
3
Medium Priority
?
951 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 2000 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…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

927 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