执行基本 XPath 操作
您可以使用 XPathExecutable 实例(使用 XFactory.prepareXPath 方法创建的实例),对 XPath 表达式进行求值。
关于此任务
可以使用 JAXP StreamSource 对象或使用纯 Java™ 字符串对象,将 XPath 表达式传递到 XFactory.prepareXPath 方法。生成的 XPathExecutable 实例对于线程是安全的,且可复用以针对多个 XML 输入文档对 XPath 表达式进行求值。
过程
示例
以下是准备和执行已解释 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())));
The
following is a basic example of preparing and executing 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())));
以下是准备和执行已解释 XPath 表达式并进行模式验证的基本示例。
// 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())));