Philip van Gass
asked on
An insight into XML schemas and namespaces
Hi experts. Please have a look at the attached file which contains a script that creates a XML schema collection.
I am trying to understand the role played by each of the namespaces defined at the start.
There is a target namespace called 'Product ModelManuInstructions', which is also the default namespace because it has 'xmlns =' without a colon or a prefix.
Following on from this the XMLSchema namespace is also defined with the prefix 'xsd'.
My understanding is that the XMLSchema namespace will contain items that are used generally (or widely) by namespaces, e.g. element, choice, string, complex type, while the target namespace is more specific to the application viz. 'tool', 'material' and 'blueprint'.
Is this a correct interpretation ?
If so, does the collection always have to contain the XMLSchema namespace and could the target namespace have been omitted leaving the the default namespace on it's own or vice-versa ?
I am trying to understand the role played by each of the namespaces defined at the start.
There is a target namespace called 'Product ModelManuInstructions', which is also the default namespace because it has 'xmlns =' without a colon or a prefix.
Following on from this the XMLSchema namespace is also defined with the prefix 'xsd'.
My understanding is that the XMLSchema namespace will contain items that are used generally (or widely) by namespaces, e.g. element, choice, string, complex type, while the target namespace is more specific to the application viz. 'tool', 'material' and 'blueprint'.
Is this a correct interpretation ?
If so, does the collection always have to contain the XMLSchema namespace and could the target namespace have been omitted leaving the the default namespace on it's own or vice-versa ?
ASKER
But it looks to me like, in this example, the target namespace and the default namespace are one and the same. Do you agree, and if so why is this ?
If you are familiar with the Java programming language, think of namespaces like java packages. You have two elements of the same name: Customer and Customer. Now how do you know when to use one Customer or another? This is where namespaces come from. Customer with namespace a is different to customer with namespace b.
Think of the target namespace as the package where this file (the schema) is in. Think of the xmlns as the package where all elements belong.
But it looks to me like, in this example, the target namespace and the default namespace are one and the same. Do you agree, and if so why is this ?Yes. The target namespace indicates what particular namespace this schema file describes. The xmlns is the default namespace for all elements in the schema which do not explicitly define another namespace.
Think of the target namespace as the package where this file (the schema) is in. Think of the xmlns as the package where all elements belong.
ASKER
And what is the purpose of the XMLSchema namespace ?
As I said it is necessary to distinguish different elements having the same tag name, but belong to a different entity than the XML Schema language.
xmlns:xsd is used to declare that all elements used are from the namespace 'http://www.w3.org/2001/XMLSchema'. So basically the XML Schema language.
targetNamespace and xmlns is used to identify the namespace of the declared elements. So that you can declare elements named "element" in your own schema. Both having the same value is an artifact of XML and the best practice to use no namespace prefix.
You can include an namespace prefix here, then your XSD must look like
The difference is in the step declaration, where you need to specify the FQN of the type. Thus using no prefix makes writing and reading simpler.
<xsd:schema
targetNamespace="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
xmlns:xsd is used to declare that all elements used are from the namespace 'http://www.w3.org/2001/XMLSchema'. So basically the XML Schema language.
targetNamespace and xmlns is used to identify the namespace of the declared elements. So that you can declare elements named "element" in your own schema. Both having the same value is an artifact of XML and the best practice to use no namespace prefix.
You can include an namespace prefix here, then your XSD must look like
<xsd:schema
targetNamespace="ProductModelManuInstructions"
xmlns:pmmi="ProductModelManuInstructions"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
<xsd:complexType name="StepType" mixed="true" >
<xsd:choice minOccurs="0" maxOccurs="unbounded" >
<xsd:element name="tool" type="xsd:string" />
<xsd:element name="material" type="xsd:string" />
<xsd:element name="blueprint" type="xsd:string" />
<xsd:element name="specs" type="xsd:string" />
<xsd:element name="diag" type="xsd:string" />
</xsd:choice>
</xsd:complexType>
<xsd:element name="root">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="Location" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="step" type="pmmi:StepType" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="LocationID" type="xsd:integer" use="required"/>
<xsd:attribute name="SetupHours" type="xsd:decimal" use="optional"/>
<xsd:attribute name="MachineHours" type="xsd:decimal" use="optional"/>
<xsd:attribute name="LaborHours" type="xsd:decimal" use="optional"/>
<xsd:attribute name="LotSize" type="xsd:decimal" use="optional"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The difference is in the step declaration, where you need to specify the FQN of the type. Thus using no prefix makes writing and reading simpler.
ASKER
Two questions now:
(1) When creating a XML schema collection should you always include the namespace 'http://www.w3.org/2001/XMLSchema'.
(2) What does FQN stand for ?
(1) When creating a XML schema collection should you always include the namespace 'http://www.w3.org/2001/XMLSchema'.
(2) What does FQN stand for ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops.
ASKER
I have been reading a document by Aaron Skonnard at msdn.microsoft.com/en-us/l ibrary/aa4 68565.aspx which explains namespaces very well.
Also msdn.microsoft.com/en-us/l ibrary/aa4 68557.aspx
Also msdn.microsoft.com/en-us/l
ASKER
Is there any reason why you would express the data fields as elements instead of attributes or vice-versa in an XML document, other than personal preference ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, I would call it philosophy ;)
Imho attributes should only carry invariant data or keys. The real data should be in elements.
Attributes should be mandatory. Optional data should be in elements.
There are two things you should balance: Depth of strucutre and human readability.
Imho attributes should only carry invariant data or keys. The real data should be in elements.
Attributes should be mandatory. Optional data should be in elements.
There are two things you should balance: Depth of strucutre and human readability.
ASKER
I am closing now. Thanks to both of you for your contributions.
XML namespaces provide a method to avoid element name conflicts. XML elements with the same name should denote the same data type with the same domain.
Consider having different entities with the same property name, but a different domain. Or even a different data type. This would make parsing and handling data more complicated, cause you need conditionals in that tag handler.
Having a different namespace for each entity means that this conditional is no longer needed, cause it is replaced by different handlers.
To validate a XML document beyond well-formed-ness, we can use DTD (document type definition) or XML Schema. Both describe the required structure and more of a XML document. XML schema itself is XML, whereas DTD is its own language. Thus XML schema can be processed with your normal XML tools.
So XML Schema defines the elements like element, choice, string, complex type. Whereas the XML schema namespace is needed to allow the same elements for a different entity/namespace.