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
Solved

xslt copy xml document for each occurance of a repeating node

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

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 …
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, …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

792 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