Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old December 3rd, 2007, 04:47 PM
Authorized User
 
Join Date: Jun 2003
Location: Corona, CA, USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to victorcorey
Default Sort xml file

Hello all,

I am new to xslt and I am trying to do something I think would be simple.

I am loading an xml file using the Microsoft.XMLDOM client-side. I need to sort this and I think I need to use XSLT. I would like to pass in a parameter (the node being sorted) and direction and then render the same xml (just sorted)

Help.
Thank you,
Victor

--
Victor Corey
__________________
--
Victor Corey
  #2 (permalink)  
Old December 3rd, 2007, 07:26 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, this seems a reasonable approach. Which part of it are you having difficulty with?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old December 4th, 2007, 01:32 AM
Authorized User
 
Join Date: Jun 2003
Location: Corona, CA, USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to victorcorey
Default

A couple of areas :-(

1. I think my xslt is incorrect for the output xml. It renders correctly but adds the html, head, and body tags.
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output omit-xml-declaration="no" method="xml" indent="yes"/>
  <xsl:param name="SortCol" />
  <xsl:param name="SortDir" />
  <xsl:param name="SortType" />
  <xsl:template match="worldheritage">
  <xsl:value-of select="$SortDir" />
    <xsl:for-each select="row">
        <xsl:sort order="{$SortDir}" select="*[name()=$SortCol]" data-type="{$SortType}" />
            <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
2. Client-side how do I pass parameters? I'm using the Microsoft.XMLDOM parser.
Code:
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(sFile);
xmlObj = xmlDoc.documentElement;
I need to add the xslt and pass in the parameters.

Thanks,
Victor

--
Victor Corey
  #4 (permalink)  
Old December 4th, 2007, 09:50 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to use parameters then you need to use the addParameter method of the XSLTProcessor object, see the MSXML SDK http://msdn2.microsoft.com/en-us/library/ms762312.aspx

  #5 (permalink)  
Old December 4th, 2007, 01:31 PM
Authorized User
 
Join Date: Jun 2003
Location: Corona, CA, USA.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to victorcorey
Default

OK. I think I am close. My xslt almost works. It sorts the data but only shows the row nodes not the root node.

Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
  <xsl:param name="SortCol" />
  <xsl:param name="SortDir" />
  <xsl:param name="SortType" />
  <xsl:template match="worldheritage">
    <xsl:for-each select="row">
      <xsl:sort order="{$SortDir}" select="*[name()=$SortCol]" data-type="{$SortType}" />
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Here's the xml data:
Code:
<worldheritage promo="UNESCO">
<row id="27" unique="30" trans="0" lat="37.26166667" long="-108.48555560">
    <criteria>(iii)</criteria>
    <inscribeDate>1978</inscribeDate>
    <code>us</code>
    <region>North America</region>
    <location>Colorado</location>
    <state>United States of America</state>
    <site>Mesa Verde National Park</site>
    <description>A great concentration of ancestral Pueblo Indian dwellings, built from the 6th to the 12th century, can be found on the Mesa Verde plateau in south-west Colorado at an altitude of more than 2,600 m. Some 4,400 sites have been recorded, including villages built on the Mesa top. There are also imposing cliff dwellings, built of stone and comprising more than 100 rooms.</description>
  </row>
  .
  .
  .
</worldheritage>
I just need to sort the xml by state node and return the xml.

Thanks,
Victor

--
Victor Corey
  #6 (permalink)  
Old December 4th, 2007, 01:40 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think you simply want to copy the root node from the XML input document e.g. you need

Code:
  <xsl:template match="worldheritage">
   <xsl:copy>
    <xsl:for-each select="row">
      <xsl:sort order="{$SortDir}" select="*[name()=$SortCol]" data-type="{$SortType}" />
      <xsl:copy-of select="."/>
    </xsl:for-each>
   </xsl:copy>
  </xsl:template>


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to sort a text file? spierson99 SQL Server DTS 3 November 28th, 2007 06:20 PM
Sort XML in dropdownlist Hughesie78 XSLT 1 November 23rd, 2007 08:41 AM
Need Xsl to Sort the following simple xml: Takashi321 XSLT 3 August 27th, 2007 11:54 AM
how to sort cross tab.sort based on row total joxa83 Crystal Reports 7 March 2nd, 2006 09:12 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM





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