Altova Mailing List Archives


Re: renaming an element -- how to copy namespace nodes?

From: Ben Edgington <usenet@-------.--->
To: NULL
Date: 10/12/2004 9:41:00 AM
adamba@g... (Adam Barr) writes:
> I have a tag foo that I want to copy unchanged when it is a subtag of
> bar, so I have a template (x is the namespace for the document):
> 
> <xsl:template match="x:bar/x:foo">
>   <xsl:copy>
>     <xsl:apply-templates/>
>   </xsl:copy>
> </xsl:template>
> 
> BUT, I discovered that someone has been mis-speling foo as foop in the
> source files. So I add another template to fix this up:
> 
> <xsl:template match="x:bar/x:foop">
>   <foo>
>     <xsl:apply-templates/>
>   </foo>
> </xsl:template>
> 
> The problem is that the output now has xmlns attributes added to the
> foo tag that I output. If the input spells "foo" correctly, matching
> the first template above, this doesn't happen.
<snip>

I think you need to make the default output namespace the same as the
x namespace here with a 'xmlns="..."' declaration in the
<xsl:stylesheet/> element.  This will result in the new <foo/>
elements being created in the same output namespace, and thus they
*shouldn't* have an extra namespace declaration.

So, with this XML

----
<test xmlns="http://example.com/foo">
  <bar>
    <foo>Wibble</foo>
    <foop>Wobble</foop>
  </bar>
</test>
----

and this XSLT

----
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="http://example.com/foo"
                xmlns="http://example.com/foo"
                >

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

<xsl:template match="x:bar/x:foo">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="x:bar/x:foop">
  <foo>
    <xsl:apply-templates/>
  </foo>
</xsl:template>

</xsl:stylesheet>
----

I get

----
<output xmlns="http://example.com/foo" xmlns:x="http://example.com/foo">
    <foo>Wibble</foo>
    <foo>Wobble</foo>
</output>
----

Fiddle around with the exclude-result-prefixes attribute if you want
to omit the redundant xmlns:x declaration.

-- 
Ben Edgington
  Mail to the address above is discarded.
  Mail to ben at that address might be read.
http://www.edginet.org/

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.