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

Puede utilizar las instancias de XPathExecutable que se crean utilizando los métodos XFactory.prepareXPath para llevar a cabo transformaciones de XPath.

Acerca de esta tarea

Las expresiones de XPath se pueden pasar al método XFactory.prepareXPath utilizando un objeto StreamSource de JAXP o utilizando un objeto de serie plano de Java™. La instancia resultante de XPathExecutable ofrece seguridad de hebras y se puede volver a utilizar para evaluar una XPath 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 XPath.
// Create a string for the XPath expression
String expression = "/doc/something";

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

// Create an XPath executable for the expression
XPathExecutable xPathExecutable = factory.prepareXPath(expression);

// Create the input XML source
String xml = "<doc><something>something is selected</something></doc>";

// Execute the expression and store the results in an XSequenceCursor
XSequenceCursor xSequenceCursor = xPathExecutable.execute(new StreamSource(new ByteArrayInputStream(xml.getBytes())));
A continuación encontrará un ejemplo básico para preparar y ejecutar una expresión compilada de XPath.
// Create a string for the XPath expression
String expression = "/doc/something";

// 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 an XPath executable for the expression
XPathExecutable xPathExecutable = factory.prepareXPath(expression, xStaticContext);

// Create the input XML source
String xml = "<doc><something>something is selected</something></doc>";

// Execute the expression and store the results in an XSequenceCursor
XSequenceCursor xSequenceCursor = xPathExecutable.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 XPath con validación de esquema.
// Create a string for the XPath expression
String expression = "/doc/byte cast as my:derived1-byte-enumeration-Type";

// 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:simpleType name='derived1-byte-enumeration-Type'>"
          + "   <xsd:restriction base='xsd:byte'>"
          + "     <xsd:enumeration value='1' />"
          + "     <xsd:enumeration value='-1' />"
          + "     <xsd:enumeration value='0' />"
          + "     <xsd:enumeration value='127' />"
          + "     <xsd:enumeration value='-128' />"
          + "     <xsd:enumeration value='32' />"
          + "     <xsd:enumeration value='-32' />"
          + "     <xsd:enumeration value='8' />"
          + "     <xsd:enumeration value='-8' />"
          + "     <xsd:enumeration value='2' />"
          + "     <xsd:enumeration value='-2' />"
          + "   </xsd:restriction>"
          + "  </xsd:simpleType>"
          + "</xsd:schema>";
// Load 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 new namespace
xStaticContext.declareNamespace("my", "http://www.schematype.ibm.com/UDSimple");

// Create an XPath executable for the expression
XPathExecutable xPathExecutable = factory.prepareXPath(expression, xStaticContext);
            
// Create the input XML source
String xml = "<doc>" +
             "    <byte>1</byte>" +
             "</doc>";

// Execute the expression and store the results in an XSequenceCursor
XSequenceCursor xSequenceCursor = xPathExecutable.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_xpath
File name: txml_ops_xpath.html