XSLT:如何摆脱XPath中默认名称空间的前缀? (xmlns ="...") [英] XSLT: How get rid of default namespace's prefixes in XPath? (xmlns="...")
问题描述
我有一个模板:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:jboss:domain:1.1"
xmlns:d="urn:jboss:domain:1.1"
>
...
<xsl:template match="//d:interfaces/d:interface[@name='management']/d:inet-address">
...
</xsl:template>
这有效.
<xsl:template match="//interfaces/interface[@name='management']/inet-address">
...
</xsl:template>
尽管设置了默认的命名空间,为什么这仍然不起作用?
Why this doesn't work despite I have a default namespace set?
推荐答案
<xsl:template match="//interfaces/interface[@name='management']/inet-address">
...
</xsl:template>
尽管设置了默认的命名空间,为什么这仍然不起作用?
Why this doesn't work despite I have a default namespace set?
这是任何XSLT和/或XPath列表中最常见的问题之一.
XPath会将任何不带前缀的名称视为属于没有名称空间" –不管是否存在定义的默认名称空间以及是否在范围内.
XPath treats any unprefixed name as belonging to "no namespace" -- regardless of the fact that there may be a default namespace defined and in scope.
引用 W3C XPath 1.0规范 :
To quote the W3C XPath 1.0 specification:
"节点测试中的QName使用 表达式上下文中的名称空间声明.这是一样的 起始标签和结束标签中元素类型名称的扩展方式 除了不使用通过xmlns声明的默认名称空间:if QName没有前缀,则名称空间URI为空"
"A QName in the node test is expanded into an expanded-name using the namespace declarations from the expression context. This is the same way expansion is done for element type names in start and end-tags except that the default namespace declared with xmlns is not used: if the QName does not have a prefix, then the namespace URI is null"
因此,上面的模板规则匹配在无名称空间"中的元素,但是XML文档的元素在"urn:jboss:domain:1.1"
命名空间中-因此,以上规则不匹配单个节点.
Therefore the template rule above is matching elements that are in "no namespace", but the elements of the XML document are in the "urn:jboss:domain:1.1"
namespace -- therefore not a single node is matched by the above rule.
这篇关于XSLT:如何摆脱XPath中默认名称空间的前缀? (xmlns ="...")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!