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 June 30th, 2011, 01:59 AM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default Order XML elements using XSLT

I have an XML with few elements and an XSD that tells the order in which the elements from the XML should appear.

Using XSLT, can I change the order of the elements in the XML to conform to the XSD?
 
Old June 30th, 2011, 02:18 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm sure the answer is yes, but I suspect it would be some very complicated XSLT.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old June 30th, 2011, 05:33 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Let's split this into two steps: (a) extract from the XSD the order in which you want elements to appear, (b) do the reordering.

Let's start with (b). If we assume we're talking about a simple linear ordering (no choice or repetition) then it's not too difficult. Define the required order like this:

Code:
<xsl:variable name="order" as="element(*)>
  <name/><title/><description/><status/><asphyxiation/>
</xsl:variable>
and then sort your elements like this:

Code:
<xsl:perform-sort select="$input">
  <xsl:sort select="index-of($order/node-name(.), node-name(.))"/>
</xsl:perform-sort>
Stage (a), extracting information from a schema, is more difficult, and it all depends how clever you want to be (e.g, in handling choice and repetition, not to mention such things as types derived by extension or restriction, substitution groups, or the like.)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
imshriram (July 10th, 2011)
 
Old July 1st, 2011, 03:26 AM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Hi Kay, Thanks for your reply. I am feeling better to know that this could be achieved in the first place.
But i am not able to get it to work:
Input:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <group>
        <element4>SomeValue4</element4>
      <element1>Changed1</element1>
      <element2>Changed2</element2>
      <element3>Changed3</element3>
    </group>
</root>
XSL:
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
 <xsl:variable name="order" as="element(*)">
  <element1/><element2/><element3/><element4/>
</xsl:variable>
<xsl:variable name="input" select="/" />
<xsl:template match="/">
<xsl:perform-sort select="$input">
  <xsl:sort select="index-of($order/node-name(.), node-name(.))"/>
</xsl:perform-sort>
</xsl:template>
</xsl:stylesheet>
But the output i get is
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <group>
        <element4>SomeValue4</element4>
      <element1>Changed1</element1>
      <element2>Changed2</element2>
      <element3>Changed3</element3>
    </group>
</root>
I am not sure on what i am doing wrong.
 
Old July 1st, 2011, 04:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've set $input to a single node (the root node, '/'). There's not much point in sorting a sequence that contains only one node. Try

Code:
<xsl:variable name="input" select="/root/group/*" />
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
imshriram (July 10th, 2011)
 
Old July 1st, 2011, 05:36 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You also need to make the following change to the sort:

Code:
<xsl:sort select="index-of($order//node-name(.), node-name(.))"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
imshriram (July 10th, 2011)
 
Old July 9th, 2011, 04:08 AM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Oops, I get the following error now,

A sequence of more than one item is not allowed as the value of variable $order (<element1/>, <element2/>, ...) ; SystemID: ; Line#: 9; Column#: -1
 
Old July 9th, 2011, 06:16 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Change
Code:
<xsl:variable name="order" as="element(*)">
to
Code:
<xsl:variable name="order" as="element()*">
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
imshriram (July 10th, 2011)
 
Old July 10th, 2011, 08:17 AM
Authorized User
 
Join Date: Apr 2011
Posts: 22
Thanks: 4
Thanked 0 Times in 0 Posts
Default

You guys are awesome.
Kay, Martin Honnen and Samjudson. How do i close this thread as answered?
 
Old July 10th, 2011, 10:56 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't mark a post as the 'answer' on here. I think you can click the 'Thanks' button for any post that is helpful though.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to access XML elements one at a time using xslt? Techcarol XSLT 19 January 16th, 2011 09:56 AM
XSLT for displaying XML data in list order kvishnu XSLT 1 May 11th, 2009 08:27 AM
Filtering XML Elements using XSLT. alapati.sasi XSLT 5 March 23rd, 2009 11:56 AM
Getting fields, specific order from xml using xslt Jaipal XSLT 4 July 23rd, 2007 11:35 AM
renumbering xml elements with XSLT csbdeady XSLT 5 July 27th, 2005 09:17 AM





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