Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Here is a pure XSLT 1.0 solution</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&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="OBJECT//*[not(self::identity) and text()]"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="text()"/&gt; &lt;/xsl:copy&gt; &lt;xsl:apply-templates select="@* | node()[not(self::text())]"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="OBJECT//*[not(self::identity) and (not(text()))]"&gt; &lt;xsl:copy/&gt; &lt;xsl:apply-templates select="@*"/&gt; &lt;xsl:apply-templates select="node()"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*"&gt; &lt;xsl:element name="{name()}"&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;xsl:template match="identity"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the provided XML document:</strong></p> <pre><code>&lt;ROOT&gt; &lt;OBJECT&gt; &lt;identity&gt; &lt;id&gt;123&lt;/id&gt; &lt;/identity&gt; &lt;child2 attr = "aa"&gt;32&lt;/child2&gt; &lt;child3&gt; &lt;childOfChild3 att1="aaa" att2="bbb" att3="CCC"&gt;LN&lt;/childOfChild3&gt; &lt;/child3&gt; &lt;child4&gt; &lt;child5&gt; &lt;child6&gt;3ddf&lt;/child6&gt; &lt;child7&gt; &lt;childOfChild7 att31="RR"&gt;1231&lt;/childOfChild7&gt; &lt;/child7&gt; &lt;/child5&gt; &lt;/child4&gt; &lt;/OBJECT&gt; &lt;OBJECT&gt; &lt;identity&gt; &lt;id&gt;124&lt;/id&gt; &lt;/identity&gt; &lt;child2 attr = "bb"&gt;212&lt;/child2&gt; &lt;child3&gt; &lt;childOfChild3 att1="ee" att2="ccc" att3="EREA"&gt;OP&lt;/childOfChild3&gt; &lt;/child3&gt; &lt;child4&gt; &lt;child5&gt; &lt;child6&gt;213r&lt;/child6&gt; &lt;child7&gt; &lt;childOfChild7 att31="EE"&gt;1233&lt;/childOfChild7&gt; &lt;/child7&gt; &lt;/child5&gt; &lt;/child4&gt; &lt;/OBJECT&gt; &lt;/ROOT&gt; </code></pre> <p><strong>the wanted, correct result is produced:</strong></p> <pre><code>&lt;ROOT&gt; &lt;OBJECT&gt; &lt;id&gt;123&lt;/id&gt; &lt;child2&gt;32&lt;/child2&gt; &lt;attr&gt;aa&lt;/attr&gt; &lt;child3/&gt; &lt;childOfChild3&gt;LN&lt;/childOfChild3&gt; &lt;att1&gt;aaa&lt;/att1&gt; &lt;att2&gt;bbb&lt;/att2&gt; &lt;att3&gt;CCC&lt;/att3&gt; &lt;child4/&gt; &lt;child5/&gt; &lt;child6&gt;3ddf&lt;/child6&gt; &lt;child7/&gt; &lt;childOfChild7&gt;1231&lt;/childOfChild7&gt; &lt;att31&gt;RR&lt;/att31&gt; &lt;/OBJECT&gt; &lt;OBJECT&gt; &lt;id&gt;124&lt;/id&gt; &lt;child2&gt;212&lt;/child2&gt; &lt;attr&gt;bb&lt;/attr&gt; &lt;child3/&gt; &lt;childOfChild3&gt;OP&lt;/childOfChild3&gt; &lt;att1&gt;ee&lt;/att1&gt; &lt;att2&gt;ccc&lt;/att2&gt; &lt;att3&gt;EREA&lt;/att3&gt; &lt;child4/&gt; &lt;child5/&gt; &lt;child6&gt;213r&lt;/child6&gt; &lt;child7/&gt; &lt;childOfChild7&gt;1233&lt;/childOfChild7&gt; &lt;att31&gt;EE&lt;/att31&gt; &lt;/OBJECT&gt; &lt;/ROOT&gt; </code></pre>
 

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