Cómo llevar a cabo operaciones básicas de XQuery

Puede utilizar las instancias de XQueryExecutable que se crean utilizando los métodos XFactory.prepareXQuery para llevar a cabo transformaciones de XQuery.

Acerca de esta tarea

Las expresiones de XQuery se pueden pasar al método XFactory.prepareXQuery utilizando un objeto StreamSource de JAXP o utilizando un objeto de serie plano de Java™. La instancia resultante de XQueryExecutable ofrece seguridad de hebras y se puede volver a utilizar para evaluar una XQuery en diversos documentos de entrada XML.

Procedimiento

Ejemplo

A continuación encontrará un ejemplo básico para preparar y ejecutar una expresión interpretada de XQuery.
// 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())));
A continuación encontrará un ejemplo básico para preparar y ejecutar una expresión compilada de XQuery.
// 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())));
A continuación encontrará un ejemplo básico de la preparación y ejecución de expresiones interpretadas de XQuery con independencia del esquema.
// 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())));

Icon that indicates the type of topic Task topic



Timestamp icon Last updated: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_ops_xquery
File name: txml_ops_xquery.html