Reputation: 31
I'm trying to get an XML element be able to use a complexType
and a simpleType
for its type attribute. I'm relatively new to XML and XSD, and I'm not sure if this is possible. I wrote some test code to explain the problem.
The one semi-sort of solution I've got to work was just creating two different EntityType
types, but in this case, I do not want to do that because rewriting all the same elements into a new type just to change the PhoneNumber
element type in the two EntityType
types feels like a waste of code, especially when EntityType
could have a lot more elements in it. I've tried looking into XML version 1.1 and using xs:alternative
, but I'm not sure if it would work in this particular situation.
Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<City>
<Bob>
<Name>Bob</Name>
<PhoneNumber>262-662-6262</PhoneNumber>
</Bob>
<QMart>
<Name>QMart</Name>
<PhoneNumber>
<FAX>#45654</FAX>
<PhoneNumber>262-333-3939</PhoneNumber>
<PhoneNumber2>262-412-8633</PhoneNumber2>
</PhoneNumber>
</QMart>
</City>
And here is the XSD:
?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="City" type="CityType"/>
<xs:complexType name="CityType">
<xs:sequence>
<xs:element name="Bob" type="EntityType"/>
<xs:element name="QMart" type="EntityType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="EntityType">
<xs:sequence>
<xs:element name="Name" type="NameType"/>
<!-- I want both Bob and QMart to use the same EntityType type,
but the only difference is that QMart uses the complex
PhoneNumberType for element PhoneNumber, while Bob uses
the simple PhoneNumberValueType for the PhoneNumber element
Bob: <xs:element name="PhoneNumber" type="PhoneNumberValueType"/>
QMart: <xs:element name="PhoneNumber" type="PhoneNumberType"/>
-->
</xs:sequence>
</xs:complexType>
<xs:complexType name="PhoneNumberType">
<xs:sequence>
<xs:element name="FAX" type="FAXType"/>
<xs:element name="PhoneNumber" type="PhoneNumberValueType"/>
<xs:element name="PhoneNumber2" type="PhoneNumberValueType"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="PhoneNumberValueType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="NameType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="FAXType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:schema>
I've tried validating the XML and XSD with a xs:union
element, but that only works for simpleTypes
only. I'm not sure what else I can do and I'm running out of ideas. Any help is appreciated!
Upvotes: 2
Views: 416
Reputation: 111491
Regarding your title question, see XSD allowing both simpleType and complexType content for same element? There are a number of sub-questions which I'll address in turn below...
I'm trying to get an XML element be able to use a
complexType
and asimpleType
for its type attribute.
This is confusingly stated, perhaps because you're using 'attribute' in some sort of generic sense; doing so in the context of XML, where 'attribute' means something very specific, is bound to create confusion.
Regarding
two
EntityType
types feels like a waste of code, especially whenEntityType
could have a lot more elements in it.
the usual way to reduce such duplication is via xsd:group
. See
Regarding having two sorts of phone numbers, consider having two distinctly named elements, one for each (simple and complex) type and using xsd:choice
in the containing content model to allow either element where you wish to allow phone numbers.
Upvotes: 1