Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's a mistake to think of XSLT as processing the literal text of the document. It's working on a model, a representation, one in which <code>&lt;page/&gt;</code> and <code>&lt;page&gt;&lt;/page&gt;</code> are equivalent. However, you made the following comment on a previous answer:</p> <blockquote> <p>Every <code>b</code> is line of text from a book. Every <code>&lt;page/&gt;</code> means that one page ended and next starts. So I want to have every "page" (<code>b</code>s between <code>&lt;page/&gt;</code>s) wrapped inside <code>&lt;p&gt;</code>. I hope this explains it a little bit.</p> </blockquote> <p>The following stylesheet uses a <code>key</code> to group <code>b</code> elements by their preceding <code>page</code>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/&gt; &lt;xsl:key name="byPage" match="b" use="generate-id(preceding-sibling::page[1])"/&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="a"&gt; &lt;xsl:copy&gt; &lt;!-- handle the first group of b's with no preceding page --&gt; &lt;p&gt;&lt;xsl:apply-templates select="key('byPage', '')"/&gt;&lt;/p&gt; &lt;xsl:apply-templates select="@*|page"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="page"&gt; &lt;p&gt;&lt;xsl:apply-templates select="key('byPage', generate-id())"/&gt;&lt;/p&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>The following output is produced:</p> <pre><code>&lt;a&gt; &lt;p&gt; &lt;b&gt;Text&lt;/b&gt; &lt;b&gt;Text&lt;/b&gt; &lt;b&gt;Text&lt;/b&gt; &lt;/p&gt; &lt;p&gt; &lt;b&gt;Text&lt;/b&gt; &lt;b&gt;Text&lt;/b&gt; &lt;/p&gt; &lt;p&gt; &lt;b&gt;Text&lt;/b&gt; &lt;b&gt;Text&lt;/b&gt; &lt;b&gt;Text&lt;/b&gt; &lt;b&gt;Text&lt;/b&gt; &lt;/p&gt; &lt;/a&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