我正在研究描述消息結構的XML Schema(message.xsd
)。XML Schema:以任意順序出現的元素,出現0-1次或0- *次
這裏是<message>
元件(根目錄)的預期小孩:
<from>
:包含發送者的地址。發生0或1次。<to>
:包含收件人的地址。發生0次或更多次。<cc>
:包含複製中的收件人地址。發生0次或更多次。<subject>
:包含郵件的主題。發生0或1次。<body>
:包含消息的正文。發生0或1次。
元素的順序並不重要,這意味着它們可以以任意順序排列。
這裏是一個必須是有效的第一個簡單的XML例子(message-1.xml
):
$ xmllint --schema message.xsd message-1.xml
<?xml version="1.0"?>
<message xmlns="http://yugiohjcj.1s.fr/message">
<from>[email protected]</from>
<to>[email protected]</to>
<subject>How are you?</subject>
<body>Hello. How are you? Thank you. Best regards.</body>
</message>
message-1.xml validates
而且它是有效的!
這裏是一個必須是有效的第二個更復雜的XML例子(message-2.xml
):
$ xmllint --schema message.xsd message-2.xml
<?xml version="1.0"?>
<message xmlns="http://yugiohjcj.1s.fr/message">
<subject>How are you?</subject>
<body>Hello. How are you? Thank you. Best regards.</body>
<from>[email protected]</from>
<to>[email protected]</to>
<to>[email protected]</to>
<cc>[email protected]</cc>
<cc>[email protected]</cc>
</message>
message-2.xml validates
而且它是有效的!
這裏是一個必須是無效的第三個XML例子(message-3.xml
):
$ xmllint --schema message.xsd message-3.xml
<?xml version="1.0"?>
<message xmlns="http://yugiohjcj.1s.fr/message">
<from>[email protected]</from>
<from>[email protected]</from>
<to>[email protected]</to>
<subject>How are you?</subject>
<subject>How are you again?</subject>
<body>Hello. How are you? Thank you. Best regards.</body>
<body>Hello. How are you again? Thank you. Best regards.</body>
</message>
message-3.xml validates
但它是有效的! 這就是問題所在。
這裏是我的XML模式(message.xsd
):
$ cat message.xsd
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://yugiohjcj.1s.fr/message" targetNamespace="http://yugiohjcj.1s.fr/message" elementFormDefault="qualified">
<xs:element name="message">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="from"/>
<xs:element name="to"/>
<xs:element name="cc"/>
<xs:element name="subject"/>
<xs:element name="body"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我想我需要在maxOccurs
屬性的值設置爲1
的地方或者我需要以不同的組合<xs:sequence>
和<xs:choice>
元素,但我有沒有找到如何達到我的目標。
任何想法如何解決這個XML架構好嗎?
謝謝。
此致敬禮。
編輯:好吧,我用Relax NG代替XSD 1.0找到了解決方法,我的問題。它工作正常,並且與libxml2兼容。
這裏是我的message.rng
文件:
$ cat message.rng
<element name="message" xmlns="http://relaxng.org/ns/structure/1.0">
<interleave>
<zeroOrMore>
<element name="from">
<text/>
</element>
</zeroOrMore>
<zeroOrMore>
<element name="to">
<text/>
</element>
</zeroOrMore>
<zeroOrMore>
<element name="cc">
<text/>
</element>
</zeroOrMore>
<optional>
<element name="subject">
<text/>
</element>
</optional>
<optional>
<element name="body">
<text/>
</element>
</optional>
</interleave>
</element>
這裏是我的3個XML實例(message-1.xml
,message-2.xml
和message-3.xml
)結果:
$ xmllint --relaxng message.rng message-1.xml
<?xml version="1.0"?>
<message>
<from>[email protected]</from>
<to>[email protected]</to>
<subject>How are you?</subject>
<body>Hello. How are you? Thank you. Best regards.</body>
</message>
message-1.xml validates
$ xmllint --relaxng message.rng message-2.xml
<?xml version="1.0"?>
<message>
<subject>How are you?</subject>
<body>Hello. How are you? Thank you. Best regards.</body>
<from>[email protected]</from>
<to>[email protected]</to>
<to>[email protected]</to>
<cc>[email protected]</cc>
<cc>[email protected]</cc>
</message>
message-2.xml validates
$ xmllint --relaxng message.rng message-3.xml
<?xml version="1.0"?>
<message>
<from>[email protected]</from>
<from>[email protected]</from>
<to>[email protected]</to>
<subject>How are you?</subject>
<subject>How are you again?</subject>
<body>Hello. How are you? Thank you. Best regards.</body>
<body>Hello. How are you again? Thank you. Best regards.</body>
</message>
Relax-NG validity error : Extra element subject in interleave
message-3.xml:7: element subject: Relax-NG validity error : Element message failed to validate content
message-3.xml fails to validate
不幸的是,我沒有XSD 1.1驗證程序。我在C中使用[libxml2](http://xmlsoft.org/),它只支持XSD 1.0。我發現[Xerces](http://xerces.apache.org/mirrors.cgi)支持XSD 1.1,但在測試版本中,[Saxon](http://saxon.sourceforge.net/)僅支持XSD1.1在商業版本中。 – YuGiOhJCJ
我很同情。你得到你所付出的。 –