Search code examples
javajaxbxjcxml-binding

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


I have an XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.xyz.m" xmlns="http://www.xyz.m" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="N1" minOccurs="0">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="N_A">
            <xs:simpleType>
                <xs:restriction base="xs:byte">
                <xs:minInclusive value="0"/>
                <xs:maxInclusive value="3"/>
                <xs:totalDigits value="2">
                </xs:totalDigits>
            </xs:restriction>
                </xs:simpleType>
            </xs:element>
        <xs:element name="N_B" minOccurs="1" maxOccurs="14">
            <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="5"/>
                <xs:enumeration value="6"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>    

I created an .xjb file to generate:

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

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

[ERROR] XPath evaluation of "//xs:schema//xs:complexType[@name='N1']" 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?


Solution

  • This work for me:

    1. You must delete minOccurs from <xs:element name="N1" minOccurs="0">. Because this attribute generates the following error:

    [ERROR] s4s-att-not-allowed: Attribute 'minOccurs' cannot appear in element 'element'.

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

    <jaxb:bindings node="//xs:schema//xs:complexType[@name='N1']">

    to:

    <jaxb:bindings schemaLocation="TestXSD.xsd" node="//xs:schema//xs:element[@name='N1']">