Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to do this in XSLT1.0 is define a key that groups the non-pid elements under <strong>person-list</strong>, by the first most preceding <strong>pid</strong> element</p> <pre><code>&lt;xsl:key name="fields" match="person-list/*[not(self::pid)]" use="generate-id(preceding-sibling::pid[1])" /&gt; </code></pre> <p>Then, for the <strong>person-list</strong> element, you would select just the <strong>pid</strong> elements</p> <pre><code>&lt;xsl:apply-templates select="pid" /&gt; </code></pre> <p>And within the template that matches the <strong>pid</strong> you would create a <strong>person</strong> element, and output the other elements using the key:</p> <pre><code>&lt;xsl:apply-templates select="key('fields', generate-id())" /&gt; </code></pre> <p>Here is the full XSLT</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"/&gt; &lt;xsl:key name="fields" match="person-list/*[not(self::pid)]" use="generate-id(preceding-sibling::pid[1])" /&gt; &lt;xsl:template match="person-list"&gt; &lt;person-list&gt; &lt;xsl:apply-templates select="pid" /&gt; &lt;/person-list&gt; &lt;/xsl:template&gt; &lt;xsl:template match="pid"&gt; &lt;person&gt; &lt;xsl:copy-of select="." /&gt; &lt;xsl:apply-templates select="key('fields', generate-id())" /&gt; &lt;/person&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*|node()" name="identity"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>When applied to your sample XML, the following is output</p> <pre><code>&lt;person-list&gt; &lt;person&gt; &lt;pid&gt;100&lt;/pid&gt; &lt;pname&gt;Tom Jones&lt;/pname&gt; &lt;pdescription&gt;Some Text&lt;/pdescription&gt; &lt;/person&gt; &lt;person&gt; &lt;pid&gt;101&lt;/pid&gt; &lt;pname&gt;John Thomas&lt;/pname&gt; &lt;pdescription&gt;&lt;/pdescription&gt; &lt;/person&gt; &lt;/person-list&gt; </code></pre> <p>Note that with approach you could add further fields to you input document for each person without the need for amending the XSLT.</p> <p>Also note the use of the 'identity transform' to copy existing elements.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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