XQuery에서 외부 함수 사용

외부 함수를 사용하는 XQuery 표현식을 사용하는 경우 XStaticContext 인스턴스를 사용하거나 외부 함수로 XQuery prolog에서 함수 서명을 선언하십시오. XDynamicContext 인스턴스를 사용하는 각 함수에 대해 Java 구현을 제공 또는 바인드합니다.

프로시저

  1. 외부 함수를 사용하는 XQuery 표현식을 준비하는 경우 XStaticContext 인스턴스를 사용하거나 XQuery prolog에서 함수 서명을 선언하십시오.

    XStaticContext에 선언된 함수만 기본 모듈에 표시됩니다. 라이브러리 모듈에 외부 함수를 표시하려면 해당 라이브러리 모듈의 prolog에 선언되어야 합니다.

    XStaticContext 인터페이스에는 각각 세 개의 매개변수(각각 이름, 함수의 리턴 유형, 인수 유형에 대한 배열)를 사용하는 2개의 declareFunction 메소드가 있습니다. 이름은 항상 QName 오브젝트로 제공되지만, 유형은 QNames 또는 XSequenceTypes일 수 있습니다. 함수 이름, 리턴 유형, 인수 유형은 함수를 고유하게 식별해야 합니다.

    표 1. XStaticContext declareFunction 메소드.

    다음 표는 declareFunction 메소드의 각 양식을 사용할 시점을 설명합니다.

    메소드 서명 목적
    declareFunction(QName name, QName type, QName[] argTypes) 함수의 리턴값 및 인수가 모두 단일 원자 값인 경우 사용

    QNames 유형은 XStaticContext 인스턴스를 작성하기 위해 사용되는 XFactory 인스턴스에 등록된 스키마에 선언된 기본 유형 또는 글로벌 유형을 참조해야 합니다. QName이 원자 외 유형을 참조하면 프로세서는 이를 유형 요소(*, ns:type)로 처리합니다. 여기서, ns:type은 지정된 QName입니다. XTypeConstants 인터페이스에서는 각 기본 유형에 대한 QName 오브젝트를 제공하는 편리한 상수가 사용 가능합니다.

    declareFunction(QName name, XSequenceType type, XSequenceType[] argTypes) 함수의 인수 또는 리턴값이 노드이거나 원자 값 또는 노드의 시퀀스일 때 사용
    다음 XQuery 표현식은 세 개의 함수(2개는 prolog에 선언됨)를 사용합니다.
    declare namespace xs = "http://www.w3.org/2001/XMLSchema";
    declare namespace trig = "http://www.example.org/trigonometry";
    declare function trig:arctan($ratio as xs:double) as xs:double external;
    declare function trig:sin($angle as xs:double) as xs:double external;
    
    <ramps>
    {
        for $ramp in ramps/ramp
        let $angleRadians := trig:arctan($ramp/height div $ramp/base)
        let $angle := trig:toDegrees($angleRadians)
        let $length := $ramp/height div trig:sin($angleRadians)
        return
            element ramp
            {
                $ramp/height,
                $ramp/base,
                element angle { $angle },
                element length { $length }
            }
    }
    </ramps>
    지정된 조회는 xquerySource 소스 오브젝트를 통해 사용 가능하다고 가정하고 다음 코드는 조회를 준비합니다. 조회 자체에 선언되지 않은 함수, trig:toDegrees는 XStaticContext 인스턴스에 선언됩니다.
    // Create the factory
    XFactory factory = XFactory.newInstance();
    
    // Create a new static context
    XStaticContext staticContext = factory.newStaticContext();
    
    // Declare a namespace for the functions
    staticContext.declareNamespace("trig", "http://www.example.org/trigonometry");
    
    // Create a QName for the trig:toDegrees function
    QName toDegreesQName = new QName("http://www.example.org/trigonometry", "toDegrees");
    
    // Declare the function on the static context
    staticContext.declareFunction(toDegreesQName, XTypeConstants.DOUBLE_QNAME, new QName[]{XTypeConstants.DOUBLE_QNAME});
    
    // Create an XQuery executable for the query
    XQueryExecutable executable = factory.prepareXQuery(xquerySource, staticContext);
  2. 외부 함수를 사용하는 XQuery 표현식을 실행하려면 XDynamicContext 인스턴스를 사용하여 함수를 구현하는 Java 메소드를 제공 또는 바인드하십시오.

    외부 함수에 대한 바인딩은 해당 함수에 대한 prolog에 외부 함수 선언을 포함하는 라이브러리 모듈 및 기본 모듈에 대해 사용 가능합니다.

    Java 리플렉션을 사용하여 함수에 대한 java.lang.reflect.Method 오브젝트를 확보하십시오. 메소드가 인스턴스 메소드인 경우 이 함수를 바인드할 때 인스턴스 오브젝트가 필요합니다.

    XQuery 표현식을 실행할 때 사용된 함수에 대한 Java 메소드를 제공하지 않으면 오류가 발생합니다.

    XDynamicContext에는 두 개의 bindFunction 메소드가 있습니다. 각각에는 함수에 대한 구현을 제공할 Java 메소드를 식별하는 메소드 오브젝트 및 함수 이름에 대응하는 QName 오브젝트가 필요합니다.
    표 2. XDynamicContext bindFunction 메소드.

    다음 표는 XDynamicContext bindFunction 메소드의 각 양식을 사용할 시점을 설명합니다.

    메소드 이름 목적
    bindFunction(QName qname, Method method) 정적 메소드를 바인드하는 경우 사용
    bindFunction(QName qname, Method method, Object instanceObject) 인스턴스 메소드를 바인드하는 경우 사용
    다음 예제는 첫 번째 예제에서 준비한 XQuery 표현식을 실행합니다. 이때, 먼저 사용하는 함수에 대한 메소드를 바인드합니다. 이 예제에서 java.lang.Math 클래스의 static atan, sin, toDegrees 메소드는 외부 함수의 구현을 제공하는 데 사용됩니다.
    // Create a new dynamic context
    XDynamicContext dynamicContext = factory.newDynamicContext();
    
    // Retrieve the java.lang.reflect.Method object for the trig:toDegrees function
    Method toDegreesMethod = Math.class.getMethod("toDegrees", Double.TYPE);
    
    // Bind the function to the dynamic context
    dynamicContext.bindFunction(toDegreesQName, toDegreesMethod);
    
    // Create QNames for the trig:arctan and trig:sin functions
    QName arctanQName = new QName("http://www.example.org/trigonometry", "arctan");
    QName sinQName = new QName("http://www.example.org/trigonometry", "sin");
    
    // Retrieve the java.lang.reflect.Method objects for the trig:arctan and trig:sin functions
    // then bind them to the dynamic context
    Method arctanMethod = Math.class.getMethod("atan", Double.TYPE);
    Method sinMethod = Math.class.getMethod("sin", Double.TYPE);
    dynamicContext.bindFunction(arctanQName, arctanMethod);
    dynamicContext.bindFunction(sinQName, sinMethod);
    
    // Create an XML input document
    String xml = "<ramps>" +
    "<ramp><base>4</base><height>4</height></ramp>" +
    "<ramp><base>4</base><height>3</height></ramp>" +
    "<ramp><base>10</base><height>2</height></ramp>" +
    "</ramps>";
    StreamSource source = new StreamSource(new StringReader(xml));
    
    // Execute the query
    XSequenceCursor result = executable.execute(source, dynamicContext);
     
    // Serialize the result to System.out
    result.exportItem(new StreamResult(System.out));

주제 유형을 표시하는 아이콘 태스크 주제



시간소인 아이콘 마지막 업데이트 날짜: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_funcs_xquery
파일 이름:txml_funcs_xquery.html