Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

XML Schema-part 1

VIEWS: 20 PAGES: 24

									XML Schema

   Part 1
                 What is it?
An XML Schema defines the legal structure that an
  XML document may take, much like a blueprint.
  This is accomplished by:
• defining elements and child elements
• defining attributes
• defining the order of child elements
• defining element content
• defining data types for elements and attributes
• defining default and fixed values
              Schema vs. DTD
•   Schemas are extensible.
•   Schemas have more depth and power.
•   Schemas are written in XML.
•   Schemas support data types.
•   Schemas support namespaces.
      Why Support Data Types?
• Easier to describe allowable document
  content.
• Easier to validate data.
• Easier to work data from a database.
• Easier to define data restrictions.
• Easier to define data formats.
• Easier to convert data between types.
    Benefits of XML and Schemas
• You don’t need to learn a new language.
• Editor of choice will work on Schemas.
• Current parser will work on Schemas.
• You can manipulate your Schema with the
  XML DOM.
• Schemas are transformable with XSLT.
       Schemas are Extensible
• Reuse Schemas in other Schemas.
• Create proprietary data types from public
  types.
• Reference multiple Schemas in one document.
          Data Bound Checking
• According to W3Schools, doing format
  checking with your data isn’t enough.
  Schemas offer bound checking.

Ex: you order 5 gross of laser printers, instead of
  5 laser printers.
               XML - Note
<?xml version="1.0"?> <note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
            XML – Note DTD
<!ELEMENT note (to, from, heading, body)
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
                  XML – Note Schema
<?xml version="1.0"?> <xs:schema
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   targetNamespace="http://www.w3schools.com"
   xmlns="http://www.w3schools.com" elementFormDefault="qualified">
<xs:element name="note">
   <xs:complexType>
        <xs:sequence>
                <xs:element name="to" type="xs:string"/>
                <xs:element name="from" type="xs:string"/>
                <xs:element name="heading" type="xs:string"/>
                <xs:element name="body" type="xs:string"/>
        </xs:sequence>
   </xs:complexType>
</xs:element>
</xs:schema>
           Reference to DTD
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
  "http://www.w3schools.com/dtd/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
           Reference to Schema
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-
  instance"
  xsi:schemaLocation="http://www.w3schools.com
  note.xsd">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
        <schema> Element
• The <schema> element forms the root
  element for each XML Schema document.

<?xml version=“1.0”?>
<xs:schema>
<!—Stuff goes here
</xs:schema>
                                Attributes
Attribute              Description
id                     Optional. Creates an unique ID for the element.
attributeFormDefault   Optional. The form for attributes declared in target name space of
                       the schema. Must be either “qualified” or “unqualified”
elementFormDefault     Optional. The form for elements declared in the target name space
                       of the schema. Must either be “qualified” or “unqualified”
blockDefault           Optional. Specifies the default value of the block attribute on
                       element and complexType elements in namespace.
finalDefault           Optional. Specifies the default value of the final attribute on
                       element, simpleType, and complexType elements in the namespace.
targetNamespace        Optional. An URI ref to the namespace of this schema.
version                Optional. Specifies the version of the schema.
xmlns                  A URI ref specifying one or more namespaces for the schema.
any attributes         Optional. Specifies other attributes with non-schema namespace.
           Schema Element
<?xml version="1.0"?>
<xs:schema
  xmlns:xs="http://www.w3.org/2001/XMLSche
  ma"
  targetNamespace="http://www.w3schools.co
  m" xmlns="http://www.w3schools.com"
  elementFormDefault="qualified“>
<!—Stuff goes here
</xs:schema>
            Fragment: xmlns
xmlns:ns=“http://www.w3.org/2001
  /XMLSchema”
• “” is the URI to the used namespace for the
  elements in the schema.
• ‘xs’ denotes that all data types and elements
  from the namespace are to be prefixed with
  xs. Ex <xs:myelement>.
    Fragment: targetNamespace
targetNamespace=“http://www.w3sc
 hools.com”

• The elements defined by this schema come
  from the namespace used in this attribute.
            Fragment: xmlns
xmlns=“http:www.w3schools.com”

• Indicates the default namespace.
  Fragment: elementFormDefault
elementFormDefault=“qualified”

• All elements used by the XML instance
  document declared by the schema must be
  namespace qualified.
        Referencing a Schema
<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
  xmlns:xsi="http://www.w3.org/2001/XML
  Schema-instance"
  xsi:schemaLocation="http://www.w3scho
  ols.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this
  weekend!</body>
</note>
             Fragment: xmlns
xmlns=“http://www.w3schools.com”

• Specifies the default namespace. This
  declaration states that all elements in the
  document are declared in the target
  namespace.
              Fragment: xsi
xmlns:xsi=“http://www.w3.org/200
 1/XMLSchema-instance”

• Declares the XML Schema Instance
  namespace and makes it available.
      Fragment: schemaLocation
xsi:schemaLocation=“http://www.w
 3schools.com note.xsd”

• This attribute has two values. The first value
  specifies the namespace to use, while the
  second is the location of the XML schema to
  use for that particular namespace.
               Reference
W3Schools.org. May 22, 2007.
 http://www.w3schools.com/xsl/xsl_functions.
 asp

								
To top