JAXP(Java API for XML Processing )에 대한 프로세서의 관계
대부분의 경우, JAXP(Java™ API for XML Processing)를 사용하여 현재 API를 사용하는 애플리케이션의 마이그레이션이 필요합니다.
JAXP의 XSLT 및 XPath 처리 부분은 XSLT 1.0 및 XPath 1.0에 대한 참조로 정의됩니다. JAXP에는 XSLT 2.0 및 XPath 2.0 처리를 위한 프로비전이 없습니다. 특히, XSLT 2.0 또는 XPath 2.0 프로세서가 동일한 입력과 동일한 스타일시트 또는 표현식을 지정한 XSLT 1.0 또는 XPath 1.0 프로세서에서 생성된 것과 다른 결과를 생성해야 하는 상황이 있습니다. 따라서, JAXP를 사용하여 현재 프로세서를 인스턴스화할 수 없습니다.
또한 JAXP는 시퀀스, XQuery 1.0 또는 XSLT 2.0, XPath 2.0 및 XQuery 1.0에서 사용 가능한 여러 현재 데이터 유형에 대한 지원이 없습니다. 또한 JAXP는 입력과 출력을 사용할 수 있는 양식으로 제한됩니다. 이 모든 것 때문에 XPath 2.0 및 XQuery 1.0 표현식뿐만 아니라 XSLT 2.0 스타일시트를 처리하는 데 적합하지 않습니다.
다음 예는 공통 마이그레이션 시나리오 및 JAXP를 사용하여 작성될 수 있는 코드와 동등한 현재 API를 사용하여 코드를 작성하는 방법을 보여줍니다.
API를 사용한 XSLT 스타일시트 처리
XFactory factory = XFactory.newInstance();
XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl"));
style.execute(new StreamSource("input.xml"), new StreamResult(System.out));
API를 사용한 XPath 표현식 처리
XFactory factory = XFactory.newInstance();
XPathExecutable pathExpr = factory.prepareXPath("/doc/child[@id='N1378']");
// Process input from a StreamSource
XSequenceCursor result1 = pathExpr.execute(new StreamSource("input.xml"));
// Process input from a DOM node
XSequenceCursor result2 = pathExpr.execute(new DOMSource(node));
URI 참조 분석
JAXP URIResolver 인터페이스의 인스턴스를 사용하여 XSLT document() 함수에 대한 참조를 분석하면, 이제 XSourceResolver 인터페이스를 사용하여 동일한 일을 수행할 수 있습니다. document() 함수 또는 fn:doc() 함수에 대한 참조를 분석하려면, XDynamicContext 인터페이스의 인스턴스에서 XSourceResolver 인터페이스의 인스턴스를 설정할 수 있습니다. xsl:import 및 xsl:include 선언을 통해 가져온 스타일시트에 대한 참조를 분석하려면, XStaticContext 인터페이스의 인스턴스에서 XSourceResolver 인터페이스의 인스턴스를 설정할 수 있습니다.
final XFactory factory = XFactory.newInstance();
XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl"));
XDynamicContext dContext = factory.newDynamicContext();
// Create and set an instance of an anonymous inner class as the
// XSourceResolver
dContext.setSourceResolver(new XSourceResolver() {
// Create an item to use as the initial context node for
// transformations in the getSource method
private XItemView fDummyNode =
factory.getItemFactory()
.item(new StreamSource(
new StringReader("<doc/>")));
// Resolve URIs by loading the resource as an XSLT stylesheet
// and evaluating it - return the result as the Source to use
public Source getSource(String href, String base) {
java.net.URI baseURI;
try {
// Get base URI object
baseURI = new java.net.URI(base);
} catch (java.net.URISyntaxException use) {
throw new RuntimeException(use);
}
// Resolved relative reference against base URI
String resolvedURI = baseURI.resolve(href).toString();
// Prepare and execute the stylesheet
XItemView transformResult =
factory.prepareXSLT(new StreamSource(resolvedURI))
.execute(fDummyNode);
return new XItemSource(transformResult);
}
});
XSequenceCursor result = style.execute(new StreamSource("input.xml"), dContext);
확장기능 및 외부 기능 정의
XPath 표현식용 JAXP를 사용하여, XPathFunctionResolver 인터페이스의 인스턴스를 등록하여 XPath 표현식이 호출할 수 있는 확장기능의 구현을 제공할 수 있습니다. JAXP의 XSLT 부분에는 동등한 메커니즘이 없습니다.
현재 API로, XStaticContext 인터페이스에서 확장 함수를 선언하여 호출 함수의 예상된 유형 및 인수의 예상된 유형을 지정할 수 있으며, XDynamicContext 인터페이스의 인스턴스에서 확장 함수의 구현을 등록할 수 있습니다. XSLT 스타일시트 및 XPath 및 XQuery 표현식은 등록된 확장 함수를 호출할 수 있습니다.
스타일시트 매개변수 및 외부 변수의 값 설정
JAXP를 사용하여, Transformer.setParameter 메소드를 호출하여 스타일시트 매개변수의 초기 값을 제공하고 XPathVariableResolver 인터페이스의 인스턴스를 제공하여 XPath 표현식에 대한 변수의 값을 제공할 수 있습니다. API를 사용하여, XStaticContext 인터페이스의 declareVariable() 메소드를 사용하여 변수를 선언하여, 변수 이름과 예상되는 유형의 변수를 지정할 수 있습니다. XDynamicContext 인터페이스의 bind() 메소드 중 하나를 통해 스타일시트 매개변수, XPath 변수 및 XQuery 외부 변수의 값을 제공할 수 있습니다.
XFactory factory = XFactory.newInstance();
XStaticContext sContext = factory.newStaticContext();
// Declare the XPath variable "query-id" in the static context
QName queryIdVar = new QName("query-id");
sContext.declareVariable(queryIdVar, XTypeConstants.STRING_QNAME);
// Prepare the XPath expression
XItemFactory itemFactory = factory.getItemFactory();
XPathExecutable expr =
factory.prepareXPath("/catalog/product[id eq $query-id]", sContext);
XItemView catalog = itemFactory.item(new StreamSource("catalog.xml"));
XDynamicContext dContext = factory.newDynamicContext();
// Set the value of the "query-id" variable, and evaluate the
// expression with that variable value
dContext.bind(queryIdVar, "ID43785");
XSequenceCursor product1 = expr.execute(catalog, dContext);
// Set the value of the "query-id" variable, and evaluate the
// expression with the new variable value
dContext.bind(queryIdVar, "ID18574");
XSequenceCursor product2 = expr.execute(catalog, dContext);
ID 변환
JAXP에서 자주 사용되는 다른 조작은 ID 변환입니다. 이는 데이터를 한 양식에서 다른 양식으로 변환하는 편리한 방법입니다(예: SAX 이벤트에서 DOM 트리 생성 또는 DOM 트리 직렬화). API를 사용하여 ID 변환을 수행할 수 있습니다. 예제를 보려면 기본 XSLT 조작 수행의 내용을 참조하십시오.
준비 시간 및 실행 시간 구성
JAXP에서, XSLT 스타일시트에 대한 많은 런타임 구성 정보(스타일시트 매개변수의 값, URIResolvers 등)를 변환을 수행하는 데 사용된 오브젝트(Transformer 인터페이스 및 TransformerHandler 인터페이스의 인스턴스)에 직접 제공할 수 있습니다. 마찬가지로, JAXP의 TransformerFactory 및 XPathFactory의 인스턴스에 직접 스타일시트와 XPath 표현식의 준비를 위한 구성 정보를 제공합니다.
API에서, XStaticContext 인터페이스의 인스턴스를 사용하여 스타일시트나 표현식이 준비된 경우(네임스페이스 바인딩, 외부 함수나 변수의 유형 등) 필요한 구성 정보를 제공할 수 있습니다. 마찬가지로, 스타일시트나 표현식을 평가하는 데 필요한 구성 정보(변수의 값, 출력 매개변수의 설정 등)를 XDynamicContext 인터페이스의 인스턴스에 제공할 수 있으며, 인수를 XExecutable 인터페이스 및 서브 인터페이스의 실행 메소드에 전달할 수 있습니다.
구성 정보를 별도의 오브젝트로 이렇게 분리하면 API가 보다 스레드에서 안전할 수 있게 됩니다. 사용자 애플리케이션은 동기화 없이 다른 스레드에서 XExecutable 인터페이스의 동일한 인스턴스를 사용할 수 있습니다. Transformer, TransformerHandler 및 XPathExpression 인터페이스의 인스턴스가 스레드에서 안전하지 않은 JAXP와 대조적입니다. 이를 사용하는 모든 스레드는 이 오브젝트의 공유된 인스턴스에 대한 액세스를 동기화하거나 각 스레드에 특정한 별개의 사본을 작성해야 합니다.
오류 처리
JAXP에서, ErrorHandler 인터페이스의 인스턴스를 제공하여 프로세서가 오류에 응답해야 하는 방법을 제어할 수 있습니다. API에서, 준비 시간 오류의 경우 XStaticContext 인터페이스 또는 실행 시간 오류의 경우 XDynamicContext 인터페이스의 인스턴스에 XMessageHandler 인터페이스의 인스턴스를 제공하여 제어할 수 있습니다.