使用 xjc 解析 xsd 时出错 [英] Error while parsing xsd using xjc

查看:26
本文介绍了使用 xjc 解析 xsd 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xjc 解析以下 xsd

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>

我收到错误:Recorded reason: cvc-datatypevalid.1.2.1: 'xsi:schemaLocation' is not'NCName' 的有效值.

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

推荐答案

好吧,这正是它所说的(我的意思是错误).字符串 xsi:schemaLocation 不能是属性的名称.但是您在这一行中指定了它:

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" />

那是因为,根据 XML,xsi: 应该是命名空间前缀,因此,实际上,它是 XML 标记的一部分,因此不能是任何纯 XML 名称(元素或属性的)的一部分.

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).

如果删除该行中的 xsi:,如下所示:

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

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

它将被毫无问题地解析.但是,问题仍然存在:xsi: 前缀是什么?你在哪里得到它?也许以后整个 XML 模式项目中的某个地方都需要它?

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?

所以,我认为,您应该更好地理解所有 XML/XSD 内容...我推荐这本 O'Reilly 的书:XML 架构:W3C 的面向对象的 XML 描述

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

我已经对此进行了更多的研究......而且它更深入.这里还有一个与您的问题相关的问题:xsi:schemaLocation 的用途是什么?

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?

xsi:schemaLocation 是 W3C 预定义命名空间中的全局属性:http://www.w3.org/2001/XMLSchema-instance

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

所以,如果你想使用那个属性,你需要先导入那个命名空间,然后,在您的架构中定义对 xsi:schemaLocation 的引用.这将如下所示:

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>

注意发生了什么变化:

  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 已添加到 标记中以将 xsi 前缀与该命名空间绑定.
  • 命名空间定义(即定义它的模式)使用元素 <xsd:import namespace="http://www.w3.org/2001/XMLSchema-instance"/>
  • 您旧的本地属性声明 <xsd:attribute name="xsi:schemaLocation" type="xsd:string"/> 被替换为全局属性引用:<xsd:attribute ref="xsi:schemaLocation"/>
  • 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.

但请确保您的 XML 模式解析器(即xjc")知道从哪里获取http://www.w3.org/2001/XMLSchema-instance 命名空间的 XML 模式.最有可能的是,确实如此.实际上,该模式正好由命名空间 URI 中表示的 URL 定位,因此可以从那里自动下载.(但是,许多使用 XML 模式的现代软件通常都保存此类内容的本地副本.)

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.)

这篇关于使用 xjc 解析 xsd 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆