Solved

Dynamic XSL using attribute values for sort

Posted on 2004-08-26
3
937 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now