匹配在 xsl 模板中如何工作?

How does match works in xsl template?

我有一个这样的 xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                version="1.0">

    <xsl:template match="wsdl:definitions">
        <!-- code here -->
    </xsl:template>

</xsl:stylesheet>

在这个 xsl 中,这一行是做什么的:

<xsl:template match="wsdl:definitions">

根据我的理解,它将严格匹配 wsdl:definitions 标签,就像给定 xml 中的 <wsdl:definitions> (如果我在这里错了请纠正我)。

但那一刻我真的很困惑,我给这个输入 xml:

<xxx:definitions xmlns:xxx="http://schemas.xmlsoap.org/wsdl/" xmlns:xxxx1="http://www.w3.org/2006/05/addressing/wsdl"  >
</xxx:definitions>

它仍然匹配 <xxx:definitions> 并根据需要进行转换。鉴于在我的 xsl sheet 中我已经给它匹配 wsdl:definitions,这怎么可能?匹配与给定输入 xml 的 xmlns 有关吗?

名称空间前缀只是 URI 的名称。如果 URI 匹配,则名称空间相同。 xmlns 向 URI 添加前缀。单个文档中的同一个命名空间甚至可以有多个前缀。

因此您可以为名称空间使用任何您想要的前缀,并且 XSL 将始终正确匹配它们。这将有助于来自不同来源的文档不需要使用相同的前缀并且仍然可以被理解。