Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get nodes in this case
    primarykey
    data
    text
    <p>I am using xslt 2.0, I have following sample xml, I want to get all "header-lightgray" nodes between the two "header-gray" nodes, the problem here is: I don't know how many "header-lightgray" there. so I can not use position() function. and xsl for each don't support break.</p> <pre><code>&lt;t&gt; &lt;parent class="header-gray"&gt;&lt;/parent&gt; &lt;parent class="header-lightgray"&gt;&lt;/parent&gt; &lt;parent class="header-lightgray"&gt;&lt;/parent&gt; &lt;parent class="header-lightgray"&gt;&lt;/parent&gt; ... &lt;parent class="header-gray"&gt;&lt;/parent&gt; &lt;parent class="header-lightgray"&gt;&lt;/parent&gt; &lt;parent class="header-lightgray"&gt;&lt;/parent&gt; &lt;parent class="header-lightgray"&gt;&lt;/parent&gt; &lt;parent class="header-gray"&gt;&lt;/parent&gt; &lt;/t&gt; </code></pre> <p>I want go through them and replace all nodes, header-gray with div and header-lightgray with span. the output expected is:</p> <pre><code>&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; </code></pre> <p>So how to do it?</p> <p>Any help is appreciated.</p> <p><strong>Solution I'd like to share according to Jan Vlcinsky's answer:</strong></p> <pre><code>&lt;xsl:for-each select="/t/parent[@class='header-gray']"&gt; &lt;xsl:variable name="ns1" select="current()/following-sibling::parent"/&gt; &lt;xsl:variable name="ns2" select="current()/following-sibling::parent[@class='header-gray'][1]/preceding-sibling::parent"/&gt; &lt;div class="header-gray"&gt; &lt;xsl:for-each select="$ns1[count(.| $ns2)=count($ns2)]"&gt; &lt;span class="header-lightgray"/&gt; &lt;/xsl:for-each&gt; &lt;/div&gt; &lt;/xsl:for-each&gt; </code></pre> <p>I don't verify above code, but it's the same logic in my real code. I didn't use xslt 2.0 way because I found that lxml 2.3 I am using does not support xslt 2. </p>
    singulars
    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.
 

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