How to get node namespace in xslt?

I might be doing something stupid here, I'm sure there is an easier way ... I need to access the node namespace. The elements in my xml are as follows:

<somenamespace:element name="SomeName"> 

Then in my xslt I access these 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, this will not work, because there is no "somenamespace" namespace, even matching the pattern ...

Can someone guide me what I'm looking for?

+4
source share
3 answers

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

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

returns somenamespace:element

+1
source

It seems to me that you want to check if the node is in a non-zero namespace. The right way to do this is

 namespace-uri() != '' 

You should not look if the lexical name has a prefix or contains a colon, because if the node is in the namespace, then it can be written with or without a prefix, and these two forms are equivalent.

But I guess what your real, basic, requirement is.

+3
source

From the comment on the OP:

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

You can access the prefix of the current node name :

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

Another way :

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

The above creates either an empty string '' or a prefix followed by a ':' character.

To check if the prefix has the name of the current node :

 not(name() = local-name()) 
+1
source

Source: https://habr.com/ru/post/1388189/


All Articles