Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Adding unused namespace in output using xslt.

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 10/7/2008 2:47:00 PM

Jan Obrestad wrote:

> I tried adding these namespaces to the xsl:styleshhet element, but they 
> did not appear in the output xml.
> Do you have any idea how to achieve this?

Here is an example that declares the namespaces on the xsl:stylesheet 
element and then copies them to the result tree for the newly created 
root element:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns="http://www.e2b.no/XMLSchema"
                 xmlns:e2bCard="http://www.e2b.no/XMLSchema/Card"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <xsl:template match="*">
     <xsl:element name="{local-name()}">
       <xsl:apply-templates select="@* | node()"/>
     </xsl:element>
   </xsl:template>
   <xsl:template match="/*">
     <xsl:element name="{local-name()}">
       <xsl:copy-of 
select="document('')/xsl:stylesheet/namespace::*[name() != 'xsl']"/>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:element>
   </xsl:template>
   <xsl:template match="@*">
     <xsl:attribute name="{local-name()}">
       <xsl:value-of select="."/>
     </xsl:attribute>
   </xsl:template>
</xsl:stylesheet>

When you run that against

<?xml version="1.0" encoding="utf-8" ?>
<pf1:root xmlns:pf1="http://example.com/2008/ns1">
   <pf1:foo>
     <pf1:bar>baz</pf1:bar>
   </pf1:foo>
</pf1:root>

then Visual Studio 2005 creates the following result document

<?xml version="1.0" encoding="utf-8"?><root 
xmlns="http://www.e2b.no/XMLSchema" 
xmlns:e2bCard="http://www.e2b.no/XMLSchema/Card" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <foo>
     <bar>baz</bar>
   </foo>
</root>

I don't understand why you would want
   xsi:noNamespaceSchemaLocation="e2b_Invoice_Interchange_v3p0.xsd"
in the result as your result has a namespace so providing a 
noNamespaceSchemaLocation does not make any sense to me. Nevertheless if 
you really need it then simply add that attribute to the root with e.g.

   <xsl:template match="/*">
     <xsl:element name="{local-name()}">
       <xsl:copy-of 
select="document('')/xsl:stylesheet/namespace::*[name() != 'xsl']"/>
       <xsl:attribute 
name="xsi:noNamespaceSchemaLocation">e2b_Invoice_Interchange_v3p0.xsd</xsl:attribute>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:element>
   </xsl:template>

The other two templates remain unchanged.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent