Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between name() and local-name()?

Tags:

xml

xpath

I don't really understand the difference between the XPath functions name and local-name.

Could you give an example of a situation where they would differ?

Edit

Given this example:

<?xml version="1.0" ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">   <head></head> </html> 

I get the same result with these two queries: //*[local-name()="head"] and //*[name()="head"]. Why is that?

like image 219
troelskn Avatar asked Mar 17 '10 12:03

troelskn


People also ask

What is local name ()?

So applying the local-name() function to this node returns author . When a node has no expanded name, such as author , applying the local-name() function on this node returns the node name as is, i.e., author . If the node-set argument is omitted, it defaults to a node-set with the context node as its only member.

What is local name () xpath?

The local-name function returns a string representing the local name of the first node in a given node-set.

What is local name in XML?

The name of the current node with the prefix removed. For example, LocalName is book for the element <bk:book>.


1 Answers

With the XML being

<x:html xmlns:x="http://www.w3.org/1999/xhtml"/> 

the stylesheet

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="1.0">    <xsl:output indent="yes"/>    <xsl:template match="*">     <local-name><xsl:value-of select="local-name()"/></local-name>     <name><xsl:value-of select="name()"/></name>   </xsl:template>  </xsl:stylesheet> 

outputs

<local-name>html</local-name> <name>x:html</name> 

So the local-name() result is without any prefix, the name() result might include a prefix.

In your sample with a default namespace declaration no prefix is present, therefore name() and local-name() give the same result.

like image 133
Martin Honnen Avatar answered Nov 10 '22 17:11

Martin Honnen