Solved

xslt copy xml document for each occurance of a repeating node

Posted on 2015-02-09
4
10 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
modifying sharepoint search results 13 30
Unmarshalling Xml using JAXB 1 42
Problem to DocumentFormat 8 67
C# SQL BULK INSERT CLASS 5 35
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now