我正在嘗試為此示例 XML 創建驗證模式。我已經嘗試使用遞回定義,但找不到合適的方法。請注意 IF 和 ELSE 元素可以嵌套任意次數,并且 ACTIONx 元素可以按任意順序出現。另請注意,每個元素都有自己可能不同的屬性。
<?xml version="1.0" encoding="utf-8"?>
<KEYPRESS Buffer="1">
<ACTION1 Index="15" />
<IF Condition="0">
<IF Condition="1">
<ACTION1 Index="14" />
<ELSE>
<ACTION2 Measure="whatever" />
</ELSE>
</IF>
<IF Condition="2">
<IF Condition="5">
<ACTION2 Measure="whatelse" />
<ACTION3 Type="Flag" />
</IF>
<ELSE>
<ACTION1 Index="0" />
<ACTION3 Type="Other" />
<IF Condition="1">
<ACTION3 Type="Flag" />
</IF>
</ELSE>
</IF>
</IF>
</KEYPRESS>
任何幫助將不勝感激。
*** 編輯*** 非常感謝Michael Kay,出色的解決方案!我在這里發布結果模式,以防有人可能感興趣。
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio 2021 (https://www.liquid-technologies.com) -->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="INSTRUCTION" abstract="true"/>
<xs:element name="ACTION1" substitutionGroup="INSTRUCTION">
<xs:complexType>
<xs:attribute name="Index" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="ACTION2" substitutionGroup="INSTRUCTION">
<xs:complexType>
<xs:attribute name="Index" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="ACTION3" substitutionGroup="INSTRUCTION">
<xs:complexType>
<xs:attribute name="Type" type="xs:string" use="required" />
<xs:attribute name="Index" type="xs:string" use="required" />
<xs:attribute name="Value" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="IF" substitutionGroup="INSTRUCTION">
<xs:complexType>
<xs:sequence>
<xs:element ref="INSTRUCTION" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="ELSE" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="Type" type="xs:string" use="required" />
<xs:attribute name="Index" type="xs:string" use="required" />
<xs:attribute name="Condition" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="KEYPRESS" >
<xs:complexType>
<xs:sequence>
<xs:element ref="INSTRUCTION" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="KeySeq" type="xs:string" use="required" />
<xs:attribute name="Buffer" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="ELSE" >
<xs:complexType>
<xs:sequence>
<xs:element ref="INSTRUCTION" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
uj5u.com熱心網友回復:
這種結構就是取代基的用途。
定義一個抽象元件INSTRUCTION,然后定義IF,ACTION1,ACTION2,和ACTION3作為取代基的成員INSTRUCTION,每個都有其自己的型別定義限定所允許的屬性。的內容模型KEYPRESS和ELSE似乎是INSTRUCTION*(0以上的指令序列),和的內容模型IF看起來是(INSTRUCTION*, ELSE?)(0以上的指令序列任選地隨后通過ELSE)。
(ELSE不是替換組的成員,因為它不能出現在任何出現指令的地方)。
uj5u.com熱心網友回復:
我認為您不能定義具有可變元素名稱的架構。
一個元素宣告需要一個名字。該值的型別為xs:NCName,它只是一個字串而不是模式。為了撰寫模式,您必須找出操作元素的最大數量。并且您必須為每個單個元素定義一個 complexType。我不認為你想這樣做。
您的 XML 檔案是由對 XML 不太了解的人設計的。你最好告訴他,格式必須重新設計。代替
<ACTION1 ...>
利用
<ACTION N="1" ...>
并且模式驗證將很容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/391148.html
上一篇:在Powershell中將UFT-8xml轉換為Unicode時,$encoding屬性值在輸出xml中顯示bigEndianUnicode,我想要UTF-16
下一篇:Odoo-如何模塊化XML欄位
