Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to move elements via XSLT
    primarykey
    data
    text
    <p>I have the following XML.</p> <pre><code> &lt;body&gt; &lt;p type="Heading 1"&gt;My Heading&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text... &lt;p type="Key Words"&gt;This is a keyword A&lt;/p&gt;&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text... &lt;p type="Key Words"&gt;This is a keyword B&lt;/p&gt;&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p type="Heading 1"&gt;My Next Heading&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... &lt;p type="Key Words"&gt;This is a keyword C&lt;/p&gt;This is paragraph text...&lt;/p&gt; &lt;p type="Heading 2"&gt;My Next Heading&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... &lt;p type="Key Words"&gt;This is a keyword D&lt;/p&gt; This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;/body&gt; </code></pre> <p>I would like to move all of the "Key Words" to just before the next heading as shown below:</p> <pre><code>&lt;body&gt; &lt;p type="Heading 1"&gt;My Heading&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p type="Key Words"&gt;This is a keyword A&lt;/p&gt; &lt;p type="Key Words"&gt;This is a keyword B&lt;/p&gt; &lt;p type="Heading 1"&gt;My Next Heading&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p type="Key Words"&gt;This is a keyword C&lt;/p&gt; &lt;p type="Heading 2"&gt;My Next Heading&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p&gt;This is paragraph Text... This is paragraph text...&lt;/p&gt; &lt;p type="Key Words"&gt;This is a keyword D&lt;/p&gt; &lt;/body&gt; </code></pre> <p>I have code that works, but it has serious performance problems because I am running this transform on documents that have 10's of thousands of words. Below is my current code.</p> <pre><code>&lt;!-- Place all keywords in section right before the next heading title. --&gt; &lt;xsl:template match="p[contains(@type,'Heading')]"&gt; &lt;xsl:variable name="headingCount" &gt; &lt;xsl:value-of select="count(preceding::p[contains(@type,'Heading')])"/&gt; &lt;/xsl:variable&gt; &lt;xsl:variable name="precedingKeyWordCount"&gt; &lt;xsl:value-of select="count(preceding::p[contains(@type,'Key Words') and count(preceding::p[contains(@type,'Heading')]) = $headingCount])"/&gt; &lt;/xsl:variable&gt; &lt;xsl:if test="$precedingKeyWordCount &gt; 0" &gt; &lt;p type="Key Words"&gt; &lt;xsl:apply-templates select="preceding::p[contains(@type,'Key Words') and count(preceding::p[contains(@type,'Heading')]) = $headingCount]" /&gt; &lt;/p&gt; &lt;/xsl:if&gt; &lt;!-- place original heading --&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; </code></pre> <p>Does anyone know a more efficient way to accomplish this?</p> <p>Thank you.</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