How to create a reusable “US state” in an XML schema?

I have an XML schema containing several addresses:

<xs:element name="personal_address" maxOccurs="1"> <!-- address fields go here --> </xs:element> <xs:element name="business_address" maxOccurs="1"> <!-- address fields go here --> </xs:element> 

Within each address element, I include the listing "US State":

 <xs:simpleType name="state"> <xs:restriction base="xs:string"> <xs:enumeration value="AL" /> <xs:enumeration value="AK" /> <xs:enumeration value="AS" /> .... <xs:enumeration value="WY" /> </xs:restriction> </xs:simpleType> 

How can I once list the US State enumeration and reuse it in each of my address elements? I apologize in advance if this is a n00b question - I have never written XSD before.

My initial hit in it is as follows:

 <xs:element name="business_address" maxOccurs="1"> <!-- address fields go here --> <xs:element name="business_address_state" type="state" maxOccurs="1"></xs:element> </xs:element> 
+4
source share
2 answers

I think you're on the right track. I think this has more to do with XML namespaces. Try the following:

 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/foo" xmlns:tns="http://www.example.org/foo" elementFormDefault="qualified"> <xs:element name="business_address"> <xs:complexType> <xs:sequence> <xs:element name="business_address_state" type="tns:state" maxOccurs="1" /> </xs:sequence> </xs:complexType> </xs:element> <xs:simpleType name="state"> <xs:restriction base="xs:string"> <xs:enumeration value="AL" /> <xs:enumeration value="AK" /> <xs:enumeration value="AS" /> <xs:enumeration value="WY" /> </xs:restriction> </xs:simpleType> </xs:schema> 

Note that the tns: state type is not only a state

And here is how you use it:

 <?xml version="1.0" encoding="UTF-8"?> <business_address xmlns="http://www.example.org/foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/foo foo.xsd "> <business_address_state>AL</business_address_state> </business_address> 

Note that this XML uses the default namespace, the same as targetNamespace XSD

+5
source

While namespaces help organize schemas and prevent conflicts, this is not a namespace that allows reuse, it is placing the type as the immediate child of the <xs: schema> root, which makes it global. (Used in a namespace without a namespace qualifier and from anywhere, where the tns namespace is apparently w / tns: qualifier.)

I prefer to build my schemes in accordance with the Garden of Eden approach, which maximizes the reuse of both elements and types (and can also facilitate the external logical binding of a carefully made unique type / element, for example, from a stored data dictionary in the database.

Note that while the Garden of Eden scheme offers maximum reuse, it also includes most of the work. At the bottom of this article, I have provided links to other templates described in the blog series.

& bull; Approach from the Garden of Eden http://blogs.msdn.com/skaufman/archive/2005/05/10/416269.aspx

It uses a modular approach, defining all elements globally and, like the Venetian Blind method, all type definitions are declared globally. Each element is globally defined as an immediate child of a node, and its type attribute can be set to one of these complex types.
 <?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="TargetNamespace" xmlns:TN="TargetNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="BookInformation" type="BookInformationType"/> <xs:complexType name="BookInformationType"> <xs:sequence> <xs:element ref="Title"/> <xs:element ref="ISBN"/> <xs:element ref="Publisher"/> <xs:element ref="PeopleInvolved" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="PeopleInvolvedType"> <xs:sequence> <xs:element name="Author"/> </xs:sequence> </xs:complexType> <xs:element name="Title"/> <xs:element name="ISBN"/> <xs:element name="Publisher"/> <xs:element name="PeopleInvolved" type="PeopleInvolvedType"/> </xs:schema> 
The advantage of this approach is that schemes can be reused. Because both elements and types are globally defined, both are reusable. This approach offers the maximum amount of reusable content. The disadvantages are that the scheme is verbose. This would be a suitable design when creating shared libraries in which you can allow yourself to make any assumptions about the scale of elements and types of schemes and their use in other schemes, especially with regard to extensibility and modularity.


Since each individual type and element has one global definition, these canonical particles / components can be connected one-to-one with identifiers in the database. Although it might seem like a tedious routine at first glance to maintain the relationship between the text particles / XSD components and the database, SQL Server 2005 can actually generate canonical component identifiers through the statement

 CREATE XML SCHEMA COLLECTION 

http://technet.microsoft.com/en-us/library/ms179457.aspx

Conversely, to build a schema from canonical particles, SQL Server 2005 provides

 SELECT xml_schema_namespace function 

http://technet.microsoft.com/en-us/library/ms191170.aspx

sa · not · i · kal Related to math. (equations, coordinates, etc.), "in the simplest or standard form" http://dictionary.reference.com/browse/canonical

Other, simpler to construct, but less resultant / more "denormalized / redundant" schema schemes include

& bull; The Russian Doll approach http://blogs.msdn.com/skaufman/archive/2005/04/21/410486.aspx

There is one global element in the scheme - the root element. All other elements and types enter deeper, deeper, giving it a name due to the fact that each type is adjusted to what is above it. Because elements in this design are declared locally, they will not be reused using import or include statements.

& bull; Salami Slice Approach http://blogs.msdn.com/skaufman/archive/2005/04/25/411809.aspx

All elements are defined globally, but type definitions are defined locally. Thus, other schemes can reuse elements. With this approach, the global element with its locally defined type provides a complete description of the contents of the elements. This informational “slice” is declared individually and then aggregated back together and can also be brought together to build other schemes.

& bull; The Venetian Blind Approach http://blogs.msdn.com/skaufman/archive/2005/04/29/413491.aspx

Similar to Russian Doll approach, as they use one global element. The Venetian Blind approach describes a modular approach by naming and defining all type definitions globally (as opposed to the Salami Slice approach, which declares global elements and types locally). Each globally defined type describes an individual “tablet” and can be reused by other components. In addition, all locally declared elements can be either a qualified namespace or a namespace (sections can be “open” or “closed”) depending on the parameter parameter elementFormDefault at the top of the schema.
+2
source

All Articles