如何让XSLT适配带命名空间的SharePoint生成XML以提取数据?
你遇到的核心问题是原XML中的元素都属于特定命名空间,而你的XSLT没有声明和引用这些命名空间,导致XPath无法匹配到对应的节点。下面是修改后的XSLT,以及详细的解释:
修改后的完整XSLT代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:a="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/> <xsl:template match="a:feed"> <Applications> <xsl:for-each select="a:entry"> <App> <Code><xsl:value-of select="a:content/m:properties/d:Codigo"/></Code> <Name><xsl:value-of select="a:content/m:properties/d:App_x0020_name"/></Name> <Uri><xsl:value-of select="a:id"/></Uri> </App> </xsl:for-each> </Applications> </xsl:template> </xsl:stylesheet>
关键修改点说明
- 声明命名空间:在
<xsl:stylesheet>标签里添加了原XML中用到的三个核心命名空间:xmlns:a="http://www.w3.org/2005/Atom":对应原XML的默认命名空间(所有不带前缀的元素,比如feed、entry、id、content都属于这个空间)xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices":对应原XML中d:前缀的元素(比如d:Codigo、d:App_x0020_name)xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata":对应原XML中m:前缀的元素(比如m:properties)
- 修改XPath选择器:所有节点引用都加上了对应的命名空间前缀,比如:
- 匹配根节点从
match="feed"改为match="a:feed" - 遍历条目从
select="entry"改为select="a:entry" - 选择属性节点时,从
content/properties/Codigo改为a:content/m:properties/d:Codigo,明确指定每个节点所属的命名空间
- 匹配根节点从
这样修改后,XSLT就能正确识别原XML中带命名空间的节点,输出符合预期的简化版XML了。
内容的提问来源于stack exchange,提问作者mnieto
相关产品推荐
相关产品推荐

