name() 和 local-name() 有什么区别? [英] What is the difference between name() and local-name()?
问题描述
我不太明白 XPath 函数之间的区别 name
和 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?
以这个例子为例:
<?xml version="1.0" ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head></head>
</html>
我用这两个查询得到相同的结果://*[local-name()="head"]
和 //*[name()="head"]代码>.这是为什么?
I get the same result with these two queries: //*[local-name()="head"]
and //*[name()="head"]
. Why is that?
推荐答案
随着 XML 的存在
<x:html xmlns:x="http://www.w3.org/1999/xhtml"/>
样式表
<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>
输出
<local-name>html</local-name>
<name>x:html</name>
所以 local-name()
结果没有任何前缀,name()
结果可能包含前缀.
So the local-name()
result is without any prefix, the name()
result might include a prefix.
在带有默认命名空间声明的示例中,不存在前缀,因此 name()
和 local-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() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!