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 May 11th, 2009, 05:29 AM
Authorized User
 
Join Date: May 2009
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Default Sequencing a XML file

Hi I have a requirement where I need to restructure the XML file using the xsl sort based on the attribute. sno is the attribute i am using.

My input xml is
----------------
<emp>

<emp1 sno="8">
<name>Chandra</name>
<country>India</country>
<company>B</company>
<dept>
<deptno>6</deptno>
<deptname>Dev</deptname>
</dept>
</emp1>
<emp2 sno="7">
<name>Vijay</name>
<country>Australia</country>
<company>C</company>
<dept>
<deptno>1</deptno>
<deptname>RND</deptname>
</dept>
</emp2>

</emp>

My Output xml requirement is as follows:
---------------------------------------
<emp>
<emp2>
<name>Vijay</name>
<country>Australia</country>
<company>C</company>
<dept>
<deptno>1</deptno>
<deptname>RND</deptname>
</dept>
</emp2>
<emp1>
<name>Chandra</name>
<country>India</country>
<company>B</company>
<dept>
<deptno>6</deptno>
<deptname>Dev</deptname>
</dept>
</emp1>
</emp>

I am not able to get the root element but able to acheive

<emp2>
<name>Vijay</name>
<country>Australia</country>
<company>C</company>
<dept>
<deptno>1</deptno>
<deptname>RND</deptname>
</dept>
</emp2>
<emp1>
<name>Chandra</name>
<country>India</country>
<company>B</company>
<dept>
<deptno>6</deptno>
<deptname>Dev</deptname>
</dept>
</emp1>

My xsl code:
-------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="//*[@sno]">
<xsl:sort select="@sno"/>
<xsl:variable name="temp">
<xsl:value-of select="local-name()"/>
</xsl:variable>
<xsl:element name="{$temp}">
<xsl:apply-templates/>
</xsl:element>
</xsl:for-each>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Please suggest.
 
Old May 11th, 2009, 05:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, it's a very strange use of XML for each employee to have a different element name. If you have any influence over the design, get this changed.

It's simple to add a wrapper element:

<xsl:template match="/">
<emp>
<xsl:for-each ...

</xsl:for-each>
</emp>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 11th, 2009, 05:50 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try this:
Code:
<xsl:template match="/">
<emp>
<xsl:for-each select="//*[@sno]">
<xsl:sort select="@sno"/>
<xsl:variable name="temp">
<xsl:value-of select="local-name()"/>
</xsl:variable>
<xsl:element name="{$temp}">
<xsl:apply-templates/>
</xsl:element>
</xsl:for-each>
</emp>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

__________________
Rummy
 
Old May 11th, 2009, 07:57 AM
Authorized User
 
Join Date: May 2009
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi Michael thanks for the reply. In the above suggestion we are hardcoding the root element but i dont want to do that because i want to identify the parent instead of hardcoding. can you plz suggest that?

Actually this is a prototype that i am trying to solve original requirement. We have an input xml file coming in an order different to defined XSD. So we will will sequence this xml file to correct order of the xsd before giving input to webservice as the validation fails otherwise. In my xsl file by adding the attribute keys [sno is the attribute in the above prototype] and sort based on the attribute key. Can you suggest an alternate approach. Also other approach i am giving it a try is to parse the XML file using the DOM Parser.


Thank you.
 
Old May 11th, 2009, 08:34 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Where do you want to take the root element from? From the input element? Then simply copy it over e.g.
Code:
<xsl:template match="/*">
  <xsl:copy>
    <xsl:apply-templates select="*">
      <xsl:sort select="@sno"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()">
               <xsl:copy>
                       <xsl:apply-templates select="@*|node()"/>
               </xsl:copy>
      </xsl:template>
__________________
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:
chandra_perni (May 11th, 2009)
 
Old May 11th, 2009, 09:15 AM
Authorized User
 
Join Date: May 2009
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi Martin yes I want to take the root element from the input element. This is fine but in this we will be printing the attributes also. But in my actual xsd there will be no attribute for the elements [i mean sno] so validation fails. I will be assigning it in my input xml only for sorting purpose. So i want my output xml to be

<emp>
<emp2>
<name>Vijay</name>
<country>Australia</country>
<company>C</company>
<dept>
<deptno>1</deptno>
<deptname>RND</deptname>
</dept>
</emp2>
...
</emp>
 
Old May 11th, 2009, 09:24 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you do not want to copy the 'sno' attributes then add a template
Code:
<xsl:template match="/*/*/@sno"/>
which suppresses copying those attributes.
__________________
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:
chandra_perni (May 11th, 2009)
 
Old May 11th, 2009, 09:32 AM
Authorized User
 
Join Date: May 2009
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thank you very much Martin I am able to acheive my desired output.

Last edited by chandra_perni; May 12th, 2009 at 12:07 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
Sequencing installation apondu General .NET 0 October 28th, 2006 01:49 AM
DTS Package, XML task. Read XML file and store it Victoria SQL Server DTS 0 July 24th, 2006 02:43 PM
XSLT - 2 level grouping & sequencing krayan001 XSLT 3 June 27th, 2005 01:23 PM
Beginner - Access '02 Autonumber sequencing logic? Mita Access 10 September 25th, 2003 03:46 PM





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