<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="https://www.xlayer.co.za/forum/extern.php?action=feed&amp;tid=34&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[SXI Forum / XSLT Stylesheets - Remove namespaces]]></title>
		<link>https://www.xlayer.co.za/forum/viewtopic.php?id=34</link>
		<description><![CDATA[The most recent posts in XSLT Stylesheets - Remove namespaces.]]></description>
		<lastBuildDate>Tue, 30 Jun 2020 13:10:44 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[Re: XSLT Stylesheets - Remove namespaces]]></title>
			<link>https://www.xlayer.co.za/forum/viewtopic.php?pid=178#p178</link>
			<description><![CDATA[<p>When we convert a JSON object to an XML one there are a number of additional attributes that are included in order to show us what type of data in in the new XML element. E.g.&#160; <ins>type=&quot;string&quot;</ins>.</p><p>Often we do not want to send these additional attributes to our clients and so remove them with a stylesheet.&#160; However if we do not use the correct namespaces we end up with </p><div class="quotebox"><blockquote><div><p>xmlns=&quot;&quot;</p></div></blockquote></div><p> in the xml output.&#160; This is because the child elements are in a different namespace from the parent.</p><p>E.g. The &quot;OutputMemoryToLog&quot; looks as follows:</p><div class="codebox"><pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;XServiceBroker xmlns=&quot;http://www.sxi.co.za/XMLSchema&quot;&gt;
  &lt;payload&gt;
    &lt;Object xmlns=&quot;&quot;&gt;
      &lt;number type=&quot;string&quot;&gt;INC0000123&lt;/number&gt;
      &lt;short_description xmlns=&quot;&quot;&gt;ACME - Test 123&lt;/summary
      ...
     &lt;/Object&gt;
  &lt;/payload&gt;
&lt;/XServiceBroker&gt;</code></pre></div><p>When we apply the following stylesheet:</p><div class="codebox"><pre><code>&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output indent=&quot;yes&quot; method=&quot;xml&quot; encoding=&quot;utf-8&quot; omit-xml-declaration=&quot;yes&quot;/&gt;
    
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;data&gt;
            &lt;ref_num&gt;&lt;xsl:value-of select=&quot;//*[local-name()=&#039;number&#039;]&quot;/&gt;&lt;/ref_num&gt;
            &lt;summary&gt;&lt;xsl:value-of select=&quot;//*[local-name()=&#039;short_description&#039;]&quot;/&gt;&lt;/summary&gt;
            ...
        &lt;/data&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;</code></pre></div><p>However after we have transformed the xml it looks as follows:</p><div class="codebox"><pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;XServiceBroker xmlns=&quot;http://www.sxi.co.za/XMLSchema&quot;&gt;
  &lt;ref_num xmlns=&quot;&quot;&gt;INC0000123&lt;/ref_num&gt;
  &lt;summary xmlns=&quot;&quot;&gt;TCM - Test 123&lt;/summary&gt;
  ...
&lt;/XServiceBroker&gt;</code></pre></div><p>To ensure your output does not have xmlns-&quot;&quot; in each element you need to add the <strong>xmlns=&quot;http://www.sxi.co.za/XMLSchema&quot;</strong> declaration to your stylesheet.&#160; This will create the new elements in the same namespace as the parent in the &quot;OutputMemoryToLog&quot; object.</p><div class="codebox"><pre><code>&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns=&quot;http://www.sxi.co.za/XMLSchema&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    &lt;xsl:output indent=&quot;yes&quot; method=&quot;xml&quot; encoding=&quot;utf-8&quot; omit-xml-declaration=&quot;yes&quot;/&gt;
    
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;data&gt;
            &lt;ref_num&gt;&lt;xsl:value-of select=&quot;//*[local-name()=&#039;number&#039;]&quot;/&gt;&lt;/ref_num&gt;
            &lt;summary&gt;&lt;xsl:value-of select=&quot;//*[local-name()=&#039;short_description&#039;]&quot;/&gt;&lt;/summary&gt;
            ...
        &lt;/data&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;</code></pre></div><p>The result will now look correct:</p><div class="codebox"><pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;XServiceBroker xmlns=&quot;http://www.sxi.co.za/XMLSchema&quot;&gt;
  &lt;ref_num&gt;INC0000123&lt;/ref_num&gt;
  &lt;summary&gt;ACME - Test 123&lt;/summary&gt;
  ...
&lt;/XServiceBroker&gt;</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (SeanR)]]></author>
			<pubDate>Tue, 30 Jun 2020 13:10:44 +0000</pubDate>
			<guid>https://www.xlayer.co.za/forum/viewtopic.php?pid=178#p178</guid>
		</item>
		<item>
			<title><![CDATA[Re: XSLT Stylesheets - Remove namespaces]]></title>
			<link>https://www.xlayer.co.za/forum/viewtopic.php?pid=97#p97</link>
			<description><![CDATA[<div class="codebox"><pre class="vscroll"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    
    &lt;xsl:output indent=&quot;yes&quot; method=&quot;xml&quot; encoding=&quot;utf-8&quot; omit-xml-declaration=&quot;yes&quot;/&gt;
    
    &lt;!-- Stylesheet to remove all namespaces from a document --&gt;
    &lt;!-- NOTE: this will lead to attribute name clash, if an element contains
        two attributes with same local name but different namespace prefix --&gt;
    &lt;!-- Nodes that cannot have a namespace are copied as such --&gt;
    
    &lt;!-- template to copy elements --&gt;
    &lt;xsl:template match=&quot;*&quot;&gt;
        &lt;xsl:element name=&quot;{local-name()}&quot;&gt;
            &lt;xsl:apply-templates select=&quot;@* | node()&quot;/&gt;
        &lt;/xsl:element&gt;
    &lt;/xsl:template&gt;
    
    &lt;!-- template to copy attributes --&gt;
    &lt;xsl:template match=&quot;@*&quot;&gt;
        &lt;xsl:attribute name=&quot;{local-name()}&quot;&gt;
            &lt;xsl:value-of select=&quot;.&quot;/&gt;
        &lt;/xsl:attribute&gt;
    &lt;/xsl:template&gt;
    
    &lt;!-- template to copy the rest of the nodes --&gt;
    &lt;xsl:template match=&quot;comment() | text() | processing-instruction()&quot;&gt;
        &lt;xsl:copy/&gt;
    &lt;/xsl:template&gt;
    
&lt;/xsl:stylesheet&gt;</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (StephanB)]]></author>
			<pubDate>Sat, 24 Nov 2018 18:50:41 +0000</pubDate>
			<guid>https://www.xlayer.co.za/forum/viewtopic.php?pid=97#p97</guid>
		</item>
		<item>
			<title><![CDATA[XSLT Stylesheets - Remove namespaces]]></title>
			<link>https://www.xlayer.co.za/forum/viewtopic.php?pid=54#p54</link>
			<description><![CDATA[<p>This stylesheet removes namespaces from XML elements</p><div class="codebox"><pre><code>&lt;!-- Stylesheet to remove all namespaces from a document --&gt;
&lt;!-- NOTE: this will lead to attribute name clash, if an element contains
two attributes with same local name but different namespace prefix --&gt;
&lt;!-- Nodes that cannot have a namespace are copied as such --&gt;

&lt;!-- template to copy elements --&gt;

&lt;!-- template to copy attributes --&gt;

&lt;!-- template to copy the rest of the nodes --&gt;</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (SeanR)]]></author>
			<pubDate>Wed, 21 Nov 2018 16:24:40 +0000</pubDate>
			<guid>https://www.xlayer.co.za/forum/viewtopic.php?pid=54#p54</guid>
		</item>
	</channel>
</rss>
