使用未解析的文本解析器

可以实现 XUnparsedTextResolver 接口,且向 XDynamicContext 注册实现以覆盖通过 XSLT unparsed-text 函数加载的资源的缺省解析行为。

关于此任务

通过 XSLT unparsed-text 函数加载的资源的缺省源解析行为是基于静态上下文中的基本 URI 解析相对 URI。如果基本 URI 不可用,那么会使用当前工作目录。使用绝对 URI 保持不变。

过程

在 XDynamicContext 接口上使用 setUnparsedTextResolver 方法,以注册未解析的文本解析器。

示例

以下为使用未解析的文本解析器的基本示例。
XFactory factory = XFactory.newInstance();

// Prepare the stylesheet.
XSLTExecutable executable = factory.prepareXSLT(new StreamSource(stylesheetFile));
           
// Create the dynamic context and set the unparsed text resolver.
XDynamicContext dynamicContext = factory.newDynamicContext();
AnUnparsedTextResolver resolver = new AnUnparsedTextResolver(replacementBase);
dynamicContext.setUnparsedTextResolver(resolver);
           
// Execute the transformation.
Source source = new StreamSource(inputFile);
Result result = new StreamResult(System.out);
executable.execute(source, dynamicContext, result);
以下为未解析的文本解析器实现的基本示例。
class AnUnparsedTextResolver implements XUnparsedTextResolver
{
    String _replacementBase;
       
    public AnUnparsedTextResolver(String replacementBase)
    {
        _replacementBase=replacementBase;
    }
       
    // Resolve URI, returning the resource that URI represents.
    // Implements the "rebase:" pseudo-scheme.
    public String getResource(String href, String encoding, String base) {
        String rebasePrefix="rebase:";

        if (href.startsWith(rebasePrefix)) {
            href = href.substring(rebasePrefix.length());
            base = _replacementBase;
        }
           
        try {
            // Get base URI object
            URI uri = new java.net.URI(base);
            // Resolved relative reference against base URI
            URI resolvedURI = uri.resolve(href);
            // Try to read...
            URL url = resolvedURI.toURL();
            URLConnection urlCon = url.openConnection();
            BufferedInputStream stream = new BufferedInputStream(urlCon.getInputStream());
            StringBuffer buffer = new StringBuffer();
            int s;
            while ((s = stream.read())!= -1) {
                // Do any character manipulation here.
                buffer.append((char)s);
            }
            return buffer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_resolvers_text
文件名:txml_resolvers_text.html