XFactory 类
这是 XFactory 类提供的方法的快速概述。在信息中心的其他文章中会详细讨论各个方法。
XFactory 类是为 XPath、XQuery 和 XSLT 创建可执行文件的主要工厂类。此类还可用于为其他类和工厂(例如 XStaticContext、XDynamicContext、XItemFactory 和 XSequenceTypeFactory 类)创建实例。XFactory 的实例还维护一组已注册模式,且可为正进行验证或未进行验证。正进行验证的工厂提供模式感知可执行文件,且确保在处理源文档之前根据一组已注册模式对它们进行验证。如果样式表或表达式的不同集合需要不同模式集合,那么通过使用多个 XFactory 实例将它们保持独立。可以通过在 XFactory 类上调用静态 newInstance() 方法创建 XFactory 实例。只要设置保持不变,实例对于线程就是安全的。
以下为使用 XFactory 类的示例。
// Create a new XFactory
XFactory factory = XFactory.newInstance();
// Create an XPath executable
XPathExecutable executable = factory.prepareXPath("/something/bar[2]");
// Create a new XStaticContext
XStaticContext staticContext = factory.newStaticContext();
// Create an XPath executable that is compiled in backwards
// compatibility mode
staticContext.setXPathCompatibilityMode(XStaticContext.XPATH1_0_BC_COMPATIBILITY);
XPathExecutable bcExecutable = factory.prepareXPath("/something/bar[2]", staticContext);
// Set validating
factory.setValidating(true);
// Register a schema
factory.registerSchema(new StreamSource("myschema.xsd"));
// Create a schema aware XPath executable
staticContext = factory.newStaticContext();
staticContext.declareNamespace("something", "http://myschema/something");
XPathExecutable schemaExecutable =
factory.prepareXPath("/something:something/bar[2] instance of element(bar, something:barType)", staticContext);
// Get the item factory for this XFactory (the two are related
// since the item factory depends on the schemas that are
// registered with XFactory).
XItemFactory itemFactory = factory.getItemFactory();