+

Search Tips   |   Advanced Search

Perform basic XPath operations

Use the XPathExecutable instances created using XFactory.prepareXPath methods to evaluate XPath expressions.

XPath expressions can be passed to the XFactory.prepareXPath method using a JAXP StreamSource object or using a plain Java string object. The resulting XPathExecutable instance is thread safe and can be reused to evaluate an XPath expression on multiple XML input documents.

For more info on usage, download and execute the XML samples


Tasks


Example


Example: Simple XPath invocation.

The above code reads names.xml

To invoke...

Output is "Doe"


Example: Prepare and execute an interpreted XPath expression.

// 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 = xPathExecutable.execute(new StreamSource(new ByteArrayInputStream(xml.getBytes())));


Example: Prepare and execute a compiled XPath expression.

// 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())));


Example: Prepare and execute interpreted XPath expressions with schema validation.

// 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())));
  • Use static and dynamic contexts
  • Use external functions
  • Use external functions with XPath
  • Use a source resolver at prepare time
  • Use a message handler
  • Use a message handler and managing exceptions
  • Choosing between the compiler and the interpreter
  • Use external variables
  • Use external variables with XPath
  • Use a source resolver at execution time
  • Registering a collection resolver
  • Work with collations
  • XML Samples Application