向我展示这个XSD的示例XML?

时间:2017-02-15 21:13:42

标签: xml xsd xsd-validation xml-validation

我正在尝试为此架构编写有效的XML:

<xsd:complexType name="resourceType">
    <annotation xmlns="http://www.w3.org/2001/XMLSchema">
        <documentation>
            A resource root within a deployment.
        </documentation>
    </annotation>
    <xsd:all>
        <xsd:element name="filter" type="filterType" minOccurs="0">
            <annotation xmlns="http://www.w3.org/2001/XMLSchema">
                <documentation>
                    A path filter specification for this resource root (optional). By default all paths are accepted.
                </documentation>
            </annotation>
        </xsd:element>
    </xsd:all>
    <xsd:attribute name="name" type="xsd:string" use="optional">
        <annotation xmlns="http://www.w3.org/2001/XMLSchema">
            <documentation>
                The name of this resource root (optional). If not specified, defaults to the value of the path
                attribute.
            </documentation>
        </annotation>
    </xsd:attribute>
    <xsd:attribute name="path" type="xsd:string" use="required">
        <annotation xmlns="http://www.w3.org/2001/XMLSchema">
            <documentation>
                The path of this resource root, relative to the path in which the module.xml file is found.
            </documentation>
        </annotation>
    </xsd:attribute>
</xsd:complexType>

我是XML和XSD的新手,我将不胜感激:根据这个XSD,什么是一个有效的示例XML文档?

1 个答案:

答案 0 :(得分:1)

有两个问题阻止任何XML对提供的XSD有效:

  1. XSD包含对未定义类型的引用filterType
  2. XSD指定没有根元素。
  3. filterType替换为xsd:string并添加根元素,r类型等于XSD中的顶级complexType,然后修改XSD,

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="r" type="resourceType"/>
      <xsd:complexType name="resourceType">
        <annotation xmlns="http://www.w3.org/2001/XMLSchema">
          <documentation>
            A resource root within a deployment.
          </documentation>
        </annotation>
        <xsd:all>
          <xsd:element name="filter" type="xsd:string" minOccurs="0">
            <annotation xmlns="http://www.w3.org/2001/XMLSchema">
              <documentation>
                A path filter specification for this resource root
                (optional). By default all paths are accepted.
              </documentation>
            </annotation>
          </xsd:element>
        </xsd:all>
        <xsd:attribute name="name" type="xsd:string" use="optional">
          <annotation xmlns="http://www.w3.org/2001/XMLSchema">
            <documentation>
              The name of this resource root (optional). If not specified,
              defaults to the value of the path attribute.
            </documentation>
          </annotation>
        </xsd:attribute>
        <xsd:attribute name="path" type="xsd:string" use="required">
          <annotation xmlns="http://www.w3.org/2001/XMLSchema">
            <documentation>
              The path of this resource root, relative to the path in
              which the module.xml file is found.
            </documentation>
          </annotation>
        </xsd:attribute>
      </xsd:complexType>
    </xsd:schema>
    

    将成功验证以下XML,例如:

    <?xml version="1.0" encoding="UTF-8"?>
    <r path="">
      <filter/>
    </r>