Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the namespace of a node in xslt?

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?

like image 637
aurel Avatar asked Dec 27 '11 19:12

aurel


People also ask

What is namespace in XSLT?

The namespace http://www.w3.org/1999/XSL/Transform is referred to as "the XSLT namespace". The prefix xsl is conventionally used to refer to this namespace (and is so used both within this document and within the XSLT specification), but it has no special status: any prefix may be used.

How do you add a namespace in XSLT?

Provided that you're using XSLT 2.0, all you have to do is add the xpath-default-namespace attribute on the document element of each stylesheet: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://example.com"> ...

What is Number () in XSLT?

XSLT format-number is defined to convert a number into a string value using the second argument's pattern. This is the type conversion formatting output. We use a different notation for numbers like comma for decimal formats and some points for thousands representation. To use all these, XSL uses format-number.

What is current () XSLT?

XSLT current() Function The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.


2 Answers

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.

like image 74
Michael Kay Avatar answered Sep 23 '22 22:09

Michael Kay


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

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

returns somenamespace:element

like image 26
Kirill Polishchuk Avatar answered Sep 24 '22 22:09

Kirill Polishchuk