XSLT: How get rid of default namespace's prefixes in XPath? (xmlns=“…”)

此生再无相见时 提交于 2019-12-10 19:04:36

问题


I have a template:

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

This works.

<xsl:template match="//interfaces/interface[@name='management']/inet-address">
    ...
</xsl:template>

Why this doesn't work despite I have a default namespace set?


回答1:


<xsl:template match="//interfaces/interface[@name='management']/inet-address">
     ... 
</xsl:template> 

Why this doesn't work despite I have a default namespace set?

This is one of the most FAQ on any XSLT and/or XPath list.

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.

To quote the W3C XPath 1.0 specification:

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

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.



来源:https://stackoverflow.com/questions/9304592/xslt-how-get-rid-of-default-namespaces-prefixes-in-xpath-xmlns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!