Exécution d'opérations de base avec XPath
Vous pouvez utiliser les instances XPathExecutable qui sont créées par la méthode XFactory.prepareXPath pour calculer des expressions XPath.
Pourquoi et quand exécuter cette tâche
Vous pouvez transmettre des expressions XPath à la méthode XFactory.prepareXPath à l'aide d'un objet JAXP StreamSource ou d'un objet chaîne simple Java™. L'instance XPathExecutable obtenue bénéficie d'unités d'exécution sécurisées et peut être réutilisée pour évaluer une expression XPath sur plusieurs documents d'entrée XML.
Procédure
Exemple
L'exemple suivant illustre comment préparer puis exécuter une expression XPath interprétée.
// 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())));
L'exemple suivant illustre comment préparer puis exécuter une expression XPath compilée.
// 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())));
L'exemple suivant illustre comment préparer puis exécuter des expressions XPath interprétées avec une validation de schéma.
// 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())));