Note that there are some explanatory texts on larger screens.

plurals
  1. POFlatten nodes that have repeated child node using XSLT
    text
    copied!<p>I have a document of following structure (this is just an example to help me verbalize the problem), that I'm trying to flatten. By flattening I mean copying all the <code>&lt;Report_Entry&gt;</code> nodes with several <code>&lt;Event&gt;</code>s so that each <code>&lt;Report_Entry&gt;</code> node contained just a single <code>&lt;Event&gt;</code></p> <p>What I have:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;Report_Data&gt; &lt;Report_Entry&gt; &lt;ID&gt;1&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-06&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-10&lt;/End_Date&gt; &lt;/Event&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-10&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-15&lt;/End_Date&gt; &lt;/Event&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-15&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-20&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;Report_Entry&gt; &lt;ID&gt;2&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-20&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-25&lt;/End_Date&gt; &lt;/Event&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-25&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-30&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;Report_Entry&gt; &lt;ID&gt;3&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-30&lt;/Start_Date&gt; &lt;End_Date&gt;2011-10-05&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;/Report_Data&gt; </code></pre> <p>What I'm trying to get:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;Report_Data&gt; &lt;Report_Entry&gt; &lt;ID&gt;1&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-06&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-10&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;Report_Entry&gt; &lt;ID&gt;1&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-10&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-15&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;Report_Entry&gt; &lt;ID&gt;1&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-15&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-20&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;Report_Entry&gt; &lt;ID&gt;2&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-20&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-25&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;Report_Entry&gt; &lt;ID&gt;2&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-25&lt;/Start_Date&gt; &lt;End_Date&gt;2011-09-30&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;Report_Entry&gt; &lt;ID&gt;3&lt;/ID&gt; &lt;Event&gt; &lt;Start_Date&gt;2011-09-30&lt;/Start_Date&gt; &lt;End_Date&gt;2011-10-05&lt;/End_Date&gt; &lt;/Event&gt; &lt;/Report_Entry&gt; &lt;/Report_Data&gt; </code></pre> <p>Here is XSLT that I'm using:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="node() | @*"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node() | @*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Report_Entry"&gt; &lt;xsl:for-each select="Event"&gt; &lt;Report_Entry&gt; &lt;xsl:copy-of select="../*[not(self::Event)]"/&gt; &lt;xsl:copy-of select="."/&gt; &lt;/Report_Entry&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>It works, though I feel that there might be a better, faster and more universal solution. In particular, I don't like "hardcoding" <code>&lt;Report_Entry&gt;</code> since this way I wouldn't be able to copy its attributes (if any). Are there other ways/templates to deal with this problem? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload