?
Solved

Dynamic XSL using attribute values for sort

Posted on 2004-08-26
3
Medium Priority
?
949 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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

743 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