Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>but what I want to get is child without namespace declaration from parent xmlns="default_ns".</p> </blockquote> <p><strong>This is not possible to achieve by only evaluating an XPath expression.</strong></p> <p><strong>In XML any element inherits all of its parent's namespace nodes</strong>, unless it redefines a particular namespace.</p> <p><strong>This means that <code>some_prefix:a</code> inherits the default namespace <code>"default_ns"</code></strong> from its parent (<code>subelement</code>), which itself inherits this same default namespace node from the top element <code>root</code>.</p> <p>XPath is a query language for XML documents. As such, it only helps select nodes, but <strong>the evaluation of an XPath expression never destroys, adds or alters nodes, including namespace nodes</strong>.</p> <p>Because of this, the default namespace node that belongs to <code>some_prefix:a</code> cannot be destroyed as result of the evaluation of your XPath expression -- thus this namespace node is shown when <code>some_prefix:a</code> is serialized to text.</p> <p><strong>Solution</strong>: Use your favorite PL that hosts XPath, to delete the unwanted namespace node.</p> <p><strong>For example, if the hosting language is XSLT</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:d="default_ns"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:apply-templates mode="delNS" select="/*/d:subelement/*[1]"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*" mode="delNS"&gt; &lt;xsl:element name="{name()}" namespace="{namespace-uri()}"&gt; &lt;xsl:copy-of select="namespace::*[name()]"/&gt; &lt;xsl:copy-of select="@*"/&gt; &lt;xsl:apply-templates mode="delNS" select="node()"/&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when this transformation is applied on the provided XML document</strong>:</p> <pre><code>&lt;root xmlns="default_ns"&gt; &lt;subelement&gt; &lt;!-- here we can have an element of any namespace --&gt; &lt;some_prefix:a xmlns:some_prefix="some_namespace"&gt; &lt;some_prefix:b/&gt; &lt;/some_prefix:a&gt; &lt;/subelement&gt; &lt;/root&gt; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>&lt;some_prefix:a xmlns:some_prefix="some_namespace"&gt; &lt;some_prefix:b/&gt; &lt;/some_prefix:a&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