首页 > 解决方案 > xsd 模式文件 - 显示 elementName 已定义

问题描述

我有两个 XSD 文件,我想拥有两个不同 xsd 文件的元素,它们的名称相同但属性类型不同。

假设下面是 xml1.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.example.com/wm"
    xmlns="http://www.example.com/wm"
  elementFormDefault="qualified">
    <xsd:element name="testEame1">
        <xsd:annotation>
            <xsd:documentation>       test       </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="id" type='xsd:string' minOccurs="1"/>
                <xsd:element name="session" type='xsd:string' minOccurs="1"/>
            </xsd:sequence>
            <xsd:attribute name="pid" type="xsd:integer" use="required"/>
            <xsd:attribute name="version" type="xsd:string" use="required"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

下面是xml2.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.example.com/wm"
    xmlns="http://www.example.com/wm"
  elementFormDefault="qualified">
    <xsd:element name="testEame1">
        <xsd:annotation>
            <xsd:documentation>        test       </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="id" type='xsd:string' minOccurs="1"/>
                <xsd:element name="session" type='xsd:integer' minOccurs="1"/>
            </xsd:sequence>
            <xsd:attribute name="pid" type="xsd:integer" use="required"/>
            <xsd:attribute name="version" type="xsd:string" use="required"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

xml1 和 xml2 的区别是

<xsd:element name="session" type='xsd:string' minOccurs="1"/>

<xsd:element name="session" type='xsd:integer' minOccurs="1"/>

在使用 xsd 文件运行 xjc 时,我面临以下问题。

C:\Temp\tt>xjc *.xsd
parsing a schema...
[ERROR] 'testEame1' is already defined
  line 17 of file:/C:/Temp/tt/xml2.xsd

[ERROR] (related to above error) the first definition appears here
  line 5 of file:/C:/Temp/tt/xml1.xsd

Failed to parse a schema.

我访问了什么版本控制链接2

但我不确定如何实现版本控制和零错误编译。任何帮助将不胜感激!

更新1: 或者我想让会话元素具有整数或字符串类型

<xsd:element name="session" type='xsd:integer | xsd:string' minOccurs="1"/>

标签: javaxmlxsdjaxbxsd-validation

解决方案


您的两个架构的 targetNamespace 相同(http://www.example.com/wm)。一次尝试为每个模式使用不同的 targetNamespace。


推荐阅读