Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming that <code>&lt;cb&gt;</code> occur always on the same level (within <code>&lt;p&gt;</code>) this is more or less something like <a href="https://stackoverflow.com/q/16188341/2115381">"Create an hierarchical xml form an flat xml"</a> or perhaps look to <a href="https://stackoverflow.com/a/3836240/2115381">Kayessian method</a>. </p> <p>Try something like this (still open for improvements):</p> <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:strip-space elements="*" /&gt; &lt;xsl:output method="xml" indent="yes"/&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="p[cb]" &gt; &lt;!-- following siblings including next p[cb] --&gt; &lt;xsl:variable name="fs" select="following-sibling::*[ generate-id( preceding-sibling::p[cb][1] ) = generate-id(current()) ]" /&gt; &lt;div id="{cb/@n}"&gt; &lt;xsl:apply-templates select="cb" mode="start"/&gt; &lt;xsl:apply-templates select="$fs[position() != last()]" /&gt; &lt;xsl:apply-templates select="$fs[last()]" mode="stop"/&gt; &lt;/div&gt; &lt;/xsl:template&gt; &lt;xsl:template match="p[cb]" mode="stop"&gt; &lt;p&gt; &lt;xsl:copy-of select="cb/preceding-sibling::node()"/&gt; &lt;/p&gt; &lt;/xsl:template&gt; &lt;xsl:template match="cb[parent::p]" mode="start"&gt; &lt;p&gt; &lt;xsl:copy-of select="following-sibling::node()"/&gt; &lt;/p&gt; &lt;/xsl:template&gt; &lt;xsl:template match="div[p/cb]"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="p[cb][1]" mode="stop"/&gt; &lt;xsl:apply-templates select="p[cb]"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Which will generate the following output:</p> <pre><code> &lt;div&gt; &lt;p&gt;Hello my name is Bob &lt;/p&gt; &lt;div id="1"&gt; &lt;p&gt;and I live in a house&lt;/p&gt; &lt;p&gt;My name is Susan.&lt;/p&gt; &lt;p&gt;Where are you from?&lt;/p&gt; &lt;p&gt;I am from &lt;/p&gt; &lt;/div&gt; &lt;div id="2"&gt; &lt;p&gt;Chicago, Illinois&lt;/p&gt; &lt;p&gt;I also live in Chicago&lt;/p&gt; &lt;p&gt;But I wish I &lt;/p&gt; &lt;/div&gt; &lt;div id="3"&gt; &lt;p&gt;lived in New York&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; </code></pre>
    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.
    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