JAXB绑定文件:名称空间感知节点选择 [英] JAXB bindings file: namespace-aware node selection

查看:104
本文介绍了JAXB绑定文件:名称空间感知节点选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我倾向于使用外部JAXB绑定文件进行Schema-to-Java编译。这很好用,但我确实注意到了一件我开始想知道的事情。它不是特定于JAXB的,更像是一个XPath问题,但上下文有帮助。

I tend to use external JAXB bindings files for my Schema-to-Java compilation. This works just fine, but I did notice one thing that I've started wondering about. It's not really JAXB-specific, more like an XPath question, but the context helps.

假设我们有这个模式:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:test="www.acme.com"
 targetNamespace="www.acme.com"
 elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xs:element name="el1"/>

  <xs:complexType name="testType">
    <xs:sequence>
      <xs:element ref="test:el1"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

复杂类型中的元素引用需要前缀test,绑定到目标命名空间,以找到元素定义。如果我们省略了前缀,架构处理器会抱怨它无法找到它引用的元素。很明显,引用是一个限定名称,架构处理器也知道这一点。

The element reference in the complex type requires the prefix "test", bound to our target namespace, to find the element definition. If we omitted the prefix, a schema processor would complain that it can't find the element it refers. So obviously the reference is a qualified name and a schema processor is aware of this.

现在为XJC采用以下绑定文件 extract : / p>

Now take the following bindings file extract for XJC:

<bindings node="//xs:complexType[@name='testType']">
  <bindings node=".//xs:element[@ref='test:el1']">
    <property name="element1"/>
  </bindings>
</bindings>

复杂类型的绑定很明显。我们按名称选择它, xs 前缀绑定在绑定文件的根目录中(此处未显示)。它可能是 xsd

我的错误是嵌套绑定。在我们的复杂类型节点的上下文中,我们选择 xs:element 节点,其属性 ref 的值试验:EL1 。但这个价值仅仅被视为文本。 XML处理器不知道它应该是一个限定名称并且 test:实际上是绑定到命名空间的前缀声明。

The binding for the complex type is clear. We select it by name, and the xs prefix is bound in the bindings file's root (not shown here). It might as well be xsd.
What bugs me is the nested binding. In the context of our complex type node, we select an xs:element node for which attribute ref has value test:el1. But that value is simply regarded as text. The XML processor has no knowledge of the fact that it's supposed to be a qualified name and test: is actually a prefix declaration bound to a namespace.

现在我知道我在挑剔,但实际的前缀字符串应该没有重要性,只有命名空间URI本身。有人可以将架构中的 test 前缀更改为 acme ,并且它在语义上仍然是相同的模式。但是,我的绑定文件将不再起作用。

Now I know I'm nitpicking, but the actual prefix string should have no importance, only the namespace URI itself. Someone could change the test prefix in the schema to acme and it would still be the same schema semantically. However, my bindings file would no longer work.

那么,有没有办法构建XPath表达式而不依赖于前缀的知识,只有命名空间URI?这显然不是一个大问题,但我对此很好奇。

So, is there any way to construct the XPath expression without relying on knowledge of the prefix, only the namespace URI? It's obviously not a big problem, but I'm curious about this.

推荐答案


有没有构建
XPath表达式的方法,而不依赖于
的前缀知识,只有
命名空间URI?

Is there any way to construct the XPath expression without relying on knowledge of the prefix, only the namespace URI?

如果你谈论属性值,那就是XPath 1.0

If you talk about an attribute value, this is XPath 1.0

.//xs:element[
   namespace::*[
      . = 'www.acme.com'
   ][
      susbtring-before(
         ../@ref,
         ':'
      )
    = name()
   ]
 and
   substring(
      concat(':', @ref),
      string-length(@ref) - 1
   )
 = 'el1'
]

在XPath 2.0中要简单得多:

In XPath 2.0 is much more simple:

.//xs:element[resolve-QName(@ref,.) eq QName('www.acme.com','el1')]

这篇关于JAXB绑定文件:名称空间感知节点选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆