xml name() 和 local-name() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2462248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:01:41  来源:igfitidea点击:

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

xmlxpath

提问by troelskn

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

我不太明白 XPath 函数namelocal-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?

这两个查询得到相同的结果://*[local-name()="head"]//*[name()="head"]。这是为什么?

回答by Martin Honnen

With the XML being

随着 XML 被

<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.

所以local-name()结果没有任何前缀,the name()结果可能包含一个前缀。

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

在具有默认命名空间声明的示例中,不存在前缀,因此name()local-name()给出相同的结果。