After your application has evaluated an XPath or XQuery expression or performed a transformation with an XSLT stylesheet, you might want to write the output as an actual XML document represented as a file or as a Java string. The process of rendering results as an XML document is known as serialization.
Your application can call the XSequenceCursor.exportSequence method to serialize a sequence that is represented by an instance of the XSequenceCursor interface. The arguments on this method are an instance of the javax.xml.transform.Result interface and optionally an instance of the XOutputParameters interface.
If the instance of the Result interface is also an instance of the javax.xml.transform.stream.StreamResult class, the sequence is serialized as described in the XSLT 2.0 and XQuery 1.0 Serialization Recommendation. The StreamResult object can contain an instance of the java.io.Writer class or the java.io.OutputStream class, where the processor will write the serialized sequence.
XFactory factory = XFactory.newInstance(); XPathExecutable expr = factory.prepareXPath("/purchaseOrder/item[@price > 1000]"); XSequenceCursor exprResult = expr.execute(new StreamSource(inputFile)); System.out.println("Items purchased costing more than $1000"); if (exprResult != null) { // Set indenting in order to pretty-print result XOutputParameters params = factory.newOutputParameters(); params.setIndent(true); exprResult.exportSequence(new StreamResult(System.out), params); } else { System.out.println("None found"); }
XFactory factory = XFactory.newInstance(); XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl")); XSequenceCursor xformResult = style.execute(new StreamSource("purchase.xml"); XOutputParameters params = style.getOutputParameters(new QName("my-output-definition")); params.setMethod(XOutputParameters.METHOD_XHTML); xformResult.exportSequence(new StreamResult("output.html"), params);
Note that according to the XSLT 2.0 and XQuery 1.0 Serialization Recommendation, a serialization error results if the sequence that is to be serialized contains attribute nodes or namespace nodes. If the sequence that you need to serialize might contain attribute or namespace nodes, get the values of those nodes as strings or some other appropriate type and serialize those values instead.
You can also serialize just the current item in an instance of the XSequenceCursor interface by using one of the exportItem methods. The exportItem methods are inherited from the XItemView interface, so they can be called on an instance of that interface as well.
As with the exportSequence method described above, the arguments of the exportItem method are an instance of the javax.xml.transform.Result interface and optionally an instance of the XOutputParameters interface. The effect of calling exportItem is identical to the effect of calling exportSequence with a sequence that consists of just the current item.
XFactory factory = XFactory.newInstance(); XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl")); style.execute(new StreamSource("purchase.xml"), new StreamResult("output.xml"));
If your application supplies an instance of the XResultResolver interface on a transformation, your application can direct each final result tree to a different destination.
You can use the XML API to transform XML data contained in an instance of javax.xml.transform.Source directly to an instance of a javax.xml.transform.Result. This is often referred to as an identity transformation. See Performing basic XSLT operations for an example.
In this information ...Related information
| IBM Redbooks, demos, education, and more(Index) Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience. This feature requires Internet access. Most of the following links will take you to information that is not part of the formal product documentation and is provided "as is." Some of these links go to non-IBM Web sites and are provided for your convenience only and do not in any manner serve as an endorsement by IBM of those Web sites, the material thereon, or the owner thereof. |