使用整理
XSLT 样式表和 XQuery 和 XPath 中的表达式可使用整理 URI 引用整理。整理是一组特定于文化的规则,这些规则定义存储文本的方式,以及两个文本部分之间的哪些差异重要,哪些差异不重要。
开始之前
此文假定您已熟悉 java.util.Locale 和 java.text.Collator 类的基本知识。
关于此任务
处理器不会以任何方式解释整理 URI,它会仅将整理 URI 视为一种 Java™ Collator 类的实例名称,此类与 URI 关联。XML API 提供机制,用于指定准备时哪些是缺省整理 URI,以及用于在执行时将 Java Collator 类的实例与整理 URI 关联。
所有通过 XML API 指定的整理 URI 必须为绝对 URI 引用。 在 XSLT 样式表、XQuery 表达式或 XPath 表达式中,将针对来自该表达式的静态上下文的基本 URI,对任何相对 URI 引用(在需要整理 URI 的上下文中使用)进行解析,将确保即使是样式表或表达式中的相对 URI 引用,也可与通过 XML API 指定的绝对 URI 引用匹配。
- 如果整理 URI 绑定到 Java Collator 类的实例,且此实例不是 java.text.RuleBasedCollator 的实例,那么不允许对该整理 URI 执行某些操作。尤其是,此整理 URI 不支持 fn:starts-with,、fn:ends-with、fn:contains、fn:substring-before 和 fn:substring-after 函数。
- 当前包含在 Java 运行时环境中的 Collator 的所有实例也是 java.text.RuleBasedCollator 的实例,这样在大多数情况下,这仅是一个理论限制。但是,应该注意以下情况:应用程序定义其自己的 Java Collator 类的实例,或定义那些同时不是 java.text.RuleBasedCollator 实例的 Collator 类的子类。
过程
- 声明缺省整理 URI。
您可以通过在 XStaticContext 接口上使用 setDefaultCollation 方法,指定要用作字符串比较运算缺省值的整理 URI。在不显式指定整理 URI 的字符串比较运算中,来自 XStaticContext 接口的缺省整理 URI 将用作整理 URI。
XQuery 表达式可使用声明缺省整理声明,覆盖 XStaticContex 接口上指定的缺省整理 URI。相似地,XSLT 样式表可使用 [xsl:]default-collation 属性,覆盖缺省整理 URI。 XPath 不会提供覆盖缺省整理 URI 的方式。但是,任何执行字符串比较运算的 XPath 或 XQuery 表达式或 XSLT 样式表可指定显式整理 URI,以覆盖缺省整理 URI。
如果未在 XStaticContext 接口的任何实例上指定缺省整理,且此实例是在您准备 XSLT 样式表或 XQuery 或 XPath 表达式时提供的,那么样式表或表达式的缺省整理 URI 将为 Unicode 代码点整理 URI:http://www.w3.org/2005/xpath-functions/collation/codepoint/。
您可以在以下情况下使用 Unicode 代码点整理:字符数必须与要视为相等的 Unicode 字符数相同。 此整理定义的字典排序由字符的 Unicode 代码点确定,即由其在 Unicode 代码图表上的位置确定。因此,相对于使用以特定于文化的方式执行字符串比较的整理,使用 Unicode 代码点整理将获得更好的性能,但对于排序操作,很可能无法获得很满意的结果。
以下是一个简单示例,此示例显示如何在 XStaticContext 接口的实例上指定缺省整理 URI。// Setting of default collation URI is not changed - default remains // the Unicode code point collation URI XFactory factory = XFactory.newInstance(); XPathExecutable maxPath1 = factory.prepareXPath("max($var)"); // A new default collation URI is specified in the static context // That URI is used in any string comparison for which no other // explicit collation URI is specified XStaticContext sc = factory.newStaticContext(); sc.setDefaultCollation("http://example.org/my-collation"); XPathExecutable maxPath2 = factory.prepareXPath("max($var)", sc);
- 绑定整理 URI。
XML API 提供两种方式,用于将整理 URI 与 Java Collator 类的实例绑定以执行操作。 XDynamicContext 方法上的 bindCollation 方法具有两个自变量:第一个自变量是整理 URI;另一个自变量是 java.text.Collator 类的实例或 java.util.Locale 类的实例。如果指定了语言环境类的实例,那么处理器将使用适用于此语言环境的 Collator 类的实例。
XSLT、XPath 和 XQuery 定义“静态已知整理”的概念。 如果整理 URI 的引用显示在 XSLT 样式表或 XPath 或 XQuery 表达式中,且整理 URI 不是其中一个静态已知整理,那么在某些情况下应该会报告静态错误。但是,处理器会将所有整理 URI 视为好像它们处于静态已知整理集合中。这是由于执行时间之前,Java Collator 类的实例实际上不与整理 URI 关联,因此处理器无法静态确定哪些 URI 为未知。相反,如果在样式表或表达式中使用整理 URI,且该 URI 未绑定到 Collator 类的实例,那么处理器将报告动态错误。
您无法将 Unicode 代码点整理 URI 绑定到 Java Collator 类的任何实例。此 URI 始终与 Unicode 代码点整理隐式绑定。
以下示例说明您可如何将整理 URI 绑定到 XDynamicContex 接口的实例上的 Java Collator 类的特定实例。XFactory factory = XFactory.newInstance(); XStaticContext sc = factory.newStaticContext(); // Set up a default collation URI sc.setDefaultCollation("http://example.org/my-collation"); // Prepare an XPath expression that computes fn:max() using the // collator associated with the default collation URI and again using // the Unicode code point collation String expr = "max($var)," + "max($var,'http://www.w3.org/2005/xpath-functions/collation/codepoint')"; XPathExecutable maxPath = factory.prepareXPath(expr, sc); XDynamicContext dc = factory.newDynamicContext(); // Set the value of the variable $var dc.bind(new QName("var"), new String[] {"encyclopaedia", // U+00E6 is lower case latin ae ligature "encyclop\u00E6dia", "encyclopedia"}); // Set up a Collator for English that does not distinguish between // capitals, lower-case letters and certain character variants Collator english = (Collator) Collator.getInstance(Locale.ENGLISH).clone(); english.setStrength(Collator.SECONDARY); // Evaluate the expression with that English collator associated with // the default collation URI dc.bindCollation("http://example.org/my-collation", english); XSequenceCursor maxValues = maxPath.execute(dc); // Print maximum values - expected results are // encyclopedia for English collation and // encyclop\u00E6dia for Unicode code point collation if (maxValues != null) { do { System.out.println(maxValues.getStringValue()); } while (maxValues.toNext()); }


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