Note that there are some explanatory texts on larger screens.

plurals
  1. POMore performant XSLT -- Selective Inclusion in Output
    text
    copied!<p>I have created a stylesheet that is supposed to selectively copy the contents of an XML document so that I can strip out data that we do not need. I have provided 2 examples below and the stylesheet that we are currently using to do this. The stylesheet works, but I think there is probably a better way of doing it because in the current version I check for the same thing in two different locations (author='John Doe').</p> <p>The rules for including an xml element in the output is as follows:</p> <ul> <li>If there is a notepad element within notepads that has the author text equal to 'John Doe' then include the notepads element in the output</li> <li>If the notepad element has an author element with text equal to 'John Doe' then include all elements within the notepad element in the xml output.</li> </ul> <h1>Input Example #1</h1> <pre><code>&lt;transaction&gt; &lt;policy&gt; &lt;insco&gt;CC&lt;/insco&gt; &lt;notepads&gt; &lt;notepad&gt; &lt;author&gt;Andy&lt;/author&gt; &lt;notepad&gt; &lt;notepad&gt; &lt;author&gt;John Doe&lt;/author&gt; &lt;notepad&gt; &lt;notepad&gt; &lt;author&gt;Barney&lt;/author&gt; &lt;notepad&gt; &lt;/notepads&gt; &lt;/policy&gt; &lt;/transaction&gt; </code></pre> <h1>Expected result for Input #1</h1> <pre><code>&lt;transaction&gt; &lt;policy&gt; &lt;insco&gt;CC&lt;/insco&gt; &lt;notepads&gt; &lt;notepad&gt; &lt;author&gt;John Doe&lt;/author&gt; &lt;notepad&gt; &lt;/notepads&gt; &lt;/policy&gt; &lt;/transaction&gt; </code></pre> <h1>Input Example #2</h1> <pre><code>&lt;transaction&gt; &lt;policy&gt; &lt;insco&gt;CC&lt;/insco&gt; &lt;notepads&gt; &lt;notepad&gt; &lt;author&gt;Andy&lt;/author&gt; &lt;notepad&gt; &lt;/notepads&gt; &lt;/policy&gt; &lt;/transaction&gt; </code></pre> <h1>Expected Result for Input #2</h1> <pre><code>&lt;transaction&gt; &lt;policy&gt; &lt;insco&gt;CC&lt;/insco&gt; &lt;/policy&gt; &lt;/transaction&gt; </code></pre> <h1>Current Version of Stylesheet</h1> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn"&gt; &lt;xsl:template match="*"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="name()='notepads'"&gt; &lt;xsl:if test="/transaction/policy/insco='CC' and (notepad/author='John Doe')"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates /&gt; &lt;/xsl:copy&gt; &lt;/xsl:if&gt; &lt;/xsl:when&gt; &lt;xsl:when test="name()='notepad'"&gt; &lt;xsl:if test="author='John Doe'"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates /&gt; &lt;/xsl:copy&gt; &lt;/xsl:if&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates /&gt; &lt;/xsl:copy&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&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