使用XSLT生成XML上传UI时遇SAXParseException错误求助
问题修复方案
错误原因
- 命名空间不匹配:生成的XML里
<Apples>元素未添加目标schema要求的ns2命名空间(http://www.google.com/fruits),UI的XML校验规则要求ns2:FruitCatalog的子元素必须属于该命名空间,因此无命名空间的<Apples>被判定为无效内容。 - 节点选择路径错误:XSLT中的
xsl:for-each使用了/ns2:FruitCatalog/Apples/Apple路径,但源XML的根节点就是<Apples>,不存在ns2:FruitCatalog节点,导致这部分逻辑无法生成任何<Apple>元素,同时错误的<Apples>结构触发了schema校验失败。
修正后的XSLT代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:ns2="http://www.google.com/fruits" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <ns2:FruitCatalog> <xsl:variable name="schemaVersion" select="'26.0'"/> <!-- The variable schemaVersion can be used for further processing. --> <xsl:attribute name="schemaVersion"><xsl:value-of select="$schemaVersion"/></xsl:attribute> <ns2:Apples> <xsl:for-each select="/Apples/Apple"> <ns2:Apple> <xsl:attribute name="color"><xsl:value-of select="./@color"/></xsl:attribute> <xsl:attribute name="type"><xsl:value-of select="./@type"/></xsl:attribute> <ns2:Price><xsl:value-of select="./Price"/></ns2:Price> </ns2:Apple> </xsl:for-each> </ns2:Apples> </ns2:FruitCatalog> </xsl:template> </xsl:stylesheet>
修正要点
- 给
<Apples>、<Apple>、<Price>都加上ns2:前缀,确保所有子元素都属于目标schema指定的命名空间,符合ns2:FruitCatalog的结构要求。 - 将
xsl:for-each的选择路径改为/Apples/Apple,匹配源XML的实际结构,确保能正确遍历所有<Apple>节点并生成对应内容。
内容的提问来源于stack exchange,提问作者Codeblocks
相关产品推荐
相关产品推荐

