基于属性值的 XML 验证(不同的子标签) [英] XML validation (different child tags) based on attribute value

查看:22
本文介绍了基于属性值的 XML 验证(不同的子标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个仪表板设计器,它将根据 xml 值创建小部件.

I am writing an dashboard designer that will create widgets based on xml values.

喜欢

<dashboard>
    <widget type="chart">

    </widget>
</dashboard> 

我想根据 @type 的值更改 中的标签,例如如果 type="chart"> 那么它应该允许不同的标签

I want to change the tags inside the <widget> based on the value of @type for example if type="chart" Then it should allow different tags

<dashboard>
    <widget type="chart">
        <title text="Chart Title"></title>
        <plotOptions>
            <plotOptions>
                <pie showInLegend="true" shadow="false" innerSize="50%">
                    <dataLabels color="#fff" distance="-20" format="{point.percentage:.0f} %" overflow="false"></dataLabels>
                </pie>
            </plotOptions>
            <legend width="150" align="right" x="10" layout="vertical">
                <navigation></navigation>
            </legend>
            <tooltip enabled="false"></tooltip>
            <exporting enabled="true"></exporting>
        </plotOptions>
    </widget>
</dashboard>

如果我们有 type="table" 它应该允许不同的标签

AND if we have type="table" It should allow different tags

<dashboard>
    <widget type="table">
        <title text="Table Title"></title>
        <thead>
            <tr>
                <th>One</th>
                <th>Two</th>
                <th>Three</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>DS.One</td>
                <td>DS.Two</td>
                <td>DS.Three</td>
            </tr>
        </tbody>
    </widget>
</dashboard> 

而且它还应该在 XML 编辑器中给出自动建议,例如 "ECLIPSE"

And it should also give auto suggest in XML editor like "ECLIPSE"

推荐答案

其他解决方案是使用类型替代,来自 XML Schema 1.1.使用替代类型,您可以根据属性的值为元素设置不同的类型.在您的情况下,如果@type 的值为chart",则可以将小部件元素设置为chart"类型,如果@type 的值为table",则设置为table"类型.请参阅下面的示例架构:

Other solution is to use type alternative, from XML Schema 1.1. Using the alternative types you can set different types to an element depending on the value of an attribute. In your case you can set to the widget element the "chart" type if the value of the @type is "chart", and the "table" type in case the value of the @type is "table". See below a sample schema:

<xs:element name="dashboard">
    <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
            <xs:element name="widget" type="widgetBaseType">
                <xs:alternative test="@type='chart'" type="chart"/>
                <xs:alternative test="@type='table'" type="table"/>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="widgetBaseType">
    <xs:attribute name="type"/>
</xs:complexType>

<xs:complexType name="chart">
    <xs:complexContent>
        <xs:extension base="widgetBaseType">
            <xs:sequence>
                <xs:element name="title"/>
                <xs:element name="plotOptions"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="table">
    <xs:complexContent>
        <xs:extension base="widgetBaseType">
            <xs:sequence>
                <xs:element name="title"/>
                <xs:element name="thead"/>
                <xs:element name="tbody"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

这篇关于基于属性值的 XML 验证(不同的子标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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