使用全局属性和命名空间根据 XSD 进行验证

Validating against XSD with global attributes and namespaces

我正在尝试引用 XSD 文件(XML 架构)中的一些属性,但出现以下错误:

Attribute 'code' is not allowed to appear in element 'project'.

Attribute 'type' is not allowed to appear in element 'duration'.

Attribute 'type' is not allowed to appear in element 'duration'.

任何人都可以向我解释我做错了什么吗?

这是我的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<timesheets xmlns="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets timesheets.xsd">
        <employee id="234456" ismanager="false" hiredate="2003-01-12">
            <firstname>Julie</firstname>
            <surname>Smith</surname>
        </employee>
        <projects>
            <project code="TSK2367">
                <task>
                    <description>Create project plan for MIJ website</description>
                    <duration type="days">
                        <planned>2</planned>
                        <actual>1</actual>
                    </duration>
                </task>
                <task>
                    <description>Draft the conceptual web site navigation for MIJ</description>
                    <duration type="hours">
                        <planned>3</planned>
                        <actual>2</actual>
                    </duration>
                </task>
            </project>
        </projects>
    </timesheets>

这是我的 XSD 文件:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified"
        targetNamespace="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
        xmlns="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
        >

        <xs:attributeGroup name="employeeAttrs">
            <xs:attribute name="id" type="xs:int" />
            <xs:attribute name="ismanager" type="xs:boolean" />
            <xs:attribute name="hiredate" type="xs:date" />
        </xs:attributeGroup>
        <xs:element name="firstname" type="xs:string" />
        <xs:element name="surname" type="xs:string" />
        <xs:attribute name="code" type="xs:string" />
        <xs:element name="description" type="xs:string" />
        <xs:attribute name="type" type="xs:string" />
        <xs:element name="planned" type="xs:int" />
        <xs:element name="actual" type="xs:int" />



        <xs:element name="timesheets">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="employee">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element ref="firstname" />
                                <xs:element ref="surname" />
                            </xs:sequence>
                            <xs:attributeGroup ref="employeeAttrs" />
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="projects">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="project" maxOccurs="unbounded">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="task" maxOccurs="unbounded">
                                                <xs:complexType>
                                                    <xs:sequence>
                                                        <xs:element ref="description" />
                                                        <xs:element name="duration">
                                                            <xs:complexType>
                                                                <xs:sequence>
                                                                    <xs:element ref="planned" />
                                                                    <xs:element ref="actual" />
                                                                </xs:sequence>
                                                                <xs:attribute ref="type" />
                                                            </xs:complexType>
                                                        </xs:element>
                                                    </xs:sequence>
                                                </xs:complexType>
                                            </xs:element>
                                        </xs:sequence>
                                        <xs:attribute ref="code" />
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>

如果您希望更改 XML 以符合 XSD,请为 xmlns:ts="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets" 定义一个名称空间前缀并将其用于未找到的属性:

<?xml version="1.0" encoding="UTF-8"?>
<timesheets xmlns="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
            xmlns:ts="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets
                                timesheets.xsd">
  <employee id="234456" ismanager="false" hiredate="2003-01-12">
    <firstname>Julie</firstname>
    <surname>Smith</surname>
  </employee>
  <projects>
    <project ts:code="TSK2367">
      <task>
        <description>Create project plan for MIJ website</description>
        <duration ts:type="days">
          <planned>2</planned>
          <actual>1</actual>
        </duration>
      </task>
      <task>
        <description>Draft the conceptual web site navigation for MIJ</description>
        <duration ts:type="hours">
          <planned>3</planned>
          <actual>2</actual>
        </duration>
      </task>
    </project>
  </projects>
</timesheets>

如果您希望更改 XSD 以使 XML 有效 ,请移动 code 和 [=14] 的定义=] 局部于使用它们的地方而不是全局:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           targetNamespace="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
           xmlns:ts="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
           xmlns="http://www.dcs.bbk.ac.uk/~lharar01/ns/timesheets"
           >

  <xs:attributeGroup name="employeeAttrs">
    <xs:attribute name="id" type="xs:int" />
    <xs:attribute name="ismanager" type="xs:boolean" />
    <xs:attribute name="hiredate" type="xs:date" />
  </xs:attributeGroup>

  <xs:element name="firstname" type="xs:string" />
  <xs:element name="surname" type="xs:string" />
  <xs:element name="description" type="xs:string" />
  <xs:element name="planned" type="xs:int" />
  <xs:element name="actual" type="xs:int" />

  <xs:element name="timesheets">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="employee">
          <xs:complexType>
            <xs:sequence>
              <xs:element ref="firstname" />
              <xs:element ref="surname" />
            </xs:sequence>
            <xs:attributeGroup ref="employeeAttrs" />
          </xs:complexType>
        </xs:element>
        <xs:element name="projects">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="project" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="task" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element ref="description" />
                          <xs:element name="duration">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element ref="planned" />
                                <xs:element ref="actual" />
                              </xs:sequence>
                              <xs:attribute name="type" type="xs:string" />
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="code" type="xs:string" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

更新评论中的每个 OP 问题:

So are you saying that there is no way to reference an attribute without placing its definition in the global scope?

不,我展示了如果您想通过更改 XSD 来消除错误,您可以将 codetype 属性的声明设为局部而不是全局.

Also, why did the attributeGroup referencing not throw an error?

因为属性声明对于 attributeGroup 来说是本地的。

关于属性和命名空间的建议

使用属性的最常见方式,以及我建议您遵循的方式,除非您正在定义属性-基于词汇表故意与多个名称空间混合,是让它们不在名称空间中。

让属性不在命名空间中的选项:

  1. 没有targetNamespace.
  2. 在本地向元素或属性组声明属性。