java JAXB XJC - XPath 评估结果为空目标节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7261322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:19:46  来源:igfitidea点击:

JAXB XJC - XPath evaluation results in empty target node?

javaxpathxsdjaxbxjc

提问by maerics

I've got the following simple XSD document (foo.xsd):

我有以下简单的 XSD 文档 ( foo.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:foo">
  <xsd:element name="Person">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Name" type="xsd:string"/>
        <xsd:element name="Height">
          <xsd:simpleType>
            <xsd:restriction base="xsd:string">
              <xsd:enumeration value="Short"/>
              <xsd:enumeration value="Average"/>
              <xsd:enumeration value="Tall"/>
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

And I'd like to hint to the XJC JAXB compiler that the "Height" element should use a type safe enum class by using an external bindings file, like so (foo.xjb):

我想向 XJC JAXB 编译器提示“高度”元素应该通过使用外部绑定文件来使用类型安全的枚举类,如下所示 ( foo.xjb):

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xsd="http://www.w3c.org/2001/XMLSchema"
              jxb:version="2.0">
  <jxb:bindings schemaLocation="foo.xsd">
    <jxb:bindings node="//xsd:element[@name='Height']/xsd:simpleType">
      <jxb:typesafeEnumClass name="Height" />
    </jxb:bindings>
  </jxb:bindings>
</jxb:bindings>

But when I run the command "xjc -b foo.xjb foo.xsd" I get the following error:

但是当我运行命令“ xjc -b foo.xjb foo.xsd”时,出现以下错误:

parsing a schema...
[ERROR] XPath evaluation of "//xsd:element[@name='Height']/xsd:simpleType" results in empty target node
  line 6 of file:/Users/maerics/src/java/jaxb/foo.xjb

Failed to parse a schema.

The XPath expression looks fine to me so I'm guessing there is some subtle problem related to XML namespaces? I've tried a few combinations of using (or not) a default namespace, targetNamespace, etc. but always the same error. Note that xjcgenerates Java source for the XSD file by itself, without the external bindings file, as expected. Similarly, using embedded binding definitions in the XSD file works as expected.

XPath 表达式对我来说看起来不错,所以我猜有一些与 XML 名称空间相关的微妙问题?我尝试了使用(或不使用)默认命名空间、targetNamespace 等的几种组合,但总是出现相同的错误。请注意xjc,如预期的那样,它自己为 XSD 文件生成 Java 源代码,没有外部绑定文件。类似地,在 XSD 文件中使用嵌入的绑定定义按预期工作。

Note that I am using Java version "1.6.0_26" and xjc version "JAXB 2.1.10 in JDK 6" on Mac OS 10.6.8.

请注意,我在 Mac OS 10.6.8 上使用 Java 版本“1.6.0_26”和 xjc 版本“JDK 6 中的 JAXB 2.1.10”。

Can someone explain how to achieve this goal without modifying the original XSD?

有人可以解释如何在不修改原始 XSD 的情况下实现这一目标吗?

回答by BobG

Heh, you're going to kick yourself when you see the problem:

呵呵,看到问题你要自责了:

In foo.xsd, you have this:

在 foo.xsd 中,你有这个:

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

In foo.xjb, you have this:

在 foo.xjb 中,你有这个:

xmlns:xsd="http://www.w3c.org/2001/XMLSchema"

Note "w3" vs. "w3c". Those two attributes need to match exactly, and then your XPath will work (otherwise the namespace referenced in your xjb is distinct from the XSD namespace referenced in your XSD.)

注意“w3”与“w3c”。这两个属性需要完全匹配,然后您的 XPath 才能工作(否则 xjb 中引用的命名空间与 XSD 中引用的 XSD 命名空间不同。)