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 April 13th, 2012, 05:36 AM
Registered User
 
Join Date: Apr 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to get rid of root node and child node...

Hi all,

I am not that skilled programming Stylesheets but I think that what I want to achieve is not that complicated. I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<SIT:Wrapping xmlns:SIT="http://www.sit.com/xml/sitwrapping">
<SIT:Header>
<SIT:Operation>OperationName</SIT:Operation>
</SIT:Header>
<BOD:Body xmlns:BOD="http://www.sit.com/xml/body">
<BOD:Node>
<BOD:Element1>ElementValue</BOD:Element1>
<BOD:Element2>ElementValue</BOD:Element2>
</BOD:Node>
</BOD:Body>
</SIT:Wrapping>

Basically I need to get rid of the wrapping node together with the header so the desired XML would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<BOD:Body xmlns:BOD="http://www.sit.com/xml/body">
<BOD:Node>
<BOD:Element1>ElementValue</BOD:Element1>
<BOD:Element2>ElementValue</BOD:Element2>
</BOD:Node>
</BOD:Body>

The only tricky part is that Body will not always be the same and its name will change depending on the type of the to-be-processed XML request. So i need the XSL to dinamically take care of that.

Hope you guys can help me.

Thanks all for your time,
 
Old April 13th, 2012, 05:41 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well you need to explain to use how exactly the node to be copied is determined if the name and/or namespace can change. Do you want to copy the second child element of the root element for instance?
__________________
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:
q1sevast (April 13th, 2012)
 
Old April 13th, 2012, 05:51 AM
Registered User
 
Join Date: Apr 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Thanks for your answer. For the second root child, the namespace will always be the same but the name can be different. So basically yes, I want to keep always the second rott child element (and its child hearchy).

Regards,
Jose
 
Old April 13th, 2012, 05:56 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You could try
Code:
<xsl:template match="/">
  <xsl:copy-of select="*/*[2]"/>
</xsl:template>
then although I think you might complain then that the namespace declarations on the root are copied through.
If you don't want them then you need more code e.g.
Code:
<xsl:template match="/">
  <xsl:apply-templates select="*/*[2]"/>
</xsl:template>

<xsl:template match="*">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
    <xsl:copy-of select="@*"/>
    </xsl:apply-templates/>
  </xsl:element>
</xsl:template>
or if you use XSLT 2.0 you could make use of its options not to copy namespaces.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 13th, 2012, 05:58 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

To add a sample of XSLT 2.0
Code:
<xsl:template match="/">
  <xsl:copy-of select="*/*[2]" copy-namespaces="no"/>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 13th, 2012, 08:19 AM
Registered User
 
Join Date: Apr 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks!!! Ill try out and let you know....
 
Old April 16th, 2012, 02:56 AM
Registered User
 
Join Date: Apr 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Worked like a charm using XSL 2.0...Thanks a lot for your help.

Regards,
Jose





Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving child nodes to previous occurrance of the root node LearnerP XSLT 2 April 9th, 2012 04:24 AM
Displaying data as a Parent Node with Left Node and Right Node Manoj Bisht Visual Basic 2008 Professionals 0 April 2nd, 2009 02:34 AM
Accessing a node bases on child node value musman0047 XSLT 1 February 27th, 2009 12:26 PM
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM





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