XSLT无法匹配带命名空间的XML根元素问题求助
解决XSLT无法匹配带命名空间的根元素问题
你的问题核心是XML元素带有命名空间,但XSLT中未进行对应处理,导致直接用元素名匹配失败。
看你的XML文档,根元素PartnerMasterDataSet声明了命名空间xmlns="http://tempuri.org/PartnerMasterDataSet.xsd"——这意味着该元素及其所有子元素(除非单独声明其他命名空间)都属于这个命名空间。而你当前的XSLT中直接使用match="/PartnerMasterDataSet",实际是在匹配没有任何命名空间的PartnerMasterDataSet元素,自然找不到匹配项。
解决方案
在XSLT的<xsl:stylesheet>节点中声明该命名空间,并绑定一个自定义前缀(比如pmds),之后所有匹配该命名空间下元素的地方,都要带上这个前缀:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pmds="http://tempuri.org/PartnerMasterDataSet.xsd"> <xsl:output indent="yes" /> <xsl:template match="/pmds:PartnerMasterDataSet"> <table> <xsl:apply-templates select="pmds:Partner" /> </table> </xsl:template> <!-- 如果需要处理Partner下的子元素,也要用前缀,比如: --> <xsl:template match="pmds:Partner"> <tr> <td><xsl:value-of select="pmds:Id" /></td> <!-- 其他元素同理 --> </tr> </xsl:template> </xsl:stylesheet>
补充说明
match="/"能成功匹配,是因为/代表的是XML文档节点,它不属于任何命名空间,所以不受命名空间规则影响。- 命名空间的前缀可以自定义(比如不用
pmds,换成data也可以),只要XSLT中声明的命名空间URI和XML中的完全一致即可。
内容的提问来源于stack exchange,提问作者U. Windl
相关产品推荐
相关产品推荐

