Search code examples
xmlxsdxsd-validationxml-validation

Show me sample XML for this XSD?


I am trying to write valid XML for this schema:

<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>

I am new to XML and XSD, and I will be grateful for little help: What would be an example XML document that would be valid according to this XSD?


Solution

  • There are two issues that prevent any XML from being valid against your XSD as provided:

    1. The XSD includes a reference to an undefined type, filterType.
    2. The XSD specifies no root element.

    By replacing filterType with xsd:string and adding a root element, r of type equal to the top-level complexType in your XSD, then this modified 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>
    

    would successfully validate the following XML, for example:

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