Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2004, 07:06 AM
Registered User
 
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Complex sorting

Hi there..

Here's the XML file I'm trying to process with my XSL file (simplified):

input.xml
Code:
<folders>
 <folder name="hello"/>
 <folder name="world"/>
</folders>
config.xml
Code:
<web>
 <icons>
  <folder filename="hello" order="1"/>
  <folder filename="world" order="2"/>
 </icons>
</web>
Basically, I want to apply templates to the 'folder' elements in the input.xml file, but use the order attribute from the config.xml file.

XSL snippet - latest attempt
Code:
<xsl:template match="folders">
 <xsl:apply-templates select="folder">
  <xsl:sort select="document('config.xml')/web/icons/folder[@filename=@name]/@order" data-type="number"/>
 </xsl:apply-templates>
</xsl:template>
Important bit:
Code:
document('config.xml')/web/icons/folder[@filename=@name]/@order
Basically, I'm trying to find the matching 'folder' element in the config.xml file, and use the order attribute of it. The problem lies in the '@filename=@name' part - I really want to do @filename=(folder-element-in-input.xml)/@name, if you can see where I'm coming from?

I tried this:

Code:
<xsl:template match="folders">
 <xsl:for-each select="folder">
  <xsl:variable name="theName">
   <xsl:value-of select="@name"/>
  </xsl:variable>
  <xsl:sort select="document('config.xml')/web/icons/folder[@filename=$theName]/@order" data-type="number"/>
  <xsl:apply-templates/>
 </xsl:for-each>
</xsl:template>
..but msxml rejects it - actually it says xsl:sort cannot be a child element of xsl:for-each! - but what it really means is xsl:sort has to be the first subnode of xsl:for-each - I think.

Can anyone help?

Thanks, much appreciated.

Kieren Johnstone

 
Old November 26th, 2004, 08:11 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Firstly I usually put external files into a variable, otherwise the processor may end up reading them more than necessary.
I think you need to use the current() function to get back to the node that was the context node at the start of your select:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:variable name="config" select="document('Config.xml')"/>
  <xsl:template match="/folders">    
    <xsl:apply-templates select="folder">
      <xsl:sort select="$config/web/icons/folder[@filename = current()/@name]/@order" data-type="number"/>      
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="folder">
    <xsl:copy-of select="."/><xsl:text>#xd;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Complex How To In XSLT Wee Bubba XSLT 2 April 8th, 2005 07:16 AM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
Complex Dataviews Alaric ADO.NET 0 October 16th, 2003 07:56 AM
Complex Table (at least for me) lcsgeek Classic ASP Basics 1 August 19th, 2003 02:53 PM
Complex table (at least for me) lcsgeek Classic ASP Professional 0 August 19th, 2003 10:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.