| | |
new to xslt
Thread Solved
![]() |
•
•
Join Date: Nov 2006
Posts: 47
Reputation:
Solved Threads: 1
Hi guys,
Im brand new to doing tansformations and cannot seem to wrap my head around it.
I have a Xml File which looks something like
The childnode labled ChildNode1 is repeated several times (the values are identical) and what i am trying to do is transform this xml into a new xml which only has one childnode1
and teh rest are "deleted" or ignored during the transformation.Everything else must stay the same and the position of the childnode needs to be the first position it is found in.
I know the name of the childNode1 before hand.
Is this even possible?
How should i go about it.
Thank you
Im brand new to doing tansformations and cannot seem to wrap my head around it.
I have a Xml File which looks something like
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<parentNode> <childnode1 att1="1" att2="2"/> <childnode2></childnode2> <childnode1 att1="1" att2="2"/> <childnode21></childnode21> <childnode1 att1="1" att2="2"/> <childnode22></childnode22> <childnode1 att1="1" att2="2"/> <childnode23></childnode23> <childnode1 att1="1" att2="2"/> <childnode24></childnode24> <optionalNode></optionalNode> </parentNode>
The childnode labled ChildNode1 is repeated several times (the values are identical) and what i am trying to do is transform this xml into a new xml which only has one childnode1
and teh rest are "deleted" or ignored during the transformation.Everything else must stay the same and the position of the childnode needs to be the first position it is found in.
I know the name of the childNode1 before hand.
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<parentNode> <childnode1 att1="1" att2="2"/> <childnode2></childnode2> <childnode21></childnode21> <childnode22></childnode22> <childnode23></childnode23> <childnode24></childnode24> <optionalNode></optionalNode> </parentNode>
How should i go about it.
Thank you
•
•
Join Date: Jun 2008
Posts: 13
Reputation:
Solved Threads: 2
Here is an XSLT that will do what you want.
At the root we create the <parentNode> that will contain all children. Since we only want one childnode1, we copy the first by using the "[1]" position argument. We then loop through all of the children of parentNode, copying only those that are not named childnode1.
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <parentNode> <xsl:copy-of select=".//childnode1[1]"/> <xsl:for-each select="./parentNode/child::*[not(name()='childnode1')]"> <xsl:copy-of select="."/> </xsl:for-each> </parentNode> </xsl:template> </xsl:stylesheet>
At the root we create the <parentNode> that will contain all children. Since we only want one childnode1, we copy the first by using the "[1]" position argument. We then loop through all of the children of parentNode, copying only those that are not named childnode1.
![]() |
Similar Threads
- ASP.NET - session objects Problem (ASP.NET)
- Error loading stylesheet: Parsing an XSLT stylesheet failed (RSS, Web Services and SOAP)
- XSLT - inserting new elements (XML, XSLT and XPATH)
- How to transform XML to XSLT (HTML and CSS)
- XSLT weird problem... [urgent] (RSS, Web Services and SOAP)
- XSLT code doesn't seem to work!! (XML, XSLT and XPATH)
- Transforming XML with XSLT (HTML and CSS)
Other Threads in the XML, XSLT and XPATH Forum
- Previous Thread: Newbi error
- Next Thread: What is Data Base Synchronization..?
| Thread Tools | Search this Thread |
api blogger blogging code delete development dynamiccreationofnvariablesinxslt error firstthreecharacterofastringrequired flipbook gdata google html include java link linspire linux microsoft news node openoffice overwrite precedence programming rss standards swf template transform variable w3c web xml xmlnotloading xmlonserver xsl xslt





