6

I'm propably doing something stupid here, I bet there is an easier way... I need to access namespace of a node. Elements in my xml looks for example like this:

<somenamespace:element name="SomeName">

Then in my xslt I access this elements with:

 <xsl:template  match="*[local-name()='element']">
    <xsl:variable name="nodename">
      <xsl:value-of select="local-name(current())"/>
    </xsl:variable>

<xsl:choose>
  <xsl:when test="contains($nodename,':')">

Well, of course it doesn't work, because there is no "somenamespace" namespace even in template match...

Can anyone guide me, what am I looking for?

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
aurel
  • 1,117
  • 2
  • 11
  • 24
  • You need to tell us what you are looking for and what is not working. – kosa Dec 27 '11 at 19:32
  • Where did you find `local-namespace` function? – Kirill Polishchuk Dec 27 '11 at 19:35
  • Ow gosh, sory, it's a mistake. Corrected - I meant local-name. I'm looking for a way to access the "somenamespace" prefix. It's not working, because local-name returns only the "element" part. – aurel Dec 27 '11 at 19:38

3 Answers3

5

It looks to me as if you want to test whether the node is in a non-null namespace. The correct way to do that is

namespace-uri() != ''

You shouldn't be looking at whether the lexical name has a prefix or contains a colon, because if the node is in a namespace then it can be written either with a prefix or without, and the two forms are equivalent.

But I'm guessing as to what your real, underlying, requirement is.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
3

You are looking for name function, e..g.:

<xsl:value-of select="name()"/>

returns somenamespace:element

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
3

From OP's comment:

I'm looking for a way to access the "somenamespace" prefix.

You can access the prefix of the current node name by:

substring-before(name(), ':")

Another way:

substring-before(name(), local-name())

The above produces either the empty string '' or the prefix, followed by the ':' character.

To check if the name of the current node is prefixed:

not(name() = local-name())
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431