Re: [xsl] Namespace name from node?

Subject: Re: [xsl] Namespace name from node?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 6 Dec 2001 10:33:42 +0000
Hi Andrey,

> How can i figure out the namespace name for a given node?

It depends on what you mean by the namespace name. You can access the
namespace URI with the namespace-uri() function, for example, if you
had:

  <xhtml:element xmlns:xhtml="http://www.w3.org/1999/xhtml"; ... />

then you could get the namespace URI http://www.w3.org/1999/xhtml
with:

  <xsl:choose>
    <xsl:when test="namespace-uri() = 'http://www.w3.org/1999/xhtml'">
      ...
    </xsl:when>
    ...
  </xsl:choose>

This would work regardless of the prefix that you had in the the
source document, so it would evaluate as the same thing for:

  <element xmlns="http://www.w3.org/1999/xhtml"; ... />
  <foo:element xmlns:foo="http://www.w3.org/1999/xhtml"; ... />

Alternatively, if you know what namespace you want to test for, you
can declare it in your stylesheet:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xhtml="http://www.w3.org/1999/xhtml";>
...
</xsl:stylesheet>

And then test by seeing if the current node is a node in that
namespace using the self axis:

  <xsl:choose>
    <xsl:when test="self::xhtml:*">
      ...
    </xsl:when>
    ...
  </xsl:choose>

But you might have meant the namespace *prefix* by 'namespace name', in
which case you can use substring-before() to access the prefix:

  <xsl:choose>
    <xsl:when test="substring-before(name(), ':') = 'xhtml'">
      ...
    </xsl:when>
    ...
  </xsl:choose>

This will test as true for both:

  <xhtml:element xmlns:xhtml="http://www.w3.org/1999/xhtml"; ... />
  <xhtml:element xmlns:xhtml="http://www.rubbish.com"; ... />

And false for both:

  <element xmlns="http://www.w3.org/1999/xhtml"; ... />
  <foo:element xmlns:foo="http://www.w3.org/1999/xhtml"; ... />

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread