Freemarker DOM 属性检查不起作用
Freemarker DOM attribute check not working
我有以下XML
<OS_LINESTATUS LINENUMBER="1" QUANTITY="6" REQUESTNUMBER="598089607" STATUSCODE="LC"/>
<OS_LINESTATUS LINENUMBER="1" QUANTITY="1" REQUESTNUMBER="725772899" STATUSCODE="LC"/>
<OS_LINESTATUS LINENUMBER="1" REQUESTNUMBER="599927622" STATUSCODE="LI"/>
我有以下 freemarker 模板,其中 xml 元素被注入为 "elements"。
[<#list elements as element>{"primeLineNo":"${element.OS_LINESTATUS.@LINENUMBER}","poLineStatusInfos":[{"poLineStatus":"${element.OS_LINESTATUS.@STATUSCODE}"<#if element.OS_LINESTATUS.@QUANTITY??>,"poLineStatusQuantity":{"measurementValue":"${element.OS_LINESTATUS.@QUANTITY}","unitOfMeasure":"EA"}</#if>}]}<#sep>,</#sep></#list>]}]}}
但这在第三行失败了,因为没有数量属性,即使我已经设置了 if 条件。为什么 if 条件不起作用?
以下是堆栈跟踪
==> element.OS_LINESTATUS.@QUANTITY [in template "suman-xml-to-xml- orderrequestxform/xml-aggregation/linestatus.ftl" at line 4, column 331]
----
Tip: This XML query result can't be used as string because for that it had to contain exactly 1 XML node, but it contains 0 nodes. That is, the constructing XML query has found no matches.
FTL 堆栈跟踪(“~”表示嵌套相关):- 失败于:${element.OS_LINESTATUS.@QUANTITY} [在模板 "suman-xml-to- xml-orderrequestxform/xml-aggregation/linestatus.ftl" 第 4 行第 329 列]
Java 堆栈跟踪(对于程序员):
freemarker.core.NonStringException: [... Exception message was already printed; see it above ...]
at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:390)
at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
at freemarker.core.DollarVariable.accept(DollarVariable.java:41)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.MixedContent.accept(MixedContent.java:54)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:48)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.MixedContent.accept(MixedContent.java:54)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:268)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220)
at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:194)
at freemarker.core.Environment.visitIteratorBlock(Environment.java:572)
at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:64)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.MixedContent.accept(MixedContent.java:54)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.Environment.process(Environment.java:302)
at freemarker.template.Template.process(Template.java:325)
at example.aggregation.XmlElementAggregator.output(XmlElementAggregator.java:93)
at example.aggregation.XmlElementAggregator.aggregate(XmlElementAggregator.java:58)
at example.aggregation.XmlElementAggregator.visitAfter(XmlElementAggregator.java:45)
at org.milyn.delivery.sax.SAXHandler.visitAfter(SAXHandler.java:389)
at org.milyn.delivery.sax.SAXHandler.endElement(SAXHandler.java:204)
at org.milyn.delivery.SmooksContentHandler.endElement(SmooksContentHandler.java:96)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.milyn.delivery.sax.SAXParser.parse(SAXParser.java:76)
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:86)
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:64)
at org.milyn.Smooks._filter(Smooks.java:526)
at org.milyn.Smooks.filterSource(Smooks.java:482)
at example.TransformerBuilder.transform(TransformerBuilder.java:66)
at example.TransformAggregation.main(TransformAggregation.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
#if
条件有效,因为您询问 element.OS_LINESTATUS.@QUANTITY
是否存在,而确实存在,因为它是大小为 0 的结果集。请改用 element.OS_LINESTATUS.@QUANTITY[0]??
。所以这就是 FreeMarker 的 DOM 包装器的棘手之处,即使查询什么也找不到 returns 东西。这就是为什么这样的结果是 #list
-able,更重要的是,可以作为进一步 XPath 查询的上下文(比如 element.maybeMissingElement.child
永远不会失败,只是有时会给出一个空结果集)。
我有以下XML
<OS_LINESTATUS LINENUMBER="1" QUANTITY="6" REQUESTNUMBER="598089607" STATUSCODE="LC"/>
<OS_LINESTATUS LINENUMBER="1" QUANTITY="1" REQUESTNUMBER="725772899" STATUSCODE="LC"/>
<OS_LINESTATUS LINENUMBER="1" REQUESTNUMBER="599927622" STATUSCODE="LI"/>
我有以下 freemarker 模板,其中 xml 元素被注入为 "elements"。
[<#list elements as element>{"primeLineNo":"${element.OS_LINESTATUS.@LINENUMBER}","poLineStatusInfos":[{"poLineStatus":"${element.OS_LINESTATUS.@STATUSCODE}"<#if element.OS_LINESTATUS.@QUANTITY??>,"poLineStatusQuantity":{"measurementValue":"${element.OS_LINESTATUS.@QUANTITY}","unitOfMeasure":"EA"}</#if>}]}<#sep>,</#sep></#list>]}]}}
但这在第三行失败了,因为没有数量属性,即使我已经设置了 if 条件。为什么 if 条件不起作用?
以下是堆栈跟踪
==> element.OS_LINESTATUS.@QUANTITY [in template "suman-xml-to-xml- orderrequestxform/xml-aggregation/linestatus.ftl" at line 4, column 331]
----
Tip: This XML query result can't be used as string because for that it had to contain exactly 1 XML node, but it contains 0 nodes. That is, the constructing XML query has found no matches.
FTL 堆栈跟踪(“~”表示嵌套相关):- 失败于:${element.OS_LINESTATUS.@QUANTITY} [在模板 "suman-xml-to- xml-orderrequestxform/xml-aggregation/linestatus.ftl" 第 4 行第 329 列]
Java 堆栈跟踪(对于程序员):
freemarker.core.NonStringException: [... Exception message was already printed; see it above ...]
at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:390)
at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
at freemarker.core.DollarVariable.accept(DollarVariable.java:41)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.MixedContent.accept(MixedContent.java:54)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:48)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.MixedContent.accept(MixedContent.java:54)
at freemarker.core.Environment.visitByHiddingParent(Environment.java:345)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:268)
at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220)
at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:194)
at freemarker.core.Environment.visitIteratorBlock(Environment.java:572)
at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:64)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.MixedContent.accept(MixedContent.java:54)
at freemarker.core.Environment.visit(Environment.java:324)
at freemarker.core.Environment.process(Environment.java:302)
at freemarker.template.Template.process(Template.java:325)
at example.aggregation.XmlElementAggregator.output(XmlElementAggregator.java:93)
at example.aggregation.XmlElementAggregator.aggregate(XmlElementAggregator.java:58)
at example.aggregation.XmlElementAggregator.visitAfter(XmlElementAggregator.java:45)
at org.milyn.delivery.sax.SAXHandler.visitAfter(SAXHandler.java:389)
at org.milyn.delivery.sax.SAXHandler.endElement(SAXHandler.java:204)
at org.milyn.delivery.SmooksContentHandler.endElement(SmooksContentHandler.java:96)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.milyn.delivery.sax.SAXParser.parse(SAXParser.java:76)
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:86)
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:64)
at org.milyn.Smooks._filter(Smooks.java:526)
at org.milyn.Smooks.filterSource(Smooks.java:482)
at example.TransformerBuilder.transform(TransformerBuilder.java:66)
at example.TransformAggregation.main(TransformAggregation.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
#if
条件有效,因为您询问 element.OS_LINESTATUS.@QUANTITY
是否存在,而确实存在,因为它是大小为 0 的结果集。请改用 element.OS_LINESTATUS.@QUANTITY[0]??
。所以这就是 FreeMarker 的 DOM 包装器的棘手之处,即使查询什么也找不到 returns 东西。这就是为什么这样的结果是 #list
-able,更重要的是,可以作为进一步 XPath 查询的上下文(比如 element.maybeMissingElement.child
永远不会失败,只是有时会给出一个空结果集)。