Link to home
Start Free TrialLog in
Avatar of trudyhlittle
trudyhlittleFlag for United States of America

asked on

xslt copy xml document for each occurance of a repeating node

Hi, I am attempting to copy the contents of an entire XML document once for each occurrence of a repeating node (including it's children) within the same document using XSLT(version 1.0).  Each instance of the original document should contain only one of the repeating nodes (RepeatingNode).   Basically, I need to change the repeating nodes into a single occurrence and wrap them in a new root node.  The elements in the XML can be anything, however the name of the repeating node is always the same.  There are thousands of elements in the XML document, so I can't simply just build it.  The copy needs to be generic.   For example if I have the following XML, I need these results.

<?xml version="1.0" encoding="UTF-8"?>
<XMLTopNode attr1="1234" attr2="hithere" attr3="thanks">
      <Element1>
            <ChildOfElement1.1>data here</ChildOfElement1.1>
            <ChildOfElement1.2>data here2</ChildOfElement1.2>
            <ChildOfElement1.3>data here3</ChildOfElement1.3>
            <ChildOfElement1.4>
                  <ChildOfElement1.4.1>datahere4.1</ChildOfElement1.4.1>
            </ChildOfElement1.4>
      </Element1>
      <Element2>data here too</Element2>
      <Element3>data here three</Element3>
      <RepeatingNode>
            <ChildOfRepeatingNode1.1>1</ChildOfRepeatingNode1.1>
            <ChildOfRepeatingNode1.2>data here</ChildOfRepeatingNode1.2>
            <ChildOfRepeatingNode1.3> and data here</ChildOfRepeatingNode1.3>
            <ChildOfRepeatingNode1.4>
                  <ChildOfRepeatingNode1.4.1>data here too</ChildOfRepeatingNode1.4.1>
            </ChildOfRepeatingNode1.4>
      </RepeatingNode>
      <RepeatingNode>
            <ChildOfRepeatingNode1.1>2</ChildOfRepeatingNode1.1>
            <ChildOfRepeatingNode1.2>more data</ChildOfRepeatingNode1.2>
            <ChildOfRepeatingNode1.3>more data here</ChildOfRepeatingNode1.3>
            <ChildOfRepeatingNode1.4>
                  <ChildOfRepeatingNode1.4.1>and even more here</ChildOfRepeatingNode1.4.1>
            </ChildOfRepeatingNode1.4>
      </RepeatingNode>
      <RepeatingNode>
            <ChildOfRepeatingNode1.1>3</ChildOfRepeatingNode1.1>
            <ChildOfRepeatingNode1.2>some more</ChildOfRepeatingNode1.2>
            <ChildOfRepeatingNode1.3>still going</ChildOfRepeatingNode1.3>
            <ChildOfRepeatingNode1.4>
                  <ChildOfRepeatingNode1.4.1>last time</ChildOfRepeatingNode1.4.1>
            </ChildOfRepeatingNode1.4>
      </RepeatingNode>
</XMLTopNode>

 
I need:
<?xml version="1.0" encoding="UTF-8"?>
<NewTopNode>
      <XMLTopNode attr1="1234" attr2="hithere" attr3="thanks">
            <Element1>
                  <ChildOfElement1.1>data here</ChildOfElement1.1>
                  <ChildOfElement1.2>data here2</ChildOfElement1.2>
                  <ChildOfElement1.3>data here3</ChildOfElement1.3>
                  <ChildOfElement1.4>
                        <ChildOfElement1.4.1>datahere4.1</ChildOfElement1.4.1>
                  </ChildOfElement1.4>
            </Element1>
            <Element2>data here too</Element2>
            <Element3>data here three</Element3>
             <RepeatingNode>
                  <ChildOfRepeatingNode1.1>1</ChildOfRepeatingNode1.1>
                  <ChildOfRepeatingNode1.2>data here</ChildOfRepeatingNode1.2>
                  <ChildOfRepeatingNode1.3> and data here</ChildOfRepeatingNode1.3>
                  <ChildOfRepeatingNode1.4>
                        <ChildOfRepeatingNode1.4.1>data here too</ChildOfRepeatingNode1.4.1>
                  </ChildOfRepeatingNode1.4>
            </RepeatingNode>
      </XMLTopNode>
      <XMLTopNode attr1="1234" attr2="hithere" attr3="thanks">
            <Element1>
                  <ChildOfElement1.1>data here</ChildOfElement1.1>
                  <ChildOfElement1.2>data here2</ChildOfElement1.2>
                  <ChildOfElement1.3>data here3</ChildOfElement1.3>
                  <ChildOfElement1.4>
                        <ChildOfElement1.4.1>datahere4.1</ChildOfElement1.4.1>
                  </ChildOfElement1.4>
            </Element1>
            <Element2>data here too</Element2>
            <Element3>data here three</Element3>
            <RepeatingNode>
                  <ChildOfRepeatingNode1.1>2</ChildOfRepeatingNode1.1>
                  <ChildOfRepeatingNode1.2>more data</ChildOfRepeatingNode1.2>
                  <ChildOfRepeatingNode1.3>more data here</ChildOfRepeatingNode1.3>
                  <ChildOfRepeatingNode1.4>
                        <ChildOfRepeatingNode1.4.1>and even more here</ChildOfRepeatingNode1.4.1>
                  </ChildOfRepeatingNode1.4>
            </RepeatingNode>
      </XMLTopNode>
      <XMLTopNode attr1="1234" attr2="hithere" attr3="thanks">
            <Element1>
                  <ChildOfElement1.1>data here</ChildOfElement1.1>
                  <ChildOfElement1.2>data here2</ChildOfElement1.2>
                  <ChildOfElement1.3>data here3</ChildOfElement1.3>
                  <ChildOfElement1.4>
                        <ChildOfElement1.4.1>datahere4.1</ChildOfElement1.4.1>
                  </ChildOfElement1.4>
            </Element1>
            <Element2>data here too</Element2>
            <Element3>data here three</Element3>
            <RepeatingNode>
                  <ChildOfRepeatingNode1.1>3</ChildOfRepeatingNode1.1>
                  <ChildOfRepeatingNode1.2>some more</ChildOfRepeatingNode1.2>
                  <ChildOfRepeatingNode1.3>still going</ChildOfRepeatingNode1.3>
                  <ChildOfRepeatingNode1.4>
                        <ChildOfRepeatingNode1.4.1>last time</ChildOfRepeatingNode1.4.1>
                  </ChildOfRepeatingNode1.4>
            </RepeatingNode>
      </XMLTopNode>
</NewTopNode>


Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
Please accept https:#a40599443
It does exactly what the question asks
and at least it shows in a rather generic way how to have access to parent nodes from a child context
and how to let the mechanism of deep copying works