Solved

xslt copy xml document for each occurance of a repeating node

Posted on 2015-02-09
4
13 Views
Last Modified: 2016-06-08
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.
0
Comment
Question by:trudyhlittle
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40599443
Like this?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:template match="/*">
        <NewTopNode>
            <xsl:apply-templates/>
        </NewTopNode>
    </xsl:template>
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
 
    <xsl:template match="*[RepeatingNode]/*[not(self::RepeatingNode)]"/>

    <xsl:template match="RepeatingNode">
        <xsl:element name="{name(parent::*)}">
            <xsl:copy-of select="parent::*/@*"/>
            <xsl:copy-of select="preceding-sibling::node()[not(self::RepeatingNode)]"/>
            <xsl:copy-of select="."></xsl:copy-of>
            <xsl:copy-of select="following-sibling::node()[not(self::RepeatingNode)]"/>
        </xsl:element>
    </xsl:template>
    
    
    
</xsl:stylesheet>

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41644752
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41644753
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
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question