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 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
 
Old December 15th, 2009, 05:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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?
 
Old December 15th, 2009, 06:33 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old December 15th, 2009, 06:58 AM
Friend of Wrox
 
Join Date: Jun 2008
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





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





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