[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Namespaces issue


Subject: Re: [xsl] Namespaces issue
From: Mike Brown <mike@xxxxxxxx>
Date: Thu, 20 Jun 2002 14:14:24 -0600 (MDT)

Snow, Corey wrote:
> So I'd like to know if there's a way to force the output of a namespace
> declaration via an XSLT transform, even if there's no nodes in the input
> document that belong to that namespace. I'm using MSXML 4.0.

Normally, you just declare it in the stylesheet and create a literal result
element somewhere at or descended from the element where you declared the
namespace. It will inherit a namespace node, and a corresponding namespace
declaration will be output depending on where it's needed (so you don't really
have precise control in all circumstances). You normally also have the option
of using xsl:element and xsl:attribute, which both allow for a namespace
attribute if you don't want to specify a prefixed QName in the name attribute. 

However, since you're not actually outputting elements, just text that looks
like the serialized tags for elements, you will need to squeeze the xmlns
"attributes" into the middle of the text you're copying.

I suggest a choose-when where you just look to see that the string contains 
certain known prefixes, and if they're there, you precede the first instance 
with an xmlns.

Say string $s contains the pseudo-markup
'<someelement xlink:type="simple" xlink:href="test.html">data</someelement>'

Then, just as a quick example (untested):

<xsl:choose>
  <xsl:when test="contains($s,' xlink:')">
    <xsl:value-of select="concat(
                           substring-before($s,' xlink:'),
                           ' xmlns:xlink="http://www.w3.org/1999/xlink" xlink:',
                           substring-after($s,' xlink:')
                          )" disable-output-escaping="yes"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="$s" disable-output-escaping="yes"/>
  </xsl:otherwise>
</xsl:choose>

In the end you will probably get namespaces declared more than you need them
to be, but that won't hurt anything.

   - Mike
____________________________________________________________________________
  mike j. brown                   |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  resume: http://skew.org/~mike/resume/


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



Current Thread
Keywords