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>
Example 2.21. A schema that assigns type to elements in the http://namespaces.cafeconleche.org/xmljava/ch2/ namespace
quote.xsd
<?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.
(1) 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
<?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"
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"
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>