XSD restrictions based on open/closed elements
Is there a way in a schema to indicate the requirement of attributes determined by the open or closed nature of the element. For example i have an element that if open has no requirement for the attribute 'test', however if its closed it is required.
开发者_运维知识库<element name="employee" >
blah!
</element>
Ok
<element name="employee" />
Fail - attribute 'test' required.
I guess you are using "open" to mean that the element has a text node child, and "closed" to mean that it hasn't. So you're saying the element should either have a text node child or an attribute but not both (?) and not neither.
That's classified as a co-occurrence constraint, and can't be done in XSD 1.0. It can be done with assertions in XSD 1.1
<xs:element name="employee" type="...">
<xs:assert test="string(.) or @name"/>
</xs:element>
XSD 1.1 support is currently available in Xerces and Saxon.
No, there isn't a way to do that. (I'd cite a reference, but it's hard to reference a non-feature!)
In general, this is a step beyond what XSD provides; in only a very few cases (like IDREFs) does it provide any way to validate one datum by referencing another. XSD 1.1 provides some new assertion support that might do what you need, but the spec is just a working draft at this point.
精彩评论