将外部变量用于 XPath
使用 XPath 表达式(使用外部变量)时,使用 XDynamicContext 实例为每个变量提供(绑定)值,并可以选择使用 XStaticContext 实例声明变量的类型。
过程
- 准备使用外部变量的 XPath 表达式时,使用 XStaticContext 实例声明变量的类型。
此步骤是可选的。如果未声明变量,那么处理器会假定其类型为 item()*。换言之,变量的值可以为包含任何类型的项的任何长度序列。声明变量的类型可帮助处理器在准备期间静态检测某些使用错误。
XStaticContext 接口具有两种 declareVariable 方法,每种方法具有两个参数,一个参数对应名称,一个参数对应变量类型。始终将名称作为 QName 对象提供,但类型可以为 QName 或 XSequenceType。表 1. XStaticContext declareVariable 方法. 下表说明何时使用各种格式的 declareVariable 方法。
方法特征符 用途 declareVariable(QName name, QName type) 用于变量的值为单个原子值的情况 QName 必须引用模式中声明的内置类型或全局类型,此模式已在用于创建 XStaticContext 实例的 XFactory 实例上注册。如果 QName 引用非原子类型,那么处理器会将变量视为具有类型 element(*, ns:type),其中 ns:type 是给定的 QName。XTypeConstants 接口具有可用的方便常量,这些常量为各种内置类型提供 QName 对象。
declareVariable(QName name, XSequenceType type) 用于在变量的值为单个节点、原子值序列或节点序列的情况 以下示例说明如何准备使用变量的 XPath 表达式,其中两个变量在静态上下文中声明,一个变量未声明。// Create the factory XFactory factory = XFactory.newInstance(); // Create a new static context from the factory XStaticContext staticContext = factory.newStaticContext(); // Define QNames for the names of the variables to be declared final QName taxRate = new QName("taxRate"); final QName partNumbers = new QName("partNumbers"); // Declare a variable called "taxRate" as an xs:float staticContext.declareVariable(taxRate, XTypeConstants.FLOAT_QNAME); // Obtain an XSequenceTypeFactory instance from the factory XSequenceTypeFactory typeFactory = factory.getSequenceTypeFactory(); // Define a sequence type for a sequence of xs:integer values XSequenceType integerSequence = typeFactory.atomic(XTypeConstants.INTEGER_QNAME, XSequenceType.OccurrenceIndicator.ZERO_OR_MORE); // Declare a variable called "partNumbers" as a sequence of xs:integer values staticContext.declareVariable(partNumbers, integerSequence); // Create an XPath expression that uses the declared variables, as well as another variable "discount" // that the processor will assume has type item()* String expression = "sum(for $partNumber in $partNumbers return /inventory/part[@num=$partNumber]/@price) * (1 - $discount) * (1 + $taxRate)"; // Prepare the XPath expression XPathExecutable xpath = factory.prepareXPath(expression, staticContext);
- 要执行使用外部变量的 XPath 表达式,请使用 XDynamicContext 实例提供(或绑定)每个变量的值。
如果没有为执行 XPath 表达式时使用的变量提供值,那么会发生错误。
XDynamicContext 具有若干 bind、bindItem 和 bindSequence 方法。每个方法都有两个参数,其中第一个参数是对应于参数名称的 QName 对象,第二个参数是值。表 2. XDynamicContext bind、bindItem 和 bindSequence 方法. 下表说明何时使用各种格式的 XDynamicContext bind、bindItem 和 bindSequence 方法。
方法 用途 bind 在绑定单个原子值时使用 对于内置类型到 Java™ 类型的标准映射中使用的每种 Java 类型,都存在一种格式的此方法。还具有两种其他格式,一种格式采用节点,一种格式采用源。这两种格式分别用于从 DOM 树绑定任何节点以及解析新的源代码以生成文档节点。
bindItem 在将单个项作为 XItemView 对象绑定时使用 XItemView 对象可以通过执行其他表达式获得,也可以使用 XItemFactory 实例来构造。
bindSequence 在绑定少于一个项或多于一个项的序列时使用 对于内置类型到 Java 类型的标准映射中使用的每种 Java 类型,都存在一种格式的此方法;每个方法都接受给定类型的值数组。
还存在另一种接受 XSequenceCursor 的格式。XSequenceCursor 可以通过执行其他表达式来生成,也可以使用 XItemFactory 实例来构造。
以下示例执行第一个示例中准备的 XPath 表达式,同时首先绑定它所使用的各个变量的值。// Create a new dynamic context from the factory XDynamicContext dynamicContext = factory.newDynamicContext(); // Bind an atomic value for the "taxRate" and "discount" variables dynamicContext.bind(taxRate, 0.13f); dynamicContext.bind(new QName("discount"), 0.40); // Bind a sequence of atomic values for the "partNumbers" variable dynamicContext.bindSequence(partNumbers, new int[] {2, 1, 2, 3, 2}); // Create an XML input document String xml = "<inventory>" + "<part num='1' price='9.99'/>" + "<part num='2' price='4.47'/>" + "<part num='3' price='12.99'/>" + "</inventory>"; StreamSource source = new StreamSource(new StringReader(xml)); // Execute the expression XSequenceCursor result = xpath.execute(source, dynamicContext);


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=txml_vars_xpath
文件名:txml_vars_xpath.html