查看结果
在您的应用程序已准备或已加载 XPath 表达式(XPathExecutable 对象)、XSLT 样式表(XSLTExecutable 对象)或 XQuery 表达式(XQueryExecutable 对象)的 XExecutable 对象后,将 XExecutable 对象提供给某些输入,然后对结果执行操作。您提供的输入的源以及要对结果执行的操作确定您所使用的执行方法。
过程
- 查看 XPath 结果。
如果是 XPath 表达式,那么 XPathExecutable 接口上的执行方法将返回 XSequenceCursor 类的实例,此类包含针对给定上下文项和动态上下文对表达式进行求值生成的序列(如果存在)。
XPathExecutable 接口上还具有一组 executeToList 方法。这些方法会将序列作为 java.util.List<XItemView> 接口的实例返回,通过对 XPath 表达式求值所生成序列中的每个项在列表中表示为 XItemView 接口的实例。此列表中的条目顺序与对 XPath 表达式进行求值生成的序列顺序相同。
以下示例显示如何作为 XSequenceCursor 获取采购订单上每个项的成本。XFactory factory = XFactory.newInstance(); XPathExecutable expr = factory.prepareXPath( "/purchaseOrder/item/(@unit-price * @quantity)"); XSequenceCursor exprResult = expr.execute(new StreamSource("input.xml"));
以下示例显示如何获取 java.util.List 接口的实例中采购订单上每个项的成本。XFactory factory = XFactory.newInstance(); XPathExecutable expr = factory.prepareXPath( "/purchaseOrder/item/(@unit-price * @quantity)"); List<XItemView> exprResult = expr.executeToList(new StreamSource("input.xml"));
- 使用 XItemView 接口。
您可以在 XItemView 接口上使用方法,访问序列中的每个项。 您可以使用 XItemView.isAtomic() 方法以确定项是原子值还是节点。如果项为原子值,那么可以在 XItemView 接口上使用 getValueType() 方法。 此方法会返回枚举类型 XTypeConstants.Type 的实例。 如果原子值是内置原子类型或从内置原子类型派生的用户定义的类型的实例,那么 getValueType() 方法的结果是对应于此类型的枚举值。对于可以是用户定义的派生类型的原子值,您还可能会发现可以方便地使用 XItemView.getValueTypeName() 方法确定值的精确类型。
如果是原子值类型,那么可以使用相应方法来获取值。XFactory factory = XFactory.newInstance(); XPathExecutable expr = factory.prepareXPath( "sum(/purchaseOrder/item/(@unit-price * @quantity))"); XItemView exprResult = expr.execute(new StreamSource(purchaseOrder)); double totalCost = 0.0; // Decide how to get result based on the type of the value switch (exprResult.getValueType()) { case DOUBLE: { totalCost = exprResult.getDoubleValue(); break; } case FLOAT: { totalCost = exprResult.getFloatValue(); break; } case INTEGER: { totalCost = exprResult.getLongValue(); break; } case DECIMAL: { totalCost = exprResult.getDecimalValue().doubleValue(); break; } default: { System.err.println("Unexpected type for result"); } }
XItemView 接口还会扩展 XNodeView 接口,因此,如果调用 XItemView.isAtomic() 方法的结果为 false,即,项为节点,那么可以使用继承自 XNodeView 接口的方法访问有关节点的信息。 XNodeView.getNodeQName() 方法将返回节点的名称,XNodeView.getKind() 方法将返回枚举类型 XNodeView.Kind 的值,此枚举类型指示节点的排序,且项为:XNodeView.DOCUMENT、XNodeView.ELEMENT 等。XFactory factory = XFactory.newInstance(); // Prepare an expression to get the first node whose string value matches a given query string XPathExecutable expr = factory.prepareXPath( "(/descendant-or-self::node()/(self::node()|@*)[. = 'search'])[1]"); XItemView exprResult = expr.execute(new StreamSource(inputFile)); // Print the kind of node found and its name, if appropriate switch (exprResult.getKind()) { case ELEMENT: { System.out.print("Element " + exprResult.getNodeQName().toString()); break; } case ATTRIBUTE: { System.out.print("Attribute " + exprResult.getNodeQName().toString()); break; } case COMMENT: { System.out.print("Comment "); break; } case PROCESSING_INSTRUCTION: { System.out.print("PI " + exprResult.getNodeQName().toString()); } }
您还可以使用 XTreeCursor 接口浏览包含节点的树。
- 查看 XSLT 和 XQuery 结果。
XPathExecutable 接口上提供的所有执行方法和 executeToList 方法实际上继承自 XExecutable 接口,因此还可以将它们用在 XSLTExecutable 接口或 XQueryExecutable 接口的实例上。如果是 XQueryExecutable 接口的实例,那么此方法返回的对象会包含对查询进行求值生成的序列。如果是 XSLTExecutable 接口,那么序列会包含对样式表进行求值的主要结果的文档节点(如果存在)。
XSLTExecutable 和 XQueryExecutable 接口还定义执行方法,且这些方法接受 javax.xml.transform.Result 接口的实例。应用程序提供的结果对象将包含对样式表进行求值的主要结果(如果是 XSLTExecutable 接口),或对查询进行求值的结果(如果是 XQueryExecutable)。
以下示例会生成 DOM 树(变换的结果),并将树存储在作为自变量传递到 XSLTExecutable.execute 方法的 DOMResult 类的实例中。XFactory factory = XFactory.newInstance(); XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl")); DOMResult res = new DOMResult(); style.execute(new StreamSource("purchase.xml"), res); Node node = res.getNode();
以下示例生成 XSequenceCursor 接口的实例(变换的结果)。XFactory factory = XFactory.newInstance(); XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl")); XSequenceCursor xformResult = style.execute(new StreamSource("purchase.xml"));
- 使用 XResultResolver 和 XSLT。
XSLT 2.0 样式表可通过使用 xsl:result-document 指令,生成多个结果文档。为样式表构造的任何结果树生成变换的主要结果,包括未包含在 xsl:result-document 指令中的结果树和包含在 xsl:result-document 指令中的结果树,且 xsl:result-document 指令具有其有效值为零长度字符串的 href 属性。会使用前面描述的各种方法将主要结果返回给应用程序。
如果样式表对 xsl:result-document 指令求值,且此指令具有其有效值为非零长度字符串的 href 属性,那么缺省情况下,会将该结果写入输出流。通过根据动态上下文中的基本输出 URI 的设置解析 href 属性的有效值,以确定输出流的 URI。
您可以通过在用于变换的 XDynamicContext 上提供 XResultResolver 接口的实例,覆盖此缺省行为。如果调用了任何 xsl:result-document 指令,那么会调用 XResultResolver 接口上的 getResult 方法;在 xsl:result-document 指令的结果已定向的情况下,getResult 方法的结果为 Result 接口的实例。使用 XResultResolver 接口,使您的应用程序可以根据情况,决定是否定向样式表中的所有 xsl:result-document 指令的结果。 如果应用程序没有为 XSLTExecutable 接口的执行方法上的主要结果提供 Result 接口的实例,那么还会为主要结果调用 XResultResolver 的 getResult 方法。
如果 XResultResolver 的 getResult 方法返回空引用,那么会为此特定结果复原缺省行为,即,如果是次要结果,那么会将结果写入输出流,或者如果是主要结果,那么会将结果作为执行方法(作为 XSequenceCursor 接口的实例)的结果返回。
以下示例使用 XResultResolver 接口的实例,以捕获将样式表生成的所有结果作为变量 allResults 中的 XSequenceCursorResult 类的实例。然后,此应用程序可使用 XSequenceCursorResult.getSequenceCursor() 方法,截取 XSequenceCursorResult 类的每个实例包含的 XSequenceCursor 接口的实例。
final ArrayList<XSequenceCursorResult> allResults = new ArrayList<XSequenceCursorResult>(); XFactory factory = XFactory.newInstance(); XDynamicContext context = factory.newDynamicContext(); // Create XResultResolver that saves XSequenceCursorResult // instances in allResults context.setResultResolver(new XResultResolver() { public Result getResult(String href, String base) { XSequenceCursorResult result = new XSequenceCursorResult(); allResults.add(result); return result; } }); XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl")); style.execute(new StreamSource("purchase.xml"), context); // All results, including the primary, are now available from the allResults variable.


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_results_view
文件名:txml_results_view.html