Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xml parsing - XML validation (different child tags) based on attribute value

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

like

<dashboard>
    <widget type="chart">

    </widget>
</dashboard> 

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>

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> 

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...