Error while parsing xsd using xjc

白昼怎懂夜的黑 提交于 2019-12-12 01:13:36

问题


I am parsing following xsd using xjc

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Response">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Content">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Content1">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="Type" type="xsd:string" />
                    <xsd:element name="ID" type="xsd:string" />
                    <xsd:element name="CreationDate" type="xsd:dateTime" />
                    <xsd:element name="LastModified" type="xsd:dateTime" />
                    <xsd:element name="PublicationDate">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="Start" type="xsd:dateTime" />
                          <xsd:element name="End" type="xsd:dateTime" />
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="Content2">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="Type" type="xsd:string" />
                          <xsd:element name="Lang" type="xsd:string" />
                          <xsd:element name="Subject" type="xsd:string" />
                          <xsd:element name="TextContent" type="xsd:string" />
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
      <xsd:attribute name="xsi:schemaLocation" type="xsd:string" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

I am getting error: Recorded reason: cvc-datatypevalid.1.2.1: 'xsi:schemaLocation' is not a valid value for 'NCName'.


回答1:


Well, it is exactly what it says (the error, I mean). The string xsi:schemaLocation cannot be the name of an attribute. But you have it specified so in this line:

<xsd:attribute name="xsi:schemaLocation" type="xsd:string" />

That's because, according to XML, the xsi: is supposed to be a namespace prefix, so, effectively, it is a part of XML markups and, therefore, cannot be part of any pure XML name (of an element or attribute).

If you remove xsi: in that line, like this:

<xsd:attribute name="schemaLocation" type="xsd:string" />

it will be parsed without problem. But then, the question remain: What was that xsi: prefix for? Where did you get it? Maybe it will be needed later somewhere in the whole your XML schema project?

So, I think, you should better understand all that XML/XSD stuff... I suggest this O'Reilly book: XML Schema: The W3C's Object-Oriented Descriptions for XML


I've looked into this a bit more.... and it goes deeper. There is also a question related to your problem here: what is the use of xsi:schemaLocation?

That xsi:schemaLocation is a global attribute in the W3C-predefined namespace: http://www.w3.org/2001/XMLSchema-instance

So, if you want to use that attribute, you need to import that namespace first, and, then, to define a reference to xsi:schemaLocation in your schema. That will look like the following:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsd:import namespace="http://www.w3.org/2001/XMLSchema-instance"/>
  <xsd:element name="Response">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Content">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Content1">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="Type" type="xsd:string" />
                    <xsd:element name="ID" type="xsd:string" />
                    <xsd:element name="CreationDate" type="xsd:dateTime" />
                    <xsd:element name="LastModified" type="xsd:dateTime" />
                    <xsd:element name="PublicationDate">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="Start" type="xsd:dateTime" />
                          <xsd:element name="End" type="xsd:dateTime" />
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="Content2">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="Type" type="xsd:string" />
                          <xsd:element name="Lang" type="xsd:string" />
                          <xsd:element name="Subject" type="xsd:string" />
                          <xsd:element name="TextContent" type="xsd:string" />
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
      <xsd:attribute ref="xsi:schemaLocation"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Notice what has changed:

  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" was added in the <xsd:schema> tag to bind xsi prefix with that namespace.
  • The namespace definition (that is schema defining it) is imported with the element <xsd:import namespace="http://www.w3.org/2001/XMLSchema-instance"/>
  • Your old local attribute declaration <xsd:attribute name="xsi:schemaLocation" type="xsd:string"/> is replaced with the global attribute reference: <xsd:attribute ref="xsi:schemaLocation"/>

Now, it should work.

But make sure your XML schema parser (that 'xjc') knows where to get the XML schema for the http://www.w3.org/2001/XMLSchema-instance namespace. Most likely, it does. Actually, that schema is located exactly by that URL denoted in the namespace URI, so it can be downloaded automatically from there. (However, many modern software working with XML schemas typically hold local copies of such things.)




回答2:


As ColdFusion has already observed, the error message "Recorded reason: cvc-datatypevalid.1.2.1: 'xsi:schemaLocation' is not a valid value for 'NCName'" is pretty explicit about the problem. Your XSD schema document includes the declaration

<xsd:attribute name="xsi:schemaLocation" 
               type="xsd:string" />

but the value of the name attribute must be an NCName -- colons and namespace prefixes are not allowed.

ColdFusion is wrong, however, to suggest that you need to import the xsi namespace into your schema: you do not need to, and doing so will have no effect, because attributes in the xsi namespace are handled specially by schema validators; they are not validated against declarations from the schema being used for validation. (There is no particular harm in having declarations for them, but those declarations won't be used.)



来源:https://stackoverflow.com/questions/16960038/error-while-parsing-xsd-using-xjc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!