我有第三方提供的xml文件,我无法更改输出。
XML的结构类似于以下内容:
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="ReportName">
<table>
<details attribute1="value"/>
</table>
</report>
由于xmlns无效,我无法正确解析文档。
我的xslt如下:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" />
<xsl:preserve-space elements="*" />
<!-- root template -->
<xsl:template match="/">
<applications>
<xsl:apply-templates select="Report/table/details"/>
</applications>
</xsl:template>
<!-- details template -->
<xsl:template match="details">
<application>
<name><xsl:value-of select="@attribute1"/></name>
</application>
</xsl:template>
有没有办法忽略xslt中的错误xmlns?或者我是否需要对其进行预处理以消除不良值?或者它只是解析器中的一个设置?我在.Net中使用XslCompiledTransform来解析它。
答案 0 :(得分:1)
命名空间将使用,而不是被忽略。由于您的XML使用默认命名空间,因此您需要在样式表中声明它,为其分配前缀,并在寻址源XML中的元素时使用该前缀。
修复<Report>
与</report>
不匹配后(XML区分大小写!),以下样式表应该适用于您:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="ReportName"
exclude-result-prefixes="ns1">
<xsl:output encoding="UTF-8" indent="yes" />
<!-- root template -->
<xsl:template match="/">
<applications>
<xsl:apply-templates select="ns1:Report/ns1:table/ns1:details"/>
</applications>
</xsl:template>
<!-- details template -->
<xsl:template match="ns1:details">
<application>
<name>
<xsl:value-of select="@attribute1"/>
</name>
</application>
</xsl:template>
</xsl:stylesheet>