I’m still early on in learning XML schema, so hopefully this isn’t too easy or naive a question.
(I’m perusing the specifications, and various other resources, but if anyone can suggest further reading, I’m more than open to suggestion)
Anyway, what I’m after doesn’t appear to be possible with my novice understanding, so I’m hoping someone can correct me (thus proving it possible)
I’m trying to create a complex type that can contain either any simple type, or another node of the containing complex type, but not both; thus (with the addition of a string “key” attribute) mimicking a multidimensional associative array.
Problem is, I can’t seem to figure out how to allow a choice between the two (of course, I can’t seem to figure out if this is possible either, though my reading is leading me to believe not)
So, essentially, what would I need to do in order to have a target document validate with:
<node key="string">
<node key="string">
arbitrary value
</node>
<node key="string">
arbitrary value
</node>
</node>
But invalidate with:
<node key="string">
<node key="string">
arbitrary value
<node key="string">
arbitrary value
</node>
</node>
</node>
(Note the mixed content node, I don’t want to allow that)
This is where I’m at:
<xs:attribute name="key">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse" />
<xs:pattern value="[a-zA-Z_][a-zA-Z0-9_]*" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="data">
<!-- what do i do? -->
<xs:attribute ref="key" use="required" />
</xs:complexType>
If this isn’t in fact possible, I suppose that’s also fine, but this seems like a reasonably common data structure, one which I would think XML would accommodate.
I think you would find the XML easier to process (not just with XSD, but also with XSLT and XQuery) if you changed the design to:
It would then be easy to specify in your schema that the content model for node is a choice: either a sequence of nodes, or a single value.