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