Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
746 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...