在Java中,当我发送错误的XML(XSLT中没有提到标记的XML)时,在执行XSLT转换时不会引发TransformerException
在Java中,当使用XSLT文件将一个XML转换为另一个XML时,如果输入的是错误的XML(否定测试),则不会引发TransformerException 这里的坏XML是没有XSLT所期望的标记的XML 坏XML示例 123 XSLT快照(注意示例不是此处指定的标记)在Java中,当我发送错误的XML(XSLT中没有提到标记的XML)时,在执行XSLT转换时不会引发TransformerException,java,xml,xslt,Java,Xml,Xslt,在Java中,当使用XSLT文件将一个XML转换为另一个XML时,如果输入的是错误的XML(否定测试),则不会引发TransformerException 这里的坏XML是没有XSLT所期望的标记的XML 坏XML示例 123 XSLT快照(注意示例不是此处指定的标记) 即使没有匹配的标记,xsl转换也将通过 有几种选择 重构xsl以匹配/并查找所需/允许的标记,如果找不到,则使用抛出错误 为输入xml创建xml模式并在转换前验证 在XSLT 1.0或2.0中,添加模板规则 <xsl:t
即使没有匹配的标记,xsl转换也将通过 有几种选择
- 重构xsl以匹配/并查找所需/允许的标记,如果找不到,则使用
抛出错误 - 为输入xml创建xml模式并在转换前验证
<xsl:template match="*">
<xsl:message terminate="yes">Unknown element <xsl:value-of select="name()"/></xsl:message>
</xsl:template>
未知元素
匹配未被任何其他模板规则匹配的元素
在3.0中,添加
它没有失败的原因是XSLT定义了一个默认的模板规则来处理样式表中任何其他模板规则都不匹配的元素。您可以在XSLT转换之前实现SAX模式验证API 这里,我的SAX验证程序指定验证XML输入数据的模式: e、 g:格式错误的元素/属性=>
e、 g:xpath/tag格式错误=>
使用xpath default namespace=“samplereply”建议使用Saxon。如果是Saxon 9.8或更高版本,请尝试添加
,请参阅。XSLT 2.0是否有类似的解决方案?请参阅Michael Kay的答案。这很有效。但是,如果传入的XML缺少XSLT中定义的任何标记,我们也可能会出错。例如,```如果xml中没有订单id,我们会出错吗?一种方法是使用支持模式的XSLT转换,在XSD中单独定义xml有效性规则。在转换逻辑中,当它尝试引用源标记,而在xml中找不到源标记时,在这种情况下有可能抛出错误吗?tagnotfound“whatever”这里指的是我相信的源标记的名称。
<Sample>202008131228</Sample>
<?xml version="1.0" encoding="UTF-8"?>202008131228
Reader xmlReader = new StringReader(payloadStr);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
transformer.transform(new StreamSource(xmlReader), result);
outputResultStr = stringWriter.toString();
} catch (TransformerException trEx) {
}
<xsl:template match="*">
<xsl:message terminate="yes">Unknown element <xsl:value-of select="name()"/></xsl:message>
</xsl:template>
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
InputSourceHandle saxHandle = new InputSourceHandle();
saxHandle.setErrorHandler(new InputSourceHandle.DraconianErrorHandler());
String procSchema =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' " +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xsi:schemaLocation='http://www.w3.org/2001/XMLSchema XMLSchema.xsd'>" +
"<xs:element name='ProcedureDocument'>" +
"<xs:complexType mixed='true'>" +
"<xs:choice minOccurs='0' maxOccurs='unbounded'>" +
"<xs:element name='title'/>" +
"<xs:element name='confidentialityCode'/>" +
"<xs:element name='originalText'/>" +
"<xs:element name='statusCode'/>" +
"<xs:element name='methodCode'/>" +
"<xs:element name='reference'/>" +
"</xs:choice>" +
"<xs:attribute name=\"value\" type='xs:string' use='optional'/>" +
"</xs:complexType>" +
"</xs:element>" +
"</xs:schema>";
Schema schema = factory.newSchema(new StreamSource(new StringReader(procSchema)));
saxHandle.setDefaultWriteSchema(schema);
…….
//proceed XSLT transformation
<?xml version="1.0" encoding="utf-8"?>
</ProcedureDocument>
<title>Procedures Anesthesia</title>
<confidentialityCode>Confidential</confidentialityCode>
<originalText>Cardiac Surgery :: CABG, arterial, three</originalText>
<statusCode code="performed"/>
<methodCode>UNK</methodCode>
<reference value="#Procedure2"/>
</ProcedureDocument>
Error
Error reported by XML parser processing null: cvc-elt.1: Cannot find the declaration of
element 'title'.: cvc-elt.1: Cannot find the declaration of element 'title'.
Error
Error reported by XML parser processing null: The markup in the document preceding the
root element must be well-formed.: The markup in the document preceding the root element
must be well-formed.
ProcedureDocument
<title>Procedures Anesthesia</title>
Error
Error reported by XML parser processing null: Content is not allowed in prolog.: Content
is not allowed in prolog.