XSD架构 - 基于文档中的值的枚举

时间:2012-11-09 13:02:05

标签: xml enums xsd

这是一个正确的远景。

如果我的XSD中有一个已定义的元素,包含两个属性,比如NAME和VALUE,是否可以根据Name中的值限制VALUE枚举?

例如:

<xsd:complexType name="ConfigDef">
    <xsd:attribute name="NAME" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>The name of this Config setting.</xsd:documentation>
        </xsd:annotation>           
    </xsd:attribute>
    <xsd:attribute name="VALUE" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>The value of this Config setting.</xsd:documentation>
        </xsd:annotation>
    </xsd:attribute>
</xsd:complexType>

我不认为可以做到。可以吗?是否有XSD黑客才能让它发挥作用?

1 个答案:

答案 0 :(得分:2)

您可以使用嵌入式schematron

执行此操作
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="ConfigDef">
    <xsd:annotation>
      <xsd:appinfo>
        <sch:pattern name="Test constraints on the ConfigDef element"
          xmlns:sch="http://purl.oclc.org/dsdl/schematron">
          <sch:rule
            context="ConfigDef">
            <sch:assert test="@NAME eq 'foo' and @VALUE = ('bar','baz')">Error message when the assertion condition is broken...</sch:assert>
            <sch:assert test="@NAME eq 'qux' and @VALUE = ('teh','xyz')">Error message when the assertion condition is broken...</sch:assert>
          </sch:rule>
        </sch:pattern>
      </xsd:appinfo>
    </xsd:annotation>
    <xsd:attribute name="NAME" type="xsd:string">
      <xsd:annotation>
        <xsd:documentation>The name of this Config setting.</xsd:documentation>
      </xsd:annotation>           
    </xsd:attribute>
    <xsd:attribute name="VALUE" type="xsd:string">
      <xsd:annotation>
        <xsd:documentation>The value of this Config setting.</xsd:documentation>
      </xsd:annotation>
    </xsd:attribute>
  </xsd:complexType>
</xsd:schema>

或者你可以使用Relax Schema

来做到这一点
element ConfigDef {
  ((
  attribute NAME {'foo'},
  attribute VALUE {'bar'|'baz'}) |  
  (
  attribute NAME {'qux'},
  attribute VALUE {'teh'|'xyz'}))
}