Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

xslt copy xml document for each occurance of a repeating node

Posted on 2015-02-09
4
Medium Priority
?
26 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 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 143

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

722 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