【问题标题】:XSD for an element that must have an attribute depending on another attribute value必须具有取决于另一个属性值的属性的元素的 XSD
【发布时间】:2010-01-28 04:26:19
【问题描述】:

我有一个 <font> 标记,其属性名称为 required 、 link='notusing' required 和 replaced_with (它应该是可选的,只有 link 值不是 'notusing' 的字符串)。

XSD 还必须检查 xml 是否具有在replaced_with 中指定的名称的字体。

例子:

<fonts>
  <font name='font1' link='inuse'/>
  <font name='font2' link='inuse'/>
  <font name='font3' link='notusing' replaced_with="font2"/>
</fonts>

我该如何为此编写 XSD?谢谢

【问题讨论】:

    标签: xsd


    【解决方案1】:

    我认为不可能强制 replaced_with 只能在 link='notusing' 时出现。您可以使用minOccurs='0'replaced_with 设为可选,但仅此而已。

    如果您能够更改 XML 文件的结构,则可以这样做:

    <?xml version="1.0" encoding="utf-8" ?>
    <fontData
      xmlns="someNamespace"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="someNamespace XMLSchema1.xsd">
      <fonts>
        <font name="font1" />
        <font name="font2" />
      </fonts>
      <obsoleteFonts>
        <font name="font3" replaced_with="font2" />
      </obsoleteFonts>
    </fontData>
    

    然后您可以使用keykeyref 来强制obsoleteFonts 中的任何font 的名称存在于fonts 中。

    下面是 XSD 文件强制执行这种 XML 格式的方式:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema
      xmlns="someNamespace"
      xmlns:tns="someNamespace"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="someNamespace"
      elementFormDefault="qualified">
      <xs:element name="fontData" type="fontData">
        <xs:key name="fontKey">
          <xs:selector xpath="tns:fonts/tns:font" />
          <xs:field xpath="@name" />
        </xs:key>
        <xs:keyref name="obsoleteFontToFontKeyRef" refer="fontKey">
          <xs:selector xpath="tns:obsoleteFonts/tns:font" />
          <xs:field xpath="@replaced_with" />
        </xs:keyref>
      </xs:element>
      <xs:complexType name="fontData">
        <xs:sequence>
          <xs:element name="fonts" type="fonts" />
          <xs:element name="obsoleteFonts" type="obsoleteFonts" />
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="fonts">
        <xs:sequence>
          <xs:sequence>
            <xs:element name="font" type="font" maxOccurs="unbounded" />
          </xs:sequence>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="obsoleteFonts">
        <xs:sequence>
          <xs:sequence>
            <xs:element name="font" type="obsoleteFont" maxOccurs="unbounded" />
          </xs:sequence>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="font">
        <xs:attribute name="name" type="xs:string" />
      </xs:complexType>
      <xs:complexType name="obsoleteFont">
        <xs:attribute name="name" type="xs:string" />
        <xs:attribute name="replaced_with" type="xs:string" />
      </xs:complexType>
    </xs:schema>
    

    我使用 Visual Studio 架构验证器对此进行了测试,但希望它能够在您使用的任何技术中正常工作。

    【讨论】:

      【解决方案2】:

      在 XML 模式 (XSD) 中,您无法表达这种要求/限制。 XML 模式是关于结构的,您不能使用元素或属性的值来影响其他元素或属性的结构。做不到,它只是不是 XML 模式标准的一部分。

      如果您需要此类检查,请查看Schematron,这是一种不同类型的 XML 验证框架。

      【讨论】:

        猜你喜欢
        • 2016-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2021-04-14
        • 1970-01-01
        • 1970-01-01
        • 2012-09-09
        相关资源
        最近更新 更多