1

这是一个正确的远射。

如果我的 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 黑客可以让它工作?

4

1 回答 1

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>

或者你可以用一个放松模式来做到这一点

element ConfigDef {
  ((
  attribute NAME {'foo'},
  attribute VALUE {'bar'|'baz'}) |  
  (
  attribute NAME {'qux'},
  attribute VALUE {'teh'|'xyz'}))
}
于 2012-11-09T17:52:17.487 回答