0

我创建了一个如下所示的 XML 文件

  <monitor>
    <widget name="Widgets/TestWidget1">
        <state code="VIC" />
        <state code="TAS" />
    </widget>
    <widget name="Widgets/TestWidget2">
        <client code="someclient" />
    </widget>
  </monitor>

标记的 name 属性<widget>告诉解析器要加载什么小部件(它们是 asp.net 用户控件)。

我正在尝试为上述创建一个模式文件,问题是<widget>支持的子标签内部依赖于 name 属性。所以 TestWidget1 支持<state>标签,TestWidget2 支持<client标签。

目前我的 XML Schema 文件只显示所有可能的<widget>子标签,无论它们是否受支持。

如何编写一个仅允许基于名称属性的特定子标签的 XML 模式文件?如果这是不可能的,我有什么选择?

4

1 回答 1

0

You have several options. The simplest and most direct is to re-think your problem a bit. If the legal content of element E1 and the legal content of element E2 are different, then the simplest design is to call them different things, because in XSD as in DTDs the legal content of an element depends on the element type name. A devil's advocate would ask you "if you want different kinds of widget to obey different rules, why are you telling the validator that they are the same kind of widget? Tell the validator the truth, by giving them different names. So don't call them and so on, call them and ."

In XSD 1.1 you can also use conditional type assignment or assertions to define constraints on the legal combinations of attributes and children, but not every schema-aware editor is going to have the chops necessary to analyse the conditional type assignment rules and attributes and understand what to prompt you with.

于 2012-09-18T15:28:37.837 回答