使用元素名称冲突

时间:2016-05-18 22:10:24

标签: xml xsd xsd-validation xml-validation

我正在尝试将来自不同来源的两个XSD文件与一个元素名称的名称冲突结合在一起。使用这些XSD文件创建XML文件时,我收到错误消息

  

“此处不允许使用元素标题”

其中title是导致名称冲突的元素名称。

这是第一个XSD Meldeamt.xsd

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://services.meldeamt.gov.at/people"
xmlns="http://services.meldeamt.gov.at/people"
version="2.1">

  <xsd:element name="title">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="citizen"/>
            <xsd:enumeration value="immigrant"/>
        </xsd:restriction>
    </xsd:simpleType>
  </xsd:element>
</xsd:schema>

这是第二个XSD CalTech.xsd

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.caltech.at/hr"
        xmlns="http://www.caltech.at/hr"
        xmlns:registry="http://services.meldeamt.gov.at/people"
        version="2.1">

<xsd:import schemaLocation="Meldeamt.xsd" namespace="http://services.meldeamt.gov.at/people" />

<xsd:element name="CalTech">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="Staff"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:element name="Staff">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="Employee"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:element name="Employee">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="firstName"/>
            <xsd:element ref="name"/>
            <xsd:element ref="title" />
            <xsd:element ref="registry:title"/>
        </xsd:sequence>
        <xsd:attribute name="id" type="xsd:string" use="required" />
    </xsd:complexType>
</xsd:element>

<xsd:element name="title" type="xsd:string">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Ph.D." />
            <xsd:enumeration value="Master" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="name" type="xsd:string"/>

</xsd:schema>

以下是使用上述XSD的 Test.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<cal:CalTech xmlns:cal="http://www.caltech.at/hr"
     xmlns:meldeamt="http://services.meldeamt.gov.at/people"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.caltech.at/hr CalTech.xsd
     http://services.meldeamt.gov.at/people Meldeamt.xsd">

<cal:Staff>
    <cal:Employee id="a471">
        <cal:firstName>John</cal:firstName>
        <cal:name>Connor</cal:name>
        <cal:title>Ph.D.</cal:title>
        <meldeamt:title>immigrant</meldeamt:title>
    </cal:Employee>
</cal:Staff>

</cal:CalTech>

对于第一个标题元素cal:title,我收到错误消息

  

元素cal:此处不允许使用标题

我在IntelliJ Idea中测试XML。 如果我在CalTech.xsd和Text.xml中将cal:title的名称更改为cal:title1,则错误消息将消失,一切正常,包括内容验证。

我不知道我在这里缺少什么。

1 个答案:

答案 0 :(得分:2)

您实际上在设置机器以使用命名空间以避免名称冲突方面做得很好。只是一个小的声明错误修复,一切都会很好......

在CalTech.xsd中,更改

<xsd:element name="title" type="xsd:string">

<xsd:element name="title">

因为元素声明不能同时具有@type属性和匿名类型子元素。

然后,您的XML文件将根据您的XSD成功验证。

顺便说一下,您应该收到的错误应该更像是Xerces提供的内容:

  

[错误] CalTech.xsd:38:47:src-element.3:元素&#39;标题&#39;有两个   &#39;类型&#39;属性和匿名类型&#39;儿童。其中只有一个是   允许元素。

相关问题