Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 15th, 2009, 01:43 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to move XML element at end

Hi,

I am having XML elements like below:

<1>
<2>text 1</2>
<3>text 2</3>
<2>text 3</2>
<2>text 4</2>
<3>text 5</3></1>

I want to move the XML element <3> at end like below:

<1>
<2>text 1</2>
<2>text 3</2>
<2>text 4</2>
</1><3>text 2</3>
<3>text 5</3>

Can any one help me on how to achive this.

Thanks,
Krish
Reply With Quote
  #2 (permalink)  
Old December 15th, 2009, 05:13 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The example output above is not a valid XML document as it has no single root element. Is the <3> element meant to be moved outside the <1> element?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #3 (permalink)  
Old December 15th, 2009, 06:33 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

It helps to use well-formed XML examples: you can't use numbers as XML element names, which means I can't show you how to write path expressions to manipulate such elements, let alone testing that the examples work.

But basically you want a template rule like this:

Code:
<xsl:template match="one">
  <one>
     <xsl:copy-of select="two"/>
  </one>
  <xsl:copy-of select="three"/>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Last edited by mhkay; December 15th, 2009 at 06:34 AM.. Reason: correct element names
Reply With Quote
  #4 (permalink)  
Old December 15th, 2009, 06:58 AM
Friend of Wrox
Points: 1,306, Level: 14
Points: 1,306, Level: 14 Points: 1,306, Level: 14 Points: 1,306, Level: 14
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: Pondicherry, India
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

With XSLT 2.0 and the input xml as:
Code:
<root>
<test2>text 1</test2>
<test3>text 2</test3>
<test2>text 3</test2>
<test2>text 4</test2>
<test3>text 5</test3>
</root>
<p>I have given below two snippets. First will put <test3> as the last child in <root> and the next snippet will put <test3> out of <root></p>
Code:
<xsl:template match="root">
      <xsl:copy>
   <xsl:copy-of select="* except test3"/>
   <xsl:copy-of select="test3"/>
   </xsl:copy>
  </xsl:template>
Code:
  <xsl:template match="root">
      <xsl:copy>
   <xsl:copy-of select="* except test3"/>
   </xsl:copy>
   <xsl:copy-of select="test3"/>
  </xsl:template>
__________________
Rummy
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Move element Nagaraj XSLT 2 May 12th, 2009 03:29 AM
org.xml.sax.SAXParseException: Premature end ksskumar XSLT 1 March 19th, 2008 04:01 AM
Oracle back-end MS-Access 2003 client front-end Corey Access 2 February 16th, 2007 08:31 AM
Converting XML to XML (making element mandatory) boondocksaint20 XSLT 8 April 28th, 2006 10:54 AM
Move XML files from Windows to Website ocarroll General .NET 10 December 21st, 2004 06:30 AM



All times are GMT -4. The time now is 04:01 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.