从 xsd 架构生成源时出现问题

Issue with generating sources from xsd schema

我在为我的项目生成源时遇到以下错误。我已将一些常见类型提取到名为 CommonTypes.xsd 的模式中,但出现以下错误:

org.xml.sax.SAXParseException: src-resolve.4.1: Error resolving component 'nonEmptyString'. It was detected that 'nonEmptyString' has no namespace, but components with no target namesp
ace are not referenceable from schema document 'file:/C:/Workspace/CommonTypes.xsd'. If 'nonEmptyString' is
 intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'nonEmptyString' has no namespace, then an 'import' without a "namespace" attribute should
be added to 'file:/C:/Workspace/lps-performance-calculation-service/pcs-data/src/main/resources/xsd/calc/lps/CommonTypes.xsd'.

以下简单类型在我的 CommonTypes.xsd 架构中定义如下:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:unit="http://www.mywebsite.com/unit"
            xmlns:types="http://www.mywebsite.com/types"
            elementFormDefault="qualified" attributeFormDefault="unqualified"
            targetNamespace="http://www.mywebsite.com//types">
    <!-- import types -->
    <xsd:import namespace="http://www.mywebsite.com/unit"/>
    <!-- other common types -->
    <xsd:simpleType name="nonEmptyString">
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="1"/>
            <xsd:pattern value=".*[^\s].*"/>
        </xsd:restriction>
    </xsd:simpleType>

导致错误的第 241 行如下:

<xsd:complexType name="Message">
        <xsd:simpleContent>
            <xsd:extension base="nonEmptyString">
                <xsd:attribute type="xsd:string" name="code" use="required"/>
                <xsd:attribute name="category" use="required">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:token">
                            <xsd:enumeration value="Error"/>
                            <xsd:enumeration value="Info"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:attribute>
                <xsd:attribute type="xsd:string" name="component" use="required"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>

您知道可能导致错误的原因吗?我已经尝试搜索 Whosebug 并尝试使用 targetNamespace 和 xmlns,但没有成功。

您正在尝试引用具有

的简单类型
  • 名字'nonEmptyString'
  • 命名空间“”

但是在这个XSD中定义了简单类型'nonEmptyString',其中有targetNamespace="http://www.mywebsite.com//types"。所以你应该指的是一个简单的类型

  • 名字'nonEmptyString'
  • 命名空间“http://www.mywebsite.com//types”

你只需要改变这个:

<xsd:extension base="nonEmptyString">

对此:

<xsd:extension base="types:nonEmptyString">

您需要在相应的命名空间中导入您的 nonEmptyString 并通过前缀使该命名空间可引用。

为此,将 xmlns:types="http://www.mywebsite.com/types" 添加到导入架构的 xsd:schema

同时在导入模式的 xsd:import 中提供 namespace。应该是这样的:

<xsd:import
    namespace="http://www.mywebsite.com/types"
    schemaLocation="calc/lps/CommonTypes.xsd"/>

那么您应该能够将您的 nonEmptyString 类型引用为 types:nonEmptyString