Re: [xsl] Namespace problem

Subject: Re: [xsl] Namespace problem
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Thu, 24 Sep 2009 17:35:59 +0200
Trevor Nicholls wrote:

As far as I know I am copying nodes where the namespace is in scope.

Simple input XML (test.xml):
----
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="../../xml_utils/hcdocs.xsd" index="N"
mark="Y">
<title>Software development</title>
<section>
<title id="Head">Q.A. Cycle</title>
<steps>
<step>Test application</step>
<step>Isolate problem</step>
<step>Submit bug report</step>
<step>Wait a while</step>
<step>When anything happens, go back to step 1</step>
</steps>
</section>
</document>
----

Drastically cut down XSL (test.xsl):
----
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xmlns:xmlns="http://www.w3.org/2001/XMLNamespace";
exclude-result-prefixes="xsi">
<xsl:output doctype-system="../../xml_utils/fmdocs.dtd" method="xml"
encoding="UTF-8" />

<xsl:template match="/">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="node()">
  <xsl:copy>
    <xsl:apply-templates select="@*" />
    <xsl:apply-templates select="node()" />
  </xsl:copy>
</xsl:template>

<xsl:template match="@*">
  <xsl:copy />
</xsl:template>

<!-- suppress default attributes -->
<xsl:template match="@mark|@formatted|@xml:space" />

<!-- Frame uses DTD not XSD -->
<!--xsl:template match="@*[namespace::*]" /-->
<xsl:template match="@xmlns:xsi" />
<xsl:template match="@xsi:noNamespaceSchemaLocation" />

</xsl:stylesheet>

As David suggested, you can write a template for element nodes where you construct a new element with the same name:


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
exclude-result-prefixes="xsi">
<xsl:output doctype-system="../../xml_utils/fmdocs.dtd" method="xml"
encoding="UTF-8" />

<xsl:template match="/">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="*">
  <xsl:element name="{name()}">
    <xsl:apply-templates select="@*" />
    <xsl:apply-templates select="node()" />
  </xsl:element>
</xsl:template>

<xsl:template match="@*">
  <xsl:copy />
</xsl:template>

<!-- suppress default attributes -->
<xsl:template match="@mark|@formatted|@xml:space" />

<!-- Frame uses DTD not XSD -->

<xsl:template match="@xsi:noNamespaceSchemaLocation" />

</xsl:stylesheet>

--

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread