Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>why did the former code outputs TEXT, why should I insist XSL to ignore all other text? is that the behavior of all XML parsers or only of my own</p> </blockquote> <p><strong>You are discovering one of the most fundamental XSLT features as specified in the Specification: the <a href="http://www.w3.org/TR/xslt#built-in-rule" rel="noreferrer">built-in templates of XSLT</a></strong>.</p> <p><strong>From <a href="http://www.w3.org/TR/xslt#built-in-rule" rel="noreferrer">the Spec</a></strong>:</p> <blockquote> <p>There is a built-in template rule to allow recursive processing to continue in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule:</p> <pre><code>&lt;xsl:template match="*|/"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; </code></pre> <p>There is also a built-in template rule for each mode, which allows recursive processing to continue in the same mode in the absence of a successful pattern match by an explicit template rule in the stylesheet. This template rule applies to both element nodes and the root node. The following shows the equivalent of the built-in template rule for mode m.</p> <pre><code>&lt;xsl:template match="*|/" mode="m"&gt; &lt;xsl:apply-templates mode="m"/&gt; &lt;/xsl:template&gt; </code></pre> <p>There is also a built-in template rule for text and attribute nodes that copies text through:</p> <pre><code>&lt;xsl:template match="text()|@*"&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:template&gt; </code></pre> <p>The built-in template rule for processing instructions and comments is to do nothing.</p> <pre><code>&lt;xsl:template match="processing-instruction()|comment()"/&gt; </code></pre> <p>The built-in template rule for namespace nodes is also to do nothing. There is no pattern that can match a namespace node; so, the built-in template rule is the only template rule that is applied for namespace nodes.</p> <p>The built-in template rules are treated as if they were imported implicitly before the stylesheet and so have lower import precedence than all other template rules. Thus, the author can override a built-in template rule by including an explicit template rule.</p> </blockquote> <p>So, the reported behavior is the result of the application of the built-in templates -- the 1st and 2nd of all three of them.</p> <p><strong>It is a good XSLT design pattern to override the built-in templates</strong> with your own that will issue an error message whenever called so that the programmer immediately knows his transformation is "leaking":</p> <p><strong>For example</strong>, if there is this XML document:</p> <pre><code>&lt;a&gt; &lt;b&gt; &lt;c&gt;Don't want to see this&lt;/c&gt; &lt;/b&gt; &lt;/a&gt; </code></pre> <p><strong>and it is processed with this transformation</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="a|b"&gt; &lt;xsl:copy&gt; &lt;xsl:attribute name="name"&gt; &lt;xsl:value-of select="name()"/&gt; &lt;/xsl:attribute&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>the result is</strong>:</p> <pre><code>&lt;a name="a"&gt; &lt;b name="b"&gt;Don't want to see this&lt;/b&gt; &lt;/a&gt; </code></pre> <p>and the programmer will be greatly confused how the unwanted text appeared.</p> <p><strong>However, just adding this <em><code>catch-all template</code></em> helps avoid any such confusion and catch errors immediately</strong>:</p> <pre><code> &lt;xsl:template match="*"&gt; &lt;xsl:message terminate="no"&gt; WARNING: Unmatched element: &lt;xsl:value-of select="name()"/&gt; &lt;/xsl:message&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; </code></pre> <p><strong>Now, besides the confusing output the programmer gets a warning that explains the problem immediately</strong>:</p> <pre><code> WARNING: Unmatched element: c </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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