基于属性值的XML验证(不同的子标签)

前端之家收集整理的这篇文章主要介绍了基于属性值的XML验证(不同的子标签)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个仪表板设计器,它将根据xml值创建小部件.

喜欢

  1. <dashboard>
  2. <widget type="chart">
  3.  
  4. </widget>
  5. </dashboard>

我想更改< widget>中的标签基于@type的值,例如,如果type =“chart”那么它应该允许不同的标签

  1. <dashboard>
  2. <widget type="chart">
  3. <title text="Chart Title"></title>
  4. <plotOptions>
  5. <plotOptions>
  6. <pie showInLegend="true" shadow="false" innerSize="50%">
  7. <dataLabels color="#fff" distance="-20" format="{point.percentage:.0f} %" overflow="false"></dataLabels>
  8. </pie>
  9. </plotOptions>
  10. <legend width="150" align="right" x="10" layout="vertical">
  11. <navigation></navigation>
  12. </legend>
  13. <tooltip enabled="false"></tooltip>
  14. <exporting enabled="true"></exporting>
  15. </plotOptions>
  16. </widget>
  17. </dashboard>

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

  1. <dashboard>
  2. <widget type="table">
  3. <title text="Table Title"></title>
  4. <thead>
  5. <tr>
  6. <th>One</th>
  7. <th>Two</th>
  8. <th>Three</th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <tr>
  13. <td>DS.One</td>
  14. <td>DS.Two</td>
  15. <td>DS.Three</td>
  16. </tr>
  17. </tbody>
  18. </widget>
  19. </dashboard>

它还应该在XML编辑器中提供自动建议,如“ECLIPSE”

其他解决方案是使用XML Schema 1.1中的类型替代.
使用替代类型,您可以根据属性的值为元素设置不同的类型.在您的情况下,如果@type的值是“chart”,则可以将widget元素设置为“chart”类型,如果@type的值是“table”,则可以设置为“table”类型.
请参阅下面的示例模式:
  1. <xs:element name="dashboard">
  2. <xs:complexType>
  3. <xs:sequence maxOccurs="unbounded">
  4. <xs:element name="widget" type="widgetBaseType">
  5. <xs:alternative test="@type='chart'" type="chart"/>
  6. <xs:alternative test="@type='table'" type="table"/>
  7. </xs:element>
  8. </xs:sequence>
  9. </xs:complexType>
  10. </xs:element>
  11.  
  12. <xs:complexType name="widgetBaseType">
  13. <xs:attribute name="type"/>
  14. </xs:complexType>
  15.  
  16. <xs:complexType name="chart">
  17. <xs:complexContent>
  18. <xs:extension base="widgetBaseType">
  19. <xs:sequence>
  20. <xs:element name="title"/>
  21. <xs:element name="plotOptions"/>
  22. </xs:sequence>
  23. </xs:extension>
  24. </xs:complexContent>
  25. </xs:complexType>
  26.  
  27. <xs:complexType name="table">
  28. <xs:complexContent>
  29. <xs:extension base="widgetBaseType">
  30. <xs:sequence>
  31. <xs:element name="title"/>
  32. <xs:element name="thead"/>
  33. <xs:element name="tbody"/>
  34. </xs:sequence>
  35. </xs:extension>
  36. </xs:complexContent>
  37. </xs:complexType>

猜你在找的XML相关文章