Note that there are some explanatory texts on larger screens.

plurals
  1. POConsolidate xmlns attributes to the root element?
    text
    copied!<p>I'm converting a set of XML documents from one format, which doesn't include namespace prefixes, to another, which does. Everything is relatively straightforward, but it's a bit repetitive in the XMLNS output. Below is an example.</p> <h3>(Very Simple) Input XML</h3> <pre><code>&lt;?xml version="1.0"?&gt; &lt;a/&gt; </code></pre> <h3>XSLT</h3> <pre><code>&lt;!-- xmlns="http://www.w3.org/1999/xhtml" --&gt; &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:a="urn:data.test-a" xmlns:b="urn:data.test-b" xmlns:c="urn:data.test-c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:local.test schema/test.xsd" version="1.0"&gt; &lt;xsl:output method="xml" indent="yes" /&gt; &lt;xsl:template match="/a"&gt; &lt;xsl:element name="a:test-a"&gt; &lt;!-- attempted fix --&gt; &lt;xsl:copy-of select="namespace::*"/&gt; &lt;!-- is there a better way to get this in there? --&gt; &lt;xsl:attribute name="xsi:schemaLocation"&gt;urn:local.test schema/test.xsd&lt;/xsl:attribute&gt; &lt;xsl:element name="b:test-b"&gt; &lt;xsl:element name="c:test-c"&gt;content&lt;/xsl:element&gt; &lt;/xsl:element&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <h3>Output</h3> <pre><code>&lt;?xml version="1.0"?&gt; &lt;a:test-a xmlns:a="urn:data.test-a" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:local.test schema/test.xsd"&gt; &lt;b:test-b xmlns:b="urn:data.test-b"&gt; &lt;c:test-c xmlns:c="urn:data.test-c"&gt;content&lt;/c:test-c&gt; &lt;c:test-c xmlns:c="urn:data.test-c"&gt;content&lt;/c:test-c&gt; &lt;/b:test-b&gt; &lt;b:test-b xmlns:b="urn:data.test-b"&gt; &lt;c:test-c xmlns:c="urn:data.test-c"&gt;content&lt;/c:test-c&gt; &lt;c:test-c xmlns:c="urn:data.test-c"&gt;content&lt;/c:test-c&gt; &lt;/b:test-b&gt; &lt;/a:test-a&gt; </code></pre> <h3>Desired Output</h3> <pre><code>&lt;?xml version="1.0"?&gt; &lt;a:test-a xmlns:a="urn:data.test-a" xmlns:b="urn:data.test-b" xmlns:c="urn:data.test-c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:local.test schema/test.xsd"&gt; &lt;b:test-b&gt; &lt;c:test-c&gt;content&lt;/c:test-c&gt; &lt;c:test-c&gt;content&lt;/c:test-c&gt; &lt;/b:test-b&gt; &lt;b:test-b&gt; &lt;c:test-c&gt;content&lt;/c:test-c&gt; &lt;c:test-c&gt;content&lt;/c:test-c&gt; &lt;/b:test-b&gt; &lt;/a:test-a&gt; </code></pre> <p>Basically, I want to consolidate the namespace attributes into the root element. I've been doing some research and I thought I had this locked up using as seen <a href="http://lenzconsulting.com/namespaces-in-xslt/#xslt_1_0__copying_namespace_nodes_using_the_namespace_axis" rel="nofollow">here</a>. But, it's not having the desired effect; I assume I'm either using it incorrectly or am restricted by xsltproc's capabilities.</p> <p>Performing a second pass to clean up the XMLNS entries would be a fine solution as well.</p> <p>Also, in case it restricts a solution, I think my environment will be limited to XSLT 1.0.</p> <p>Thanks for any tips.</p> <p>PS. A smaller question is if there's a better way to get that schemaLocation attribute in the output, but that's minor.</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