Thread Solved
Reply

Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

new to xslt

 
0
  #1
Oct 21st, 2008
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
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <parentNode>
  2. <childnode1 att1="1" att2="2"/>
  3. <childnode2></childnode2>
  4. <childnode1 att1="1" att2="2"/>
  5. <childnode21></childnode21>
  6. <childnode1 att1="1" att2="2"/>
  7. <childnode22></childnode22>
  8. <childnode1 att1="1" att2="2"/>
  9. <childnode23></childnode23>
  10. <childnode1 att1="1" att2="2"/>
  11. <childnode24></childnode24>
  12. <optionalNode></optionalNode>
  13. </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)
  1. <parentNode>
  2. <childnode1 att1="1" att2="2"/>
  3. <childnode2></childnode2>
  4. <childnode21></childnode21>
  5. <childnode22></childnode22>
  6. <childnode23></childnode23>
  7. <childnode24></childnode24>
  8. <optionalNode></optionalNode>
  9. </parentNode>
Is this even possible?
How should i go about it.
Thank you
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 13
Reputation: gravyboat is an unknown quantity at this point 
Solved Threads: 2
gravyboat gravyboat is offline Offline
Newbie Poster

Re: new to xslt

 
0
  #2
Oct 27th, 2008
Here is an XSLT that will do what you want.

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="xml" indent="yes"/>
  4.  
  5. <xsl:template match="/">
  6. <parentNode>
  7. <xsl:copy-of select=".//childnode1[1]"/>
  8.  
  9. <xsl:for-each select="./parentNode/child::*[not(name()='childnode1')]">
  10. <xsl:copy-of select="."/>
  11. </xsl:for-each>
  12. </parentNode>
  13. </xsl:template>
  14.  
  15. </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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 47
Reputation: nnobakht is an unknown quantity at this point 
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

Re: new to xslt

 
0
  #3
Oct 28th, 2008
Thank you. This really Helped.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the XML, XSLT and XPATH Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC