Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you're looking for a <a href="http://www.dpawson.co.uk/xsl/sect2/N8367.html#d11679e58" rel="nofollow noreferrer">conditional deep-copy</a>.</p> <p>Here's the code in the link above rewritten for your situation:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xsl:stylesheet version="2.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;!-- nodes with Bullettext children --&gt; &lt;xsl:template match="*[Bullettext]"&gt; &lt;!-- for every child --&gt; &lt;xsl:copy&gt; &lt;xsl:for-each select="*"&gt; &lt;!-- if child is a Bullettext and it has a Bullettext before it, don't copy it (it has already been copied) --&gt; &lt;xsl:if test="not(local-name(.) = 'Bullettext' and local-name(./preceding-sibling::*[1]) = 'Bullettext')"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="local-name(.) = 'Bullettext'"&gt; &lt;!-- copy all Bullettext children adjacent to this one and each other --&gt; &lt;LIST&gt; &lt;xsl:call-template name="get-all-adjacent-siblings"&gt; &lt;xsl:with-param name="sibling-before" select="." /&gt; &lt;/xsl:call-template&gt; &lt;/LIST&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;!-- copy non-Bullettext child --&gt; &lt;xsl:apply-templates select="." /&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template name="get-all-adjacent-siblings"&gt; &lt;xsl:param name="sibling-before" /&gt; &lt;!-- return me --&gt; &lt;xsl:copy&gt; &lt;xsl:value-of select="$sibling-before" /&gt; &lt;/xsl:copy&gt; &lt;!-- return my adjacent Bullettext siblings below me --&gt; &lt;xsl:if test="local-name($sibling-before/following-sibling::*[1]) = 'Bullettext'"&gt; &lt;xsl:call-template name="get-all-adjacent-siblings"&gt; &lt;xsl:with-param name="sibling-before" select="$sibling-before/following-sibling::*[1]" /&gt; &lt;/xsl:call-template&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>The input I used was:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Articles&gt; &lt;Article&gt; &lt;Bullettext&gt;10,00 &lt;/Bullettext&gt; &lt;Bullettext&gt;8,00 &lt;/Bullettext&gt; &lt;/Article&gt; &lt;Article&gt; &lt;something&gt;some text&lt;/something&gt; &lt;/Article&gt; &lt;Article&gt; &lt;Corpsdetexte&gt;Bulgaria&lt;/Corpsdetexte&gt; &lt;deeper&gt; &lt;before&gt;dogs&lt;/before&gt; &lt;Bullettext&gt;15,0 &lt;/Bullettext&gt; &lt;Bullettext&gt;10,0 &lt;/Bullettext&gt; &lt;middle&gt;cats&lt;/middle&gt; &lt;Bullettext&gt;25,0 &lt;/Bullettext&gt; &lt;Bullettext&gt;20,0 &lt;/Bullettext&gt; &lt;after&gt;cows&lt;/after&gt; &lt;/deeper&gt; &lt;/Article&gt; &lt;/Articles&gt; </code></pre> <p>And it gave me:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Articles&gt; &lt;Article&gt; &lt;LIST&gt; &lt;Bullettext&gt;10,00 &lt;/Bullettext&gt; &lt;Bullettext&gt;8,00 &lt;/Bullettext&gt; &lt;/LIST&gt; &lt;/Article&gt; &lt;Article&gt; &lt;something&gt;some text&lt;/something&gt; &lt;/Article&gt; &lt;Article&gt; &lt;Corpsdetexte&gt;Bulgaria&lt;/Corpsdetexte&gt; &lt;deeper&gt; &lt;before&gt;dogs&lt;/before&gt; &lt;LIST&gt; &lt;Bullettext&gt;15,0 &lt;/Bullettext&gt; &lt;Bullettext&gt;10,0 &lt;/Bullettext&gt; &lt;/LIST&gt; &lt;middle&gt;cats&lt;/middle&gt; &lt;LIST&gt; &lt;Bullettext&gt;25,0 &lt;/Bullettext&gt; &lt;Bullettext&gt;20,0 &lt;/Bullettext&gt; &lt;/LIST&gt; &lt;after&gt;cows&lt;/after&gt; &lt;/deeper&gt; &lt;/Article&gt; &lt;/Articles&gt; </code></pre> <p>It's a bit messy if you want to do the other transformations like adding <code>&lt;p&gt;&lt;/p&gt;</code>s in the same stylesheet but if you do a two step transformation with two stylesheets, the first doing the conditional deep copy above and then the second doing your main transformation using the result of the first, you should be good to go.</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