Xml 如何在需要属性时防止XSD中的空元素

Xml 如何在需要属性时防止XSD中的空元素,xml,xsd,xml-validation,Xml,Xsd,Xml Validation,我正在使用下面的XSD验证XML文档,我希望确保reportPath的值不为空 这是我当前的XSD <xs:element name="reportPath"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="Name" type="xs

我正在使用下面的XSD验证XML文档,我希望确保
reportPath
的值不为空

这是我当前的XSD

    <xs:element name="reportPath">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute name="Name" type="xs:string" use="required" />
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>

您可以通过
将长度限制为至少1个字符(正如Ken White在评论中提到的)

下面介绍了如何将所有内容组合到XML示例中,包括如何在同时需要
Name
属性的情况下使用
xs:restriction

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:ra="ReportAutomation"
           targetNamespace="ReportAutomation"
           elementFormDefault="qualified">

  <xs:element name="configuration">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="templatePath" type="ra:namedNonEmptyType"/>
        <xs:element name="reportPath" type="ra:namedNonEmptyType"/>
        <xs:element name="filter" type="ra:namedNonEmptyType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="namedNonEmptyType">
    <xs:simpleContent>
      <xs:extension base="ra:nonEmptyString">
        <xs:attribute name="Name" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:simpleType name="nonEmptyString">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>


有什么问题?
关键是必须分两步执行-必须声明一个名为
的simpleType
,它限制
xs:string
以应用
minLength
限制,然后使
complexType
扩展该限制。您不能在一个声明中应用简单类型facet和复杂类型属性。感谢您提供的帮助完整答案。。!!
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:ra="ReportAutomation"
           targetNamespace="ReportAutomation"
           elementFormDefault="qualified">

  <xs:element name="configuration">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="templatePath" type="ra:namedNonEmptyType"/>
        <xs:element name="reportPath" type="ra:namedNonEmptyType"/>
        <xs:element name="filter" type="ra:namedNonEmptyType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="namedNonEmptyType">
    <xs:simpleContent>
      <xs:extension base="ra:nonEmptyString">
        <xs:attribute name="Name" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:simpleType name="nonEmptyString">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>