質問

How can I define in XMLSchema this pattern of element ?
<port num="80"/>
<port min="80" max="443"\>
because port must be defined by an attribute num or a range.

役に立ちましたか?

解決 2

You can achieve that if your parser supports W3C XSD 1.1:

<xs:element name="port" type="portType" />
<xs:complexType name="portType">
    <xs:attribute name="min" type="xs:integer" use="optional"/>
    <xs:attribute name="max" type="xs:integer" use="optional"/>
    <xs:attribute name="num" type="xs:integer" use="optional"/>
    <xs:assert test="@num or (@max and @min)" />
</xs:complexType>

If your parser doesn't support XSD 1.1, you can use XSD 1.0 + Schematron, where you place your assertions inside xs:annotation/xs:appinfo and use XSLT or some external tools to validate them. In your case, it might be simpler just to check those assertions in the language that is running your parser after the XSD validation.

他のヒント

helderdarocha has already pointed to XSD 1.1. Another approach is to re-think your design. It currently uses the same name for two different structures; giving them different names makes the problem trivial.

<portnum num="80"/>
<portrange min="80" max="443"/>

or

<port><num>80</num></port>
<port><range min="80" max="443"/></port>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top