How do I write an XSD for this XML-1 (simplified examples):
<?xml version="1.0" encoding="utf-8"?>
<user>
<username>Albert Einstein</username>
<!-- no wrapper element for address -->
<street>Main Street</street>
<city>Ghost Town</city>
<state>Up State</state>
</user>
instead of the more logical XML-2:
<?xml version="1.0" encoding="utf-8"?>
<user>
<username>Albert Einstein</username>
<!-- with wrapper element for address -->
<address>
<street>Main Street</street>
<city>Ghost Town</city>
<state>Up State</state>
</address>
</user>
where username
stands for all fields that are not part of the database table for addresses, which is a table that I cannot just change to include more fields,
with some kind of record type for the address
fields like this XSD:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="user" type="user" />
<!-- =========================================== -->
<xsd:complexType name="user">
<xsd:sequence>
<!-- username stands for all non-address fields -->
<xsd:element name="username" type="xsd:string" />
<!-- wrapper element for the address fields -->
<xsd:element name="address" type="address_Type" />
</xsd:sequence>
</xsd:complexType>
<!-- =========================================== -->
<xsd:complexType name="address_Type">
<!-- the columns of the database table for addresses -->
<xsd:sequence>
<xsd:element name="street" type="xsd:string" />
<xsd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Using the tools in Visual Studio I verified that XML-2 validates with the XSD, but XML-1 does not. How do I improve the "problem line" in the XSD? Apparently, you cannot write in the XSD an element without name:
<xsd:element type="address_type" />
I need to include the complexType
for the address
fields without the enclosing
<address>
tag.
Reason for this question is that this XSD will be useful to create a C# class for the database table address
using xsd.exe in the visual studio sdk.
I will have to process many different format XML's, with most of them having address data within the address wrapper element. It is easy to expand the XSD to validate those XML's. The problem is with XML with address data without wrapper element, that seem not to be validatable with XSD with the complexType Address_Type. Unless someone smarter than myself sees a solution.
Because of the relation between the complexType and the structure of the database table, I cannot just add the username field to the complexType. Username really stands for all fields that are NOT in the address table.
I am open for all suggestions. Even after doing quite some XSD stuff I still feel like a beginner and I am not even sure if using xsd:sequence is needed at all.
Update: New Q/A that addresses @Roland's refined requirements has been created here:
Original, outdated answer and updates follow...
If you do not want a wrapper address
element, you can simply not define one, and place its content model directly within the content model of user
:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="user" type="user" />
<xsd:complexType name="user">
<xsd:sequence>
<xsd:element name="username" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Per your comment, you don't want to do the above; you want to reuse the elements in address_Type
.
There is no direct way to say what you want, but you could have the user
complex type extend the address_Type
complex type. This would allow you to add the username
element to those elements already appearing in address_type
(although it would read rather unnaturally to extend an address in such a way).
To reuse parts of content models more naturally, consider using xs:group
. For examples, see