Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>The problem is that the provided XSLT code has a default namespace.</strong></p> <p>Therefore, the <code>&lt;firstname&gt;</code> , <code>&lt;lastname&gt;</code> and <code>&lt;email&gt;</code> elements are in the xhtml namespace. But <code>email</code> is referenced without any prefix in:</p> <blockquote> <pre><code>exsl:node-set($person)/email </code></pre> </blockquote> <p>XPath considers all unprefixed names to be in "no namespace". It tries to find a child of <code>exsl:node-set($person)</code> called <code>email</code> that is in "no namespace" and this is unsuccessful, because its <code>email</code> child is in the xhtml namespace. Thus no <code>email</code> node is selected and output.</p> <p><strong>Solution</strong>:</p> <p>This transformation:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:x="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl x"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;html&gt; &lt;p&gt;Hello world&lt;/p&gt; &lt;xsl:variable name="person"&gt; &lt;firstname&gt;Foo&lt;/firstname&gt; &lt;lastname&gt;Bar&lt;/lastname&gt; &lt;email&gt;test@example.com&lt;/email&gt; &lt;/xsl:variable&gt; &lt;xsl:text&gt;&amp;#xA;&lt;/xsl:text&gt; &lt;xsl:value-of select="exsl:node-set($person)/x:email"/&gt; &lt;xsl:text&gt;&amp;#xA;&lt;/xsl:text&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on any XML document (not used), produces the wanted result</strong>:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:x="http://www.w3.org/1999/xhtml"&gt; &lt;p&gt;Hello world&lt;/p&gt; test@example.com &lt;/html&gt; </code></pre> <p><strong>Do note</strong>:</p> <ol> <li><p>The added namespace definition with prefix <code>x</code></p></li> <li><p>The changed <code>select</code> attribute of <code>&lt;xsl:value-of&gt;</code>: </p></li> </ol> <p><code>exsl:node-set($person)/x:email</code></p>
 

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