WAS v8.5 > Develop applications > XML applications > Use the XML API to perform operations > Performing basic operations

Performing basic XQuery operations

We can use the XQueryExecutable instances created using XFactory.prepareXQuery methods to evaluate XQuery expressions. XQuery expressions can be passed to the XFactory.prepareXQuery method using a JAXP StreamSource object or using a plain Java string object. The resulting XQueryExecutable instance is thread safe and can be reused to evaluate an XQuery expression on multiple XML input documents.


Example

The following is a basic example of preparing and executing an interpreted XQuery expression.

// Create a string for the XQuery expression
String expression = "/doc/name[@first='David']";

// Create the factory
XFactory factory = XFactory.newInstance();

// Create the XQueryExecutable
XQueryExecutable xQueryExecutable = factory.prepareXQuery(expression);

// Create the input XML source String xml = "<doc<name first='John'>Wrong</name><name first='David'>Correct</name></doc>";

// Execute the expression and store the results in an XSequenceCursor
XSequenceCursor xSequenceCursor = xQueryExecutable.execute(new StreamSource(new ByteArrayInputStream(xml.getBytes())));

The following is a basic example of preparing and executing a compiled XQuery expression.

// Create a string for the XQuery expression
String expression = "/doc/name[@first='David']";

// Create the factory
XFactory factory = XFactory.newInstance();

// Create a new static context from the factory
XStaticContext xStaticContext = factory.newStaticContext();

// Set the mode to compile for the processor
xStaticContext.setUseCompiler(true);

// Create the XQueryExecutable
XQueryExecutable xQueryExecutable = factory.prepareXQuery(expression, xStaticContext);

// Create the input XML source String xml = "<doc><name first='John'>Wrong</name><name first='David'>Correct</name></doc>";

// Execute the expression and store the results in an XSequenceCursor
XSequenceCursor xSequenceCursor = xQueryExecutable.execute(new StreamSource(new ByteArrayInputStream(xml.getBytes())));

The following is a basic example of preparing and executing interpreted XQuery expressions with schema awareness.

// Create a string for the XQuery expression
String expression = "/my:doc/name[@first='David']/@first";
    
// Create the factory
XFactory factory = XFactory.newInstance();
    
// Create the schema source String schema = "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'" +
            " targetNamespace='http://www.schematype.ibm.com/UDSimple'" +
            " xmlns:my='http://www.schematype.ibm.com/UDSimple'" +
            " xmlns:smokey='http://www.schematype.ibm.com/UDSimple'>"
                    +" <xsd:element name='doc'>"
                    +   "<xsd:complexType> "
                    +   "<xsd:sequence>"
                    +   "<xsd:element name='name' minOccurs='0' maxOccurs='unbounded'>"
                    +  "<xsd:complexType>"
                    +   "<xsd:attribute name='first' type='xsd:string' use='optional'/>"
                    +  "</xsd:complexType>"
                    +   "</xsd:element>"
                    +  "</xsd:sequence>"
                    +  "</xsd:complexType>"
                    + "</xsd:element>"
                    + "</xsd:schema>";
            
// Load the schema
factory.registerSchema(new StreamSource(new ByteArrayInputStream(schema.getBytes())));
            
// Turn on validation factory.setValidating(true);
            
// Create a new static context from the factory
XStaticContext xStaticContext = factory.newStaticContext();
            
// Add a new namespace xStaticContext.declareNamespace("my", "http://www.schematype.ibm.com/UDSimple");
            
// Create the XQueryExecutable
XQueryExecutable xQueryExecutable = factory.prepareXQuery(expression, xStaticContext);
    
// Create the input XML source String xml = "<my:doc xmlns:my='http://www.schematype.ibm.com/UDSimple'>" +
              "<name first='John'/><name first='David'/></my:doc>";
    
// Execute the expression and store the results in an XSequenceCursor
XSequenceCursor xSequenceCursor = xQueryExecutable.execute(new StreamSource(new ByteArrayInputStream(xml.getBytes())));


Related


Use static and dynamic contexts
Use external functions
Use external functions with XQuery
Use a source resolver at prepare time
Use a message handler
Use a message handler and managing exceptions
Choose between the compiler and the interpreter
Use external variables
Use external variables with XQuery
Use a source resolver at execution time
Registering a collection resolver
Work with collations


+

Search Tips   |   Advanced Search