2

I am using xjc to create JAXB classes. I am using the following command

xjc -d src -p com.abc.proj the-types.xsd

I am getting the following error

parsing a schema...
[ERROR] s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xmlns:xsi' is not a valid value for 'NCName'.
  line 106 of file:/C:/Port/Field/the-types.xsd

[ERROR] src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration.
  line 106 of file:/C:/Port/Field/the-types.xsd

[ERROR] s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xsi:noNamespaceSchemaLocation' is not a valid value for 'NCName'.
  line 107 of file:/C:/Port/Field/the-types.xsd

[ERROR] src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration.
  line 107 of file:/C:/Port/Field/the-types.xsd

Failed to parse a schema.

Line 106 and 107 starting with **<xs:attribute name="" ..>**of the-types.xsd file are giving errors. The .xsd file is:

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

         <xs:element name="the-types">
               <xs:complexType>
                     <xs:sequence>
                           <xs:element name="AType" maxOccurs="unbounded">

                            ...............


                           </xs:element>
                     </xs:sequence>
                     **<xs:attribute name="xmlns:xsi" type="xs:string"></xs:attribute>
                     <xs:attribute name="xsi:noNamespaceSchemaLocation" type="xs:string"></xs:attribute>**
               </xs:complexType>
         </xs:element>
   </xs:schema>

What I understand from the link Invalid attribute value for 'name' in element 'element' that we cannot use : in the name. But this doesn't help. How should I change my xsd in order to get the JAXB classes.

Community
  • 1
  • 1
likeGreen
  • 1,001
  • 1
  • 20
  • 40

2 Answers2

3

Well the attributes you tried to create are incorrect. The name for an attribute must be an NCName (i.e. non-colonized name), so the values you have cannot be used (see here for more info). Looking at the attributes you are trying to define, I can tell you that you do not need to define these in your schema. That's because they are already defined in other schemas (in this case the XML Schema Instance schema). So these attributes can be added to the XML document. Like this:

<the-types>
   <AType xsi:schemaLocation="location.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</the-types>
Simeon G
  • 1,188
  • 8
  • 20
  • 1
    One of them isn't even an attribute and cannot be defined at all. – Abel Sep 15 '15 at 19:08
  • That's right. xsi would be a namespace declaration. The question is difficult to answer because what @Pan is trying to do is very unclear. – Simeon G Sep 15 '15 at 19:09
  • 2
    Perhaps he received his XSD from elsewhere, this happens often: you just want to create the POCO's or POJO's, from the schema and then you are hit with an error, at no clue what to do next. – Abel Sep 15 '15 at 19:25
2

[ERROR] s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xmlns:xsi' is not a valid value for 'NCName'.

<xs:attribute name="xmlns:xsi" type="xs:string"></xs:attribute>
<xs:attribute name="xsi:noNamespaceSchemaLocation" type="xs:string"></xs:attribute>

This is indeed not legal. You seem to be trying two things here:

  • define the attribute xmlns:xsi. This is not an attribute (though it looks like one). Anything starting with xmlns: is a namespace declaration and defines a namespace for the prefix following it.
  • define the attribute xsi:noNamespaceSchemaLocation. This is reserved for XSI and should not be separately specified in your XSD. When you need this attribute to define the location of your XSD for no-namespace elements, just define the XSI namespace when you want to use it.

You don't need to declare either of these. They magically exist and are understood by all standards-compliant XSD validators. They are reserved (though it is allowed to declare the XSI attribute, you shouldn't try to, as it may override standards behavior, but more likely, it will simply be ignored)

The error claims they are not NCName. That is correct. And NCName does not contain a colon. It means, you can only define the local part of a name.

Update (forgot about your last line)

But this doesn't help. How should I change my xsd in order to get the JAXB classes.

Make sure that your XSD is valid (fix the above two errors by removing those lines) and you should be fine. You can still use the attributes, and JAXB will understand it if you do. In fact, JAXB expects and requires you to do so, unless you tell it otherwise where the schema is located when validating XML.

If you want to understand how namespaces interact with your schema design, this article on XFront is a good read: Zero, One or Many namespaces. It will help you understand namespace concepts with regards to XSD design and why you can only use NCName in names.

Abel
  • 56,041
  • 24
  • 146
  • 247