기본 XQuery 조작 수행
XQuery 표현식을 평가하기 위해 XFactory.prepareXQuery 메소드를 사용하여 작성된 XQueryExecutable 인스턴스를 사용할 수 있습니다.
이 태스크 정보
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())));