基本的な XQuery 操作の実行
XFactory.prepareXQuery メソッドを使用して作成された XQueryExecutable インスタンスを使用して、 XQuery 式を評価することができます。
このタスクについて
XQuery 式は、JAXP StreamSource オブジェクトを使用して、 またはプレーンな Java™ ストリング・オブジェクトを使用して、XFactory.prepareXQuery メソッドに渡すことができます。結果として得られる XQueryExecutable インスタンスはスレッド・セーフであり、複数の XML 入力文書で XQuery 式を評価するために再利用できます。
手順
例
以下に、インタープリットされた 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())));
以下に、コンパイルされた 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())));
以下に、スキーマ認識を使用して、インタープリットされた XQuery 式の作成と実行を行う基本的な例を示します。
// 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())));