英文:
In Java , no TransformerException is thrown while performing XSLT transformation when I send a bad XML ( XML with no tags mentioned in XSLT)
问题
In Java,在使用XSLT文件将一个XML转换为另一个XML时,如果输入是一个坏的XML(负面测试),不会抛出TransformerException异常。
坏的XML是指一个XML没有XSLT文件期望的标签。
坏XML示例:
<Sample>123</Sample>
XSLT快照(请注意,Sample在此处未在任何地方指定为标签):
*<xsl:template match="/SammpleReply" xpath-default-namespace="SammpleReply" exclude-result-prefixes="#all">
...
*</RadialReply>*
输入:
<Sample>202008131228</Sample>
输出:
<?xml version="1.0" encoding="UTF-8"?>202008131228
期望结果: 如果传入的XML在XSLT中没有定义任何XML元素,应该抛出异常(但实际上没有抛出)。想要知道原因,并希望在这种情况下代码抛出异常。以下是可能实现这一目标的代码逻辑:
代码只加载XSLT文件并进行转换,不进入异常块。它成功进行了转换,而outputResultStr是上面的输出。
Reader xmlReader = new StringReader(payloadStr);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
try {
transformer.transform(new StreamSource(xmlReader), result);
outputResultStr = stringWriter.toString();
} catch (TransformerException trEx) {
// 处理异常的代码逻辑
}
如果要使代码在没有XSLT定义的XML元素时抛出异常,可以在catch块中添加适当的逻辑来检查输出是否为空或无效,然后抛出自定义异常或其他异常类型。
英文:
In Java , while transforming one XML into another XML using an XSLT file , when given a bad XML as input ( negative testing) no TransformerException is thrown.
Bad XML here is an XML with no tags which XSLT expects
Example of Bad XML
<Sample>123</Sample>
XSLT snapshot ( Note Sample is not a tag specified anywhere here )
*<xsl:template match="/SammpleReply" xpath-default-namespace="SammpleReply" exclude-result-prefixes="#all">
<RadialReply xmlns="http://SammpleReply.com">
<xsl:choose>
<xsl:when test="PaymentContextBase">
<PaymentContextBase>
<OrderId><xsl:value-of select="RadialReply/Radial:RadialReplySessionId"/></OrderId>
</PaymentContextBase>
<TenderType><xsl:value-of select="RadialReply/Radial:TenderType"/></TenderType>
</xsl:when>
<xsl:otherwise>
<RadialReplyContext>
<OrderId><xsl:value-of select="RadialContext/payment:RadialUniqueId"/></OrderId>
<xsl:element name="RadialUniqueId" >
<xsl:attribute name="isToken"><xsl:value-of select="RadialContext/payment:PaymentAccountUniqueId/@isToken"/></xsl:attribute>
<xsl:value-of select="RadialContext/payment:RadialUniqueId"/>
</xsl:element>
</RadialReplyContext>
<TenderType><xsl:value-of select="PaymentContext/payment:TenderType"/></TenderType>
</xsl:otherwise>
</xsl:choose>
<SlotsAvailable><xsl:value-of select="SlotsAvailability"/></SlotsAvailable>
<xsl:choose>
<xsl:when test="Reattempt">
<ReauthorizationAttempted><xsl:value-of select="ReattemptValue"/></ReauthorizationAttempted>
</xsl:when>
</xsl:choose>
</RadialReply>*
Input
<Sample>202008131228</Sample>
Output
<?xml version="1.0" encoding="UTF-8"?>202008131228
Desired Result : If an incoming XML does not have any XML element defined in XSLT . Exception should be thrown ( Which is not happening ) Need to know why and I want the code to throw an exception in this case . What code logic would achieve that ?
Code just loads the XSLT File and transforms it . Code does not go to exception block. it transforms successfully and String outputResultStr is the output above
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) {
}
答案1
得分: 2
在XSLT 1.0或2.0中,添加一个模板规则:
<xsl:template match="*">
<xsl:message terminate="yes">未知元素 <xsl:value-of select="name()"/> </xsl:message>
</xsl:template>
以匹配那些没有被任何其他模板规则匹配的元素。
在3.0中,添加 <xsl:mode on-no-match="fail"/>
。
之所以没有失败的原因是,XSLT定义了一个默认模板规则,用于处理未被样式表中的任何其他模板规则匹配的元素。
英文:
In XSLT 1.0 or 2.0, add a template rule
<xsl:template match="*">
<xsl:message terminate="yes">Unknown element <xsl:value-of select="name()"/></xsl:message>
</xsl:template>
to match elements that are not matched by any other template rule.
In 3.0, add <xsl:mode on-no-match="fail"/>
.
The reason it's not failing is that XSLT defines a default template rule to process elements that are not matched by any other template rule in the stylesheet.
答案2
得分: 0
xsl transform 将会通过,即使没有匹配的标签。
有几个选项...
- 重构 xsl 来匹配 / 并查找所需/允许的标签,如果找不到,则使用
<xsl:message terminate="yes"/>
抛出错误 - 为输入的 XML 创建 XML 模式并在转换之前进行验证
英文:
xsl transform will pass even if there are no matching tags.
There are several options ...
- refactor xsl to match / and look for desired/allowed tags and throw error using
<xsl:message terminate="yes"/>
if not found - create xml schema for input xml an validate before transform
答案3
得分: 0
您可以在XSLT转换之前实现SAX模式验证API。
这是我的SAX验证程序,指定要验证XML输入数据的模式:
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);
// 继续进行XSLT转换
一个格式良好且有效的XML文件示例:
<?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>
只需一次验证即可准确捕获所有范围的错误输入并正常退出应用程序。
例如:缺少标签</ProcedureDocument>
错误
由XML解析器处理null时报告的错误:cvc-elt.1:找不到元素'title'的声明。:cvc-elt.1:找不到元素'title'的声明。
例如:格式不正确的元素/属性 => <reference Value="#Procedure2"/>
错误
由XML解析器处理null时报告的错误:文档中根元素之前的标记必须是格式良好的。:文档中根元素之前的标记必须是格式良好的。
例如:格式不正确的xpath/tag =>
<?xml version="1.0" encoding="utf-8"?>
ProcedureDocument
<title>Procedures Anesthesia</title>
错误
由XML解析器处理null时报告的错误:内容不允许在前言中。:前言中不允许有内容。
英文:
You can implement SAX schema validation API before XSLT transformation.
> Here my SAX validation program specifies the schema to validate XML input data:
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
> A well-formed and valid XML file is:
<?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>
Just one validation can precisely catch all range of erroneous input and exit the application gracefully.
> e.g: missing tag </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'.
> e.g: ill-formed element/attribute => <reference Value="#Procedure2"/>
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.
> e.g: ill-formed xpath/tag =>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论