在 J2EE 上下文中使用解析器

在 Java™ 2 Platform, Enterprise Edition (J2EE) 上下文中加载工件时,您应考虑从本地部署工件加载资源时应用的特殊暗示。

关于此任务

通过 Class.getResource 和 Class.getResourceAsStream 从 J2EE 部署工件(如 EAR、WAR 和库 JAR 文件)加载本地资源,可能会在加载相关 XML 工件时带来问题。使用这些机制加载初始本地资源将成功,但是通常情况下,如果不进行具体考虑,从初始资源加载的工件将无法加载。

这是在执行时使用 XPath fn:doc 函数从样式表加载文档的示例。在此示例中,缺省行为是基于静态上下文中的基本 URI 解析文档。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  
	<xsl:variable name="names" select="doc('names.xml')"/>
	
	<xsl:template name="loadnames">
		<xsl:copy-of select="$names"/>
	</xsl:template>
	
</xsl:stylesheet>
如果这是在 J2EE 环境中使用以下 Java 代码加载的,那么示例如下:
// create the factory
XFactory factory = XFactory.newInstance();

// load the XSLT file from a local resource within a J2EE deployment artifact
StreamSource source = new StreamSource(XSLTDocFunction.class.getResourceAsStream("/samplexslts/doc.xsl"));

// Create an XSL transform executable
XSLTExecutable xslTransform = factory.prepareXSLT(source);
        
// Create the result
Result result = new StreamResult(new ByteArrayOutputStream());
        
// Create a dynamic context specifying the XSLT initial template
XDynamicContext dc = factory.newDynamicContext();
dc.setXSLTInitialTemplate(new QName("loadnames"));
        
// Execute the transformation
xslTransform.execute(dc, result);
您将收到以下错误:
IXJXE0364W: FATAL ERROR:  IXJXE0774E: [ERR 0693][ERR FODC0005] 
The URI string 'names.xml' does not map to an available document.

发生此错误的原因是加载了初始 XML 工件 (doc.xsl),但未为基本 URI 建立静态内容。在此示例中,处理器将回滚以查看当前工作目录,这在 J2EE 环境中没有意义。

有三种方式可用来解决此情况。以下是前两种方式:
  • 在静态上下文中设置 baseURI。
    调整为在静态上下文上设置 baseURI 将类似此示例:
    // create the factory
    XFactory factory = XFactory.newInstance();
            
    // set the baseURI in the static context
    URL dataURL = XSLTSchemaAware.class.getResource("/samplexslts/doc.xsl");
    XStaticContext staticContext = factory.newStaticContext();
    staticContext.setBaseURI(dataURL.toString());
    
    // load the XSLT file from a local resource within a J2EE deployment artifact        
    StreamSource source = new StreamSource(XSLTDocFunction.class.getResourceAsStream("/samplexslts/doc.xsl"));
    
    // Create an XSL transform executable
    XSLTExecutable xslTransform = factory.prepareXSLT(source, staticContext);
            
    // Create the result
    Result result = new StreamResult(new ByteArrayOutputStream());
    
    // Create a dynamic context specifying the XSLT initial template
    XDynamicContext dc = factory.newDynamicContext();
    dc.setXSLTInitialTemplate(new QName("loadnames"));
            
    // Execute the transformation
    xslTransform.execute(dc, result);
  • 以允许处理器知道 baseURI 的方式加载资源。
    通过将绝对 URI 传递到 StreamSource 构造函数,调整为加载资源(且方式为允许处理器知道 baseURI)类似如下示例:
    // Create the factory
    XFactory factory = XFactory.newInstance();
            
    // Create the source from a URL
    URL dataURL = XSLTSchemaAware.class.getResource("/samplexslts/doc.xsl");
    StreamSource source = new StreamSource(dataURL.toString());
    
    // Create an XSL transform executable for the expression
    XSLTExecutable xslTransform = factory.prepareXSLT(source);
            
    // Create the result
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Result result = new StreamResult(baos);
            
    // Create a dynamic context specifying the XSLT initial template
    XDynamicContext dc = factory.newDynamicContext();
    dc.setXSLTInitialTemplate(new QName("loadnames"));
            
    // Execute the transformation
    xslTransform.execute(dc, result);
当所有 XML 工件位于单个 J2EE 部署单元中时,以上描述的两种方法将正常可用,但如果 XML 工件跨 J2EE 部署单元拆分,那么它们将失败,这是因为没有针对所有 XML 工件的单个 baseURI。

要对 XML 工件加载进行完全控制,以支持某些情况(如工件跨多个部署单元),请使用解析器以完全控制加载行为。

过程

实现相应的解析器,并在适当的时候向静态或动态上下文注册这些解析器。

这些 XML 工件加载建议在解析器支持的所有 XML 工件的 J2EE 上下文中适用,如 XML 文档 (fn:doc(), document())、样式表 (xsl:include, xsl:import)、未解析的文本以及 XML 模式 (XSLT import-schema)。


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



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