943,725 Members | Top Members by Rank

Ad:
Oct 21st, 2008
0

new to xslt

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
nnobakht is offline Offline
47 posts
since Nov 2006
Oct 27th, 2008
0

Re: new to xslt

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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
gravyboat is offline Offline
14 posts
since Jun 2008
Oct 28th, 2008
0

Re: new to xslt

Thank you. This really Helped.
Reputation Points: 10
Solved Threads: 1
Light Poster
nnobakht is offline Offline
47 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: Newbi error
Next Thread in XML, XSLT and XPATH Forum Timeline: What is Data Base Synchronization..?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC