Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's one way to do it.</p> <p><strong>When this XSLT 2.0 solution:</strong></p> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="/*"&gt; &lt;t&gt; &lt;xsl:apply-templates select="parent[@class = 'header-gray'][following-sibling::*]"/&gt; &lt;/t&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*[@class = 'header-gray']"&gt; &lt;div class="{@class}"&gt; &lt;xsl:apply-templates select=" following-sibling::*[@class = 'header-lightgray'] intersect following-sibling::*[@class = 'header-gray'][1]/ preceding-sibling::*[@class = 'header-lightgray']"/&gt; &lt;/div&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*[@class = 'header-lightgray']"&gt; &lt;span class="{@class}"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>...is applied to the provided XML (wrapped in a top-level element to make the document well-formed):</strong></p> <pre><code>&lt;t&gt; &lt;parent class="header-gray"/&gt; &lt;parent class="header-lightgray"/&gt; &lt;parent class="header-lightgray"/&gt; &lt;parent class="header-lightgray"/&gt; &lt;parent class="header-gray"/&gt; &lt;parent class="header-lightgray"/&gt; &lt;parent class="header-lightgray"/&gt; &lt;parent class="header-lightgray"/&gt; &lt;parent class="header-gray"/&gt; &lt;/t&gt; </code></pre> <p><strong>...the desired result is produced:</strong></p> <pre><code>&lt;t&gt; &lt;div class="header-gray"&gt; &lt;span class="header-lightgray" /&gt; &lt;span class="header-lightgray" /&gt; &lt;span class="header-lightgray" /&gt; &lt;/div&gt; &lt;div class="header-gray"&gt; &lt;span class="header-lightgray" /&gt; &lt;span class="header-lightgray" /&gt; &lt;span class="header-lightgray" /&gt; &lt;/div&gt; &lt;/t&gt; </code></pre> <p>This solution makes use of <a href="http://www.w3.org/TR/xpath-functions/#func-intersect" rel="nofollow">XPath 2.0's intersect operation</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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