CS639 XML Schemas and
namespaces
XML document for pg. 103
payload of SOAP message: quote.xml in $cs639/validate-ns
<getQuote
xmlns="http://namespaces.cafeconleche.org/xmljava/ch2/">
<symbol>RHAT</symbol>
</getQuote>
Or for example,
quote1.xml
<q:getQuote
xmlns:q="http://namespaces.cafeconleche.org/xmljava/ch2/">
<q:symbol>RHAT</q:symbol>
</q:getQuote>
These documents have “logically equivalent namespaces” because the namespace id is identical. The fact that the prefix is different is not important to any XML processor that can handle prefixes. The prefix is a “dummy variable”, like the name of a method parameter. The first version above can also be processed by XML processors that don’t understand prefixes.
Example 2.21. A schema that assigns type to elements in the http://namespaces.cafeconleche.org/xmljava/ch2/ namespace
quote.xsd in $cs639/validate-ns
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace=http://namespaces.cafeconleche.org/xmljava/ch2/ (1)
xmlns=http://namespaces.cafeconleche.org/xmljava/ch2/ (2)
elementFormDefault="qualified"> (3)
<xsd:element name="getQuote">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="symbol" type="StockSymbol"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="StockSymbol"> (4)
<xsd:restriction base="xsd:string">
<!-- two to six upper case letters -->
<xsd:pattern value="[A-Z][A-Z][A-Z]?[A-Z]?[A-Z]?[A-Z]?"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
(1) The targetNamespace: this schema defines structure for elements with names in this namespace, including their attributes and content
(2) This makes the …/ch2 NS the default NS for this document, so that we don’t use prefixes on the names of this namespace (getQuote, symbol) in this schema doc
(3) This overrides the unfortunate default of elementFormDefault=”unqualified” that would make prefix use of element names depend on whether they are at top level in the schema or not.
(4) A simple type is a derivation from one of the provided XML schema types listed near the start of Chap. 2. Here string values are restricted to a certain regular expression pattern
Same schema, not using any default namespaces quote1.xsd in $cs639/validate-ns
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace=”http://namespaces.cafeconleche.org/xmljava/ch2/”
xmlns:x=”http://namespaces.cafeconleche.org/xmljava/ch2/”
elementFormDefault="qualified">
<xsd:element name="getQuote">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="symbol" type="x:StockSymbol" (5)
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="StockSymbol">
<xsd:restriction base="xsd:string">
<!-- two to six upper case letters -->
<xsd:pattern value="[A-Z][A-Z][A-Z]?[A-Z]?[A-Z]?[A-Z]?"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Same schema, using no xsd:, by using a default namespace for XML Schema names instead: quote2.xsd
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace=”http://namespaces.cafeconleche.org/xmljava/ch2/”
xmlns:x=”http://namespaces.cafeconleche.org/xmljava/ch2/”
elementFormDefault="qualified">
<element name="getQuote">
<complexType>
<sequence>
<element name="symbol" type="x:StockSymbol" (5)
maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<simpleType name="StockSymbol">
<restriction base="string">
<!-- two to six upper case letters -->
<pattern value="[A-Z][A-Z][A-Z]?[A-Z]?[A-Z]?[A-Z]?"/>
</restriction>
</simpleType>
</schema>
(5) We see a prefix in an attribute value
here. Such values are typed as xsd:QName
(see pg. 62) so that the XML processor knows what is going on. We could use this type in our own schemas,
but that’s an advanced topic. As you see here, the QName type allows reference
from an “element” element with name =“symbol” to a “simpleType” element with
name=”StockSymbol” describing the corresponding type. To see this QName type
declaration, we would have to look in the schema of schemas, meta-meta-data, available
from a link from the URL=URI http://www.w3.org/2001/XMLSchema