java 如何在另一个 XSD 中导入 XSD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41606718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:59:22  来源:igfitidea点击:

How to import XSD in Another XSD

javaxsd

提问by deadend

I am attempting to import xsd to another xsd. i am seeing some problems to import. i could not understand the solutions which provided in web. below is my XSD.

我正在尝试将 xsd 导入另一个 xsd。我看到一些需要导入的问题。我无法理解网络中提供的解决方案。下面是我的 XSD。

I have HEADER.xsd. it is common for all other xsd's.

我有 HEADER.xsd。这对于所有其他 xsd 都是常见的。

HEADER.XSD

表头文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="Header" type="reqHeader"/>

  <xs:complexType name="reqHeader">
    <xs:sequence>
      <xs:element name="MsgId" type="xs:string" minOccurs="0"/>
      <xs:element name="MsgDesc" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

MESSAGE1.XSD

消息1.XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:import namespace="" schemaLocation="\resources\xsd\HEADER.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" />

  <xs:element name="Message">
    <xs:complexType>
      <xs:sequence>

        <xs:element name="Header" type="xs:reqHeader" />

        <xs:element name="Body">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="User">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="Name" minOccurs="1"/>
                    <xs:element type="xs:int" name="DOB" minOccurs="1"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Here i am trying to import element called since it is common for all xsd's [MESSAGE1.XSD MESSAGE2.XSD and so on].

在这里,我试图导入被调用的元素,因为它对所有 xsd 的 [MESSAGE1.XSD MESSAGE2.XSD 等] 都很常见。

Exception says : Is not a valid:src-resolve.4.2: Error resolving component 'xs:reqHeader'. It was detected that 'xs:reqHeader' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:/D:/Projects/workspace/Message/resources/xsd/MESSAGE1.xsd'. If this is the incorrect namespace, perhaps the prefix of 'xs:reqHeader' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:/D:/Projects/workspace/Message/resources/xsd/MESSAGE1.XSD'.

异常表示:无效:src-resolve.4.2:解析组件“xs:reqHeader”时出错。检测到“xs:reqHeader”位于命名空间“ http://www.w3.org/2001/XMLSchema”中,但无法从架构文档“file:/D:/Projects/workspace/”中引用来自该命名空间的组件消息/资源/xsd/MESSAGE1.xsd'。如果这是不正确的命名空间,则可能需要更改“xs:reqHeader”的前缀。如果这是正确的命名空间,则应将适当的“import”标记添加到“file:/D:/Projects/workspace/Message/resources/xsd/MESSAGE1.XSD”。

My Project structure is :

我的项目结构是:

/src/com

/src/com

/lib

/lib

/resources/xsd/MESSAGE1.XSD

/resources/xsd/MESSAGE1.XSD

Please someone help me to resolve it.

请有人帮我解决它。

回答by Sourav Purakayastha

Bingo! After fighting a lot with the XSDs I have found an error free way:

答对了!在与 XSD 进行了大量斗争后,我发现了一种无错误的方法:

Here's the code:

这是代码:

  • You need to change the xs:importto xs:include
  • Remove the namespace=""and the xmlns:xs="http://www.w3.org/2001/XMLSchema"from the xs:includeline
  • 您需要将其更改xs:importxs:include
  • 从行中删除namespace=""xmlns:xs="http://www.w3.org/2001/XMLSchema"xs:include

MESSAGE1.xsd

MESSAGE1.xsd

<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:include schemaLocation="header.xsd" />

<xs:element name="Message">
    <xs:complexType>
        <xs:sequence>

            <xs:element name="Header" type="reqHeader" />

            <xs:element name="Body">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="User">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element type="xs:string" name="Name" minOccurs="1" />
                                    <xs:element type="xs:int" name="DOB" minOccurs="1" />
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

HEADER.xsd

表头文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Header" type="reqHeader" />

<xs:complexType name="reqHeader">
    <xs:sequence>
        <xs:element name="MsgId" type="xs:string" minOccurs="0" />
        <xs:element name="MsgDesc" type="xs:string" minOccurs="0" />
    </xs:sequence>
</xs:complexType>

Please modify the XSD location according to your local file's location.

请根据您本地文件的位置修改 XSD 位置。

Hope it helps!

希望能帮助到你!