Xjc 在 xmlns:xsi 和 xsi:noNamespaceSchemaLocation 的 XSD 属性定义上抛出 "Invalid attribute value, not a valid NCName"

Xjc throws "Invalid attribute value, not a valid NCName" on XSD attribute definitions for xmlns:xsi and xsi:noNamespaceSchemaLocation

我正在使用 xjc 创建 JAXB 类。我正在使用以下命令

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

我收到以下错误

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.

-types.xsd 文件中以 **<xs:attribute name="" ..>** 开头的第 106 行和第 107 行出现错误。 .xsd 文件是:

<?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>

我从 link Invalid attribute value for 'name' in element 'element' 中了解到我们不能在名称中使用 :。但这没有帮助。我应该如何更改 xsd 以获得 JAXB 类.

[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>

这确实不合法。您似乎在这里尝试了两件事:

  • 定义属性xmlns:xsi。这不是一个属性(虽然它看起来像一个)。以 xmlns: 开头的任何内容都是命名空间声明,并为其后的前缀定义命名空间。
  • 定义属性xsi:noNamespaceSchemaLocation。这是为 XSI 保留的,不应在您的 XSD 中单独指定。当您需要此属性为无名称空间元素定义 XSD 的位置时,只需在要使用它时定义 XSI 名称空间即可。

您无需声明其中任何一项。它们神奇地存在,并被所有符合标准的 XSD 验证者理解。它们是保留的(虽然允许声明 XSI 属性,但您不应该尝试这样做,因为它可能会覆盖标准行为,但更有可能的是,它会被忽略

错误声称它们不是 NCName。那是对的。并且 NCName 不包含冒号。这意味着,您只能定义名称的本地部分。

更新(忘了你的最后一行)

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

确保您的 XSD 有效(通过删除这些行来修复上述两个错误)并且您应该没问题。您仍然可以使用属性,如果您这样做,JAXB 会理解它。事实上,JAXB 期望并要求您这样做,除非您在验证 XML.

时以其他方式告诉它模式所在的位置

如果您想了解命名空间如何与您的架构设计交互,XFront 上的这篇文章是个不错的读物:Zero, One or Many namespaces。它将帮助您了解有关 XSD 设计的命名空间概念以及为什么您只能在名称中使用 NCName

嗯,您尝试创建的属性不正确。属性的名称必须是 NCName(即非殖民名称),因此不能使用您拥有的值(有关详细信息,请参阅 here)。查看您尝试定义的属性,我可以告诉您,您不需要在架构中定义这些属性。那是因为它们已经在其他模式中定义(在本例中为 XML 模式实例模式)。所以这些属性可以添加到XML文件中。像这样:

<the-types>
   <AType xsi:schemaLocation="location.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</the-types>