xsl-list
[Top] [All Lists]

Re: pulling a document with a null namespace into a namespace

2003-08-08 09:36:35

I've tried, I really have, but I can't come up with a solution to this.

Given some document with unknown elements (i.e., I can't enumerate the
elements inside the stylesheet for special processing) in a null namespace:

<random>
      <elements baz="foo">qux</elements>
</random>

Can I use XSLT to process this document and move it into a namespace:

<w:random xmlns:w="http://wibble.com/ns";>
      <w:elements baz="foo">qux</elements>
</w:random>

Thanks for reading!


the namespace is part of the element name, so as far as XSLT is
concerned this is like saying

Given some document with unknown elements (i.e., I can't enumerate the
elements inside the stylesheet for special processing) in a null namespace:
Can I use XSLT to process this document and prefix every element name
with "A"?

You code them pretty much the same way.

To do the first:

<xsl:template match="*">
 <xsl:element name="concat('A',local-name())">
  <xsl:apply-templates/>
 </xsl:element>
</xsl:template>

To do the second, you do


<xsl:template match="*">
 <xsl:element name="local-name()" namespace="http://wibble.com/ns";>
  <xsl:apply-templates/>
 </xsl:element>
</xsl:template>


which will produce 

<random xmlns="http://wibble.com/ns";>

If you need to specify the prefix you need a combination of both:



<xsl:template match="*">
 <xsl:element name="concat('w:',local-name())" namespace="http://wibble.com/ns";>
  <xsl:apply-templates/>
 </xsl:element>
</xsl:template>


In that case if your stylesheet element already has
xmlns:w="http://wibble.com/ns";
so that the w: prefix is in scope then you could simplify the last to



<xsl:template match="*">
 <xsl:element name="concat('w:',local-name())">
  <xsl:apply-templates/>
 </xsl:element>
</xsl:template>


as xsl:element will use the right namespace for the w: prefix in that
case.

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list