可变元素类型的 XML 模式 [英] XML Schema for variable element type

查看:17
本文介绍了可变元素类型的 XML 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于用户界面工具的 XML 元素,如下所示:

I have an XML element for a User Interface tool which looks like this:

<Layout Type="{type}"...>

如果 Type 是Stack",那么元素看起来像这样:

If Type is "Stack" then the element looks like this:

<Layout Type="Stack" Orientation="Horizontal"/>

但是如果它是Margin"那么元素就是

But if it's "Margin" then the element is

<Layout Type="Margin" North="1" Center="3"/>

我正在努力编写一个 XML 模式来解决这个问题.一些帮助将不胜感激.

I'm struggling to write an XML schema that copes with this. Some assistance would be appreciated.

推荐答案

一种简单的方法是将元素 Layout 声明为具有名为 Type(必需)、Orientation、North 和 Center(都是可选)的属性,并指定 if @Type = "Stack" 那么 Orientation 是有意义的,而 North 和 Center 没有意义,而如果 @Type = 'Margin' 那么 North 和 Center 表示这个或那个,而 Orientation 没有意义.

One simple approach is to declare the element Layout as having attributes named Type (required), Orientation, North, and Center (all optional) and specify that if @Type = "Stack" then Orientation is meaningful and North and Center are not, whereas if @Type = 'Margin' then North and Center mean this or that, and Orientation has no meaning.

这样做的优点是您几乎可以使用任何模式语言进行操作,并且可以使事情相对简单.它的缺点是验证共现约束的责任从验证器转移到消费软件.

This has the advantage that you can do it in virtually any schema language and it keeps things relatively simple. It has the disadvantage that the responsibility for verifying the co-occurrence constraint shifts from the validator to the consuming software.

使用 XSD 1.1 的第二种方法是使用条件类型赋值:声明具有适当属性的两种类型,然后指定使用每种类型的条件.

A second approach, using XSD 1.1, is to use conditional type assignment: declare two types with appropriate attributes and then specify the conditions under which each is used.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:tns="http://www.example.com/nss/layout"
  targetNamespace="http://www.example.com/nss/layout"
  elementFormDefault="qualified"> 

  <!--* Strictly speaking, this type is not essential,
      * but it gives an overview of the kinds of layout
      * we expect and allows a reviewer to have more
      * confidence that the conditional type assignment
      * covers all the necessary bases. *-->
  <xs:simpleType name="layout-types">
    <xs:restriction base="xs:NMTOKEN">
      <xs:enumeration value="Stack" />
      <xs:enumeration value="Margin" />      
    </xs:restriction>
  </xs:simpleType>

  <!--* Two different types for the two different
      * kinds of layout. *-->
  <xs:complexType name="StackLayout">
    <xs:attribute name="Type" 
                  type="tns:layout-types" 
                  use="required"/>
    <xs:attribute name="Orientation" 
                  type="xs:string" 
                  use="required"/>
  </xs:complexType>
  <xs:complexType name="MarginLayout">
    <xs:attribute name="Type" 
                  type="tns:layout-types" 
                  use="required"/>
    <xs:attribute name="North" 
                  type="xs:int" 
                  use="required"/>
    <xs:attribute name="Center" 
                  type="xs:int" 
                  use="required"/>
  </xs:complexType>

  <!--* The Layout element is bound to type tns:StackLayout
      * if its Type attribute has the value 'Layout'.
      * Otherwise, it's bound to tns:MarginLayout, if its
      * Type attribute = 'Margin'.  Otherwise, the Layout
      * element instance is invalid.
      * If there's a default type you can use, you don't
      * have to use xs:error.
      *-->
  <xs:element name="Layout">
    <xs:alternative test="@Type='Layout'" 
                    type="tns:StackLayout"/>
    <xs:alternative test="@Type='Margin'" 
                    type="tns:MarginLayout"/>
    <xs:alternative type="xs:error"/>
  </xs:element>

</xs:schema>

如果您无权访问 XSD 1.1 验证器,您可以使用 Schematron 检查共现约束,或者您可以编写一个 Relax NG 架构来验证这些约束.

If you don't have access to an XSD 1.1 validator, you can check the co-occurrence constraint using Schematron, or you can write a Relax NG schema that validates the constraints.

这篇关于可变元素类型的 XML 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆