生成使用JAXB的JAVA类时出错:XPath对导致为空的结果进行了评估

huangapple go评论105阅读模式
英文:

Error when generating JAVA classes with JAXB: XPath evaluation of results in empty

问题

我有一个XSD:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xs:schema targetNamespace="http://www.xyz.m" xmlns="http://www.xyz.m" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  3. <xs:element name="N1" minOccurs="0">
  4. <xs:complexType>
  5. <xs:sequence>
  6. <xs:element name="N_A">
  7. <xs:simpleType>
  8. <xs:restriction base="xs:byte">
  9. <xs:minInclusive value="0"/>
  10. <xs:maxInclusive value="3"/>
  11. <xs:totalDigits value="2"/>
  12. </xs:restriction>
  13. </xs:simpleType>
  14. </xs:element>
  15. <xs:element name="N_B" minOccurs="1" maxOccurs="14">
  16. <xs:simpleType>
  17. <xs:restriction base="xs:string">
  18. <xs:enumeration value="5"/>
  19. <xs:enumeration value="6"/>
  20. </xs:restriction>
  21. </xs:simpleType>
  22. </xs:element>
  23. </xs:sequence>
  24. </xs:complexType>
  25. </xs:element>
  26. </xs:schema>

我创建了一个.xjb文件以生成:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <jaxb:bindings
  3. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  5. version="2.1">
  6. <jaxb:bindings node="//xs:schema//xs:complexType[@name='N1']">
  7. <jaxb:class name="N1_XJB" />
  8. </jaxb:bindings>
  9. </jaxb:bindings>

我使用了xjc -b binding.xjb TestXSD.xsd命令,但是我收到了以下错误消息:

  1. [ERROR] XPath evaluation of "//xs:schema//xs:complexType[@name='N1']" results in empty target node line 6

我尝试了在Stackoverflow上找到的所有方法,但我无法生成。

所以我的问题是,.xjb文件缺少了什么部分?

英文:

I have an XSD:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;xs:schema targetNamespace=&quot;http://www.xyz.m&quot; xmlns=&quot;http://www.xyz.m&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;unqualified&quot;&gt;
  3. &lt;xs:element name=&quot;N1&quot; minOccurs=&quot;0&quot;&gt;
  4. &lt;xs:complexType&gt;
  5. &lt;xs:sequence&gt;
  6. &lt;xs:element name=&quot;N_A&quot;&gt;
  7. &lt;xs:simpleType&gt;
  8. &lt;xs:restriction base=&quot;xs:byte&quot;&gt;
  9. &lt;xs:minInclusive value=&quot;0&quot;/&gt;
  10. &lt;xs:maxInclusive value=&quot;3&quot;/&gt;
  11. &lt;xs:totalDigits value=&quot;2&quot;&gt;
  12. &lt;/xs:totalDigits&gt;
  13. &lt;/xs:restriction&gt;
  14. &lt;/xs:simpleType&gt;
  15. &lt;/xs:element&gt;
  16. &lt;xs:element name=&quot;N_B&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;14&quot;&gt;
  17. &lt;xs:simpleType&gt;
  18. &lt;xs:restriction base=&quot;xs:string&quot;&gt;
  19. &lt;xs:enumeration value=&quot;5&quot;/&gt;
  20. &lt;xs:enumeration value=&quot;6&quot;/&gt;
  21. &lt;/xs:restriction&gt;
  22. &lt;/xs:simpleType&gt;
  23. &lt;/xs:element&gt;
  24. &lt;/xs:sequence&gt;
  25. &lt;/xs:complexType&gt;
  26. &lt;/xs:element&gt;
  27. &lt;/xs:schema&gt;

I created an .xjb file to generate:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;jaxb:bindings
  3. xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
  4. xmlns:jaxb=&quot;http://java.sun.com/xml/ns/jaxb&quot;
  5. version=&quot;2.1&quot;&gt;
  6. &lt;jaxb:bindings node=&quot;//xs:schema//xs:complexType[@name=&#39;N1&#39;]&quot;&gt;
  7. &lt;jaxb:class name=&quot;N1_XJB&quot; /&gt;
  8. &lt;/jaxb:bindings&gt;
  9. &lt;/jaxb:bindings&gt;

I used xjc -b binding.xjb TestXSD.xsd command, but I got this ERROR message:

  1. [ERROR] XPath evaluation of &quot;//xs:schema//xs:complexType[@name=&#39;N1&#39;]&quot; results in empty target node line 6

I tried every method I found on Stackoverflow, but I can't generate.

So my question is, what are missing from .xjb file?

答案1

得分: 1

这对我有效:

  1. 您必须从&lt;xs:element name=&quot;N1&quot; minOccurs=&quot;0&quot;&gt;中删除minOccurs。因为该属性会生成以下错误:

[ERROR] s4s-att-not-allowed: 属性'minOccurs'不能出现在元素'element'中。

  1. .xjb文件中,您必须将以下行从:

&lt;jaxb:bindings node=&quot;//xs:schema//xs:complexType[@name=&#39;N1&#39;]&quot;&gt;

更改为:

  1. &lt;jaxb:bindings schemaLocation=&quot;TestXSD.xsd&quot; node=&quot;//xs:schema//xs:element[@name=&#39;N1&#39;]&quot;&gt;
英文:

This work for me:

  1. You must delete minOccurs from &lt;xs:element name=&quot;N1&quot; minOccurs=&quot;0&quot;&gt;. Because this attribute generates the following error:

[ERROR] s4s-att-not-allowed: Attribute &#39;minOccurs&#39; cannot appear in element &#39;element&#39;.

  1. In the .xjb file, you must change the following line from:

&lt;jaxb:bindings node=&quot;//xs:schema//xs:complexType[@name=&#39;N1&#39;]&quot;&gt;

to:

  1. &lt;jaxb:bindings schemaLocation=&quot;TestXSD.xsd&quot; node=&quot;//xs:schema//xs:element[@name=&#39;N1&#39;]&quot;&gt;

huangapple
  • 本文由 发表于 2020年7月28日 20:59:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63134726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定