Introduction to XML - PowerPoint PPT Presentation

1 / 97
About This Presentation
Title:

Introduction to XML

Description:

bug Solving Name Conflicts with Namespaces ... Attributes are specified in a similar fashion: xs:attribute name='name' type='type' ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 98
Provided by: mariostz
Category:
Tags: xml | introduction

less

Transcript and Presenter's Notes

Title: Introduction to XML


1
Introduction to XML
  • Marios Tziakouris
  • University of Cyprus
  • EPL602
  • Fall 2004

2
Introduction to XML
  • XML stands for eXtensible Markup Language.
  • XML is a way of specifying self-describing data
    in a human-readable format.
  • XMLs structure is like that of HTML it
    consists of nested tags, or elements. Elements
    can contain text data, attributes, and other
    elements.
  • XML is ideal for describing hierarchical data.

3
The Components of XML
  • XML document can be comprised of the following
    components
  • Processing Directives
  • A Root Element (required)
  • Elements
  • Attributes
  • Comments

4
An Example XML Document
  • lt?xml version"1.0" encoding"UTF-8" ?gt
  • ltbooksgt
  • lt!-- Each book is represented by a ltbookgt
    element --gt
  • ltbook price"34.95"gt 
  • lttitlegtTeach Yourself ASP 3.0 in 21
  • Dayslt/titlegt
  • ltauthorsgt ltauthorgtMitchelllt/authorgt  
  • ltauthorgtAtkinsonlt/authorgt  
  • lt/authorsgt 
  • ltyeargt1999lt/yeargt  
  • lt/bookgt
  • lt/booksgt

5
An Example XML Document
  • The first line of an XML document is referred to
    as a processing directive, and indicates the
    version of the XML standard being used and the
    encoding.lt?xml version"1.0" encoding"UTF-8"
    ?gt
  • In general, processing directives have the
    following syntax
  • lt?processing directive content ?gt

6
An Example XML Document
  • All XML Documents must have precisely one root
    element. The first element in the XML document
    is referred to as the root element.
  • For the books example, the root element is
    ltbooksgt
  • lt?xml version"1.0" encoding"UTF-8" ?gt
  • ltbooksgt
  • lt/booksgt

7
An Example XML Document
  • XML documents can contain an arbitrary number of
    elements within the root element. Elements have
    a name, can have an arbitrary number of
    attributes, and can contain either text content
    or further elements.
  • The generic syntax of an element is given as
  • ltelementName attribute1"value1"
    attributeN"valueN"gt text content or further
    elements
  • lt/elementNamegt

8
An Example XML Document
  • For example, in the books XML document, the
    ltbookgt element contains an attribute named price
    and three child elements lttitlegt, ltauthorsgt, and
    ltyeargt.
  • ltbook price"34.95"gt 
  • lttitlegtlt/titlegt
  • ltauthorsgt
  • lt/authorsgt 
  • ltyeargtlt/yeargt  
  • lt/bookgt

9
An Example XML Document
  • The lttitlegt and ltyeargt elements contain text
    content.
  • lttitlegtTeach Yourself ASP 3.0 in 21 Dayslt/titlegt
  • ltyeargt1999lt/yeargt

10
An Example XML Document
  • The ltauthorsgt element contains two ltauthorgt child
    elements. The ltauthorgt elements contain text
    content, specifying the author(s) of the book
  • ltauthorsgt ltauthorgtMitchelllt/authorgt  
  • ltauthorgtAtkinsonlt/authorgt  
  • lt/authorsgt

11
An Example XML Document
  • XML comments are delimited by lt!-- and --gt. XML
    comments are ignored by an XML parser and merely
    make the XML document easier to read for a
    human.lt!-- Each book is represented by a ltbookgt
    element --gt

12
XML Formatting Rules
  • XML is case sensitive.
  • XML Documents must adhere to specific formatting
    rules. An XML document that adheres to such
    rules is said to be well-formed.
  • These formatting rules are presented over the
    next few slides.

13
Matching Tags
  • XML elements must consist of both an opening tag
    and a closing tag. For example, the element
    ltbookgt has an opening tag - ltbookgt - and must
    also have a matching closing tag - lt/bookgt.
  • For elements with no contents, you can use the
    following shorthand notation
  • ltelementName optionalAttributes /gt

14
Elements Must be Properly Nested
  • Examples of properly nested elements
  • ltbook price"39.95"gt 
  • lttitlegtXML and ASP.NETlt/titlegt
  • lt/bookgt
  • Example of improperly nested elements
  • ltbook price"39.95"gt 
  • lttitlegtXML and ASP.NET
  • lt/bookgt lt/titlegt

15
Attribute Values Must Be Quoted
  • Recall that elements may have an arbitrary number
    of attributes, which are denoted as
  • ltelementName attribute1"value1" ...
    attributeN"valueN"gt lt/elementNamegt
  • Note that the attribute values must be delimited
    by quotation marks. That is, the following is
    malformed XML
  • ltbook price39.95gt lt/bookgt

16
Illegal Characters for Text Content
  • Recall that elements can contain text content or
    other elements. For example, the lttitlegt element
    contains text, providing the title of the book
    lttitlegtXML for ASP.NETlt/titlegt
  • There are five characters that cannot appear
    within the text content lt, gt, , ", and '.

17
Replacing the Illegal Characters with Legal Ones
  • If you need to use any of those four illegal
    characters in the text portion of an element,
    replace them with their equivalent legal value

18
Namespaces
  • Element names in an XML document can be invented
    by the XML documents creator.
  • Due to this naming flexibility, when computer
    programs work with different XML documents there
    may be naming conflicts.
  • For example, appending two XML documents together
    that use the same element name for different data
    representations leads to a naming conflict.

19
Naming Conflicts
  • For example, if the following two XML documents
    were to be merged, there would be a naming
    conflict

ltbuggt ltdategt2003-05-22lt/dategt
ltdescriptiongtThe button, when clicked, raises a
GPF exception.lt/descriptiongt lt/buggt
ltbuggt ltgenusgtHomopteralt/genusgt
ltspeciesgtPseudococcidaelt/speciesgt lt/buggt
20
Solving Name Conflicts with Namespaces
  • A namespace is a unique name that is prefixed
    to each element name to uniquely identify it.
  • To create namespaces, use the xmlns attribute
  • ltnamespace-prefixelementName xmlnsnamespace-pref
    ix"namespace"gt
  • E.g.
  • ltanimalbug xmlnsanimal"http//www.bugs.com/"gt

21
Namespaces
  • Note that the xmlns namespace attribute contains
    two parts
  • The namespace-prefix and,
  • The namespace value.
  • The namespace-prefix can be any name. The
    namespace-prefix is then used to prefix element
    names to indicate that the element belongs to a
    particular namespace.
  • The namespace for an element automatically
    transfers to its children elements, unless
    explicitly specified otherwise.

22
Namespaces
  • lthtable xmlnsh"http//www.w3.org/TR/html4/"gt
  • lthtrgt
  • lthtdgtAppleslt/htdgt lthtdgtBananaslt/htdgt
  • lt/htrgt
  • lt/htablegt

23
Namespaces
  • The namespace value must be a globally unique
    string. Typically, URLs are used since they are
    unique to companies/developers. However, any
    string value will suffice, so long as it is
    unique.

24
The Default Namespace
  • To save typing in the namespace-prefix for every
    element, a default namespace can be specified
    using xmlns without the namespace-prefix part,
    like
  • ltelementName xmlns"namespace"gt

25
Default Namespaces
  • lttable xmlns"http//www.w3.org/TR/html4/"gt lttrgt
  • lttdgtAppleslt/tdgt
  • lttdgtBananaslt/tdgt
  • lt/trgt
  • lt/tablegt

26
Namespaces in Action
  • Note that most of our XML examples will not use
    namespaces. Always using namespaces is a good
    habit to get into, but can add confusion/clutter
    to examples when learning (Hence their omission
    where possible.)

27
Comparing XML to HTML
  • HTML, or HyperText Markup Language, is the syntax
    used to describe a Web pages appearance.
  • At first glance, there appears to be a lot in
    common between HTML and XML their syntax
    appears similar, they both consist of elements,
    attributes, and textual content, and so on.

28
Comparing XML to HTML
  • However, XML and HTML are more different than
    they are alike.
  • With XML, you must create your own elements.
    With HTML, your elements must come from a
    predefined set of legal elements (ltbgt, ltpgt,
    ltbodygt, etc.)
  • XML is used solely to describe data other
    technologies must be used if you want to present
    the XML in a formatted manner.
  • HTML describes primarily presentation. That is,
    you use the ltbgt tag to make something bold, not
    to indicate its meaning.

29
Comparing XML to HTML
  • XML documents must be well-formed, while HTML
    documents are not confined to such rigorous
    rules.
  • The following legal HTML is illegal XML (why?)
  • ltp aligncentergtltbgt
  • I said, Hello, World!
  • lt/Pgtlt/bgt
  • Tags improperly nested
  • Unquoted attribute value
  • ltpgt tag and lt/Pgt do not match (due to
    case-sensitivity)
  • Contains illegal characters (") in text

30
Comparing XML to HTML
  • Realize that XML is not a replacement to HTML.
    XML and HTML have different aims XML was
    created to describe data, HTML was designed to
    present data.

31
Comparing XML to Traditional Databases
  • Imagine that we have information about books
    stored in two formats
  • In a traditional database, say Microsoft SQL
    Server 2000.
  • In an XML document
  • What advantages does having the data represented
    as XML provide?

32
XMLs Benefits Over Traditional Databases
  • XML data is human-readable. A human glancing at
    the XML document can determine what data each
    element and attribute indicates.
  • XML is self-describing. With a traditional
    database, you would need to view the table
    schemas to determine the purpose of the data.
    With XML, the purpose is reflected by the names
    of the elements and their nesting.

33
XMLs Benefits Over Traditional Databases
  • Also, since XML is naturally hierarchical,
    determining the relationship among entities is
    much simpler in an XML document than in a
    relational database.
  • XML data is simply text content, meaning the data
    is platform-neutral. For example, the XML book
    data could be utilized by a program running on a
    Linux box, whereas the raw SQL Server 2000 data
    is only useful to a Windows box running SQL
    Server 2000.

34
Creating Our First XML Document
  • Imagine that we wanted to be able to catalog our
    music collection. Being interested in learning
    XML, we decide to create an XML document that
    stores the information about our music library.
  • First, we must decide what elements our XML
    document will consist of

35
Creating the XML Document with Visual Studio .NET
  • Visual Studio .NET makes it easy to create XML
    files. Go to File gt New gt File and select the
    XML File option from the New File dialog box

36
Creating the XML Document with Visual Studio .NET
  • This will create a blank XML file with the lt?xml
    ?gt preprocessing directive.
  • At this point, you can type in the elements for
    the XML document. Note that after typing in an
    opening tag, VS.NET automatically adds the
    corresponding closing tag.

37
A Peek Ahead Defining the XML Documents Schema
  • Realize that we have yet to formally specify the
    structure of the XML document.
  • The particular data model is known only to us.
  • XML documents can contain schemas that describe
    their structure.
  • We will discuss XML Schemas in greater detail
    later.
  • Visual Studio .NET allows us to enter any XML
    content we want, even content that violates the
    schema

38
An Hierarchical Way to Think of XML Documents
  • When working with XML documents, it helps to
    think of these documents as a hierarchy, where
    the top of the hierarchy is the root element.
  • At each level in the hierarchy there are
    elements, attributes, or text content.
  • This hierarchy-model of XML documents is commonly
    used in accessing XML documents programmatically,
    and hence is worth examining.

39
What is the DOM?
  • DOM stands for Document Object Model, and its a
    model that can be used to describe an XML
    document.
  • The DOM expresses the XML document as a hierarchy
    of elements, where each element can have zero to
    many children elements.
  • The text content and attributes of an element are
    expressed as its children as well.

40
Why Discuss the DOM?
  • Being able to think of an XML document in terms
    of the DOM helps in understanding how XPath,
    XSLT, and related concepts work.
  • Also, later we will examine how to access an XML
    document programmatically. When working with XML
    documents this way, the XML documents are
    presented as a DOM.

41
Example XML File
lt?xml version"1.0" encoding"UTF-8"
?gt ltbooksgt ltbook price"34.95"gt  lttitlegtTYASP
3.0lt/titlegt ltauthorsgt ltauthorgtMitchelllt/a
uthorgt   lt/authorsgt  lt/bookgt ltbook
price29.95"gt  lttitlegtASP.NET
Tipslt/titlegt ltauthorsgt ltauthorgtMitchelllt/
authorgt ltauthorgtWaltherlt/authorgt ltauthorgtSev
enlt/authorgt lt/authorsgt  lt/bookgt lt/booksgt
42
The DOM View of the XML Document
43
Understanding the DOM
  • With the DOM model we can think of each XML
    element as having an arbitrary number of children
    elements.
  • Children element are comprised of both other
    elements as well as the text content and
    attributes.
  • Therefore, to visit every element in an XML
    document we could start with the root element and
    visit each of its children. Then, for each child
    being visited, we can recursively visit all of
    its children.

44
XML Schemas
  • Marios Tziakouris
  • University of Cyprus
  • EPL602
  • Fall 2004

45
Defining XML Document Structure
  • Relational databases employ schemas, which
    explicitly define the structure and properties of
    the data a table can hold.
  • With XML, defining the document structure is
    optional, but highly encouraged.
  • XML allows for its structure to be defined using
    either one of two technologies
  • DTDs (Document Type Definition)
  • XSD (XML Schema Definition)

46
Disadvantages of DTDs
  • DTDs are not in XML format
  • DTDs cannot specify text data any more specific
    than PCDATA (text only). That is, for a text
    element, we cannot use, say, a regular expression
    to indicate the valid pattern(s) for an email
    address.
  • Names in DTDs must be unique meaning we
    couldnt use a DTD to describe a filesystem in
    XML. (Since we had a ltfoldergt element that could
    have a ltfoldergt child.)

47
DTDs are Outdated
  • DTDs are outdated today people use XSD, or XML
    Schema.
  • Well focus much more attention on XML Schemas,
    and validating XML documents against XML Schemas.
  • However, for more information on DTDs, check out
    http//www.w3schools.com/dtd

48
Why Define the XML Documents Structure?
  • Defining an XML documents structure is essential
    when
  • Working on a project involving XML with other
    developers.
  • Building a program that sends XML data to, or
    accepts XML data from, another program.

49
Why Define the XML Documents Structure?
  • Imagine that you are building a program that
    opens and XML file containing an arbitrary number
    of contact information and sends a personalized
    email to each contact.
  • Perhaps you expect the XML to be in the following
    structure

ltcontactsgt ltcontactgt ltnamegt Contacts
Namelt/namegt ltemailgtContacts Email
Addresslt/emailgt ltinterestsgt ltinterest
level"InterestLevelValue"gtInterestlt/interestgt
lt/interestsgt lt/contactgt lt/contactsgt
50
Why Define the XML Documents Structure?
  • Perhaps we expect InterestLevelValue to be an
    integer between 1 and 5.
  • Perhaps we want to enforce that there is at least
    one ltinterestgt element, but no more than five.
  • If we are working with other developers, or other
    programs are producing this XML that we plan on
    consuming, how to we let others know what XML
    definition we expect?
  • We can tell them or,
  • We can specify the definition explicitly
    (preferred)

51
An Overview of XSD
  • XSD allows for richer XML document definition
    using XML formatting.
  • XSD allows for stricter definition of attribute
    and text node types (such as saying, An
    attribute value must be a positive integer, or,
    The text value must conform to the following
    regular expression.)
  • XSD allows for various XML elements with the same
    name (something DTDs do not allow for).

52
Advantages of XSD over DTDs
  • XSDs are specified in XML format, meaning they
    can be parsed using any standard XML parser.
  • There is strong tool-support for XSD in the .NET
    Framework
  • Can easily validate XML documents against an XSD
    schema
  • Can quickly generate XSD schemas based on
    existing XML content

53
The Components of XSD
  • Keep in mind that XSDs are XML-formatted
    documents. So, they need a root for the document
    XSD uses the tag ltxsschemagt as the root
    element.

ltxsschema xmlnsxs"http//www.w3.org/2001/XML
Schema" targetNamespace"namespace"
elementFormDefault"(qualifiedunqualified)"
attributeFormDefault"(qualifiedunqualified)"gt
54
An Example
  • Imagine that we wanted to create an XML Schema
    for the following XML document
  • ltemployeegtBob Smithlt/employeegt
  • ltemployeegtJane Doelt/employeegt
  • ltemployeegtEdward Smithlt/employeegt
  • ltemployeegtBonita Jacksonlt/employeegt
  • ltemployeegtJohnny B. Goodlt/employeegt

55
An Example
  • Clearly our XML document allows for an arbitrary
    number of ltemployeegt elements, each of whose type
    is string. This could be expressed in the
    following XML Schema

ltxsschema targetNamespace"http//foo"
xmlnsxs"http//www.w3.org/2001/XMLSchema"gt
ltxselement name"employee" type"xsstring"gt lt/xs
schemagt
56
The XML Schema Namespace
  • The ltschemagt element must specify a prefix for
    the namespace http//www.w3.org/2001/XMLSchema.
  • In the previous example, the namespace prefix xs
    is used. This is the namespace prefix VS.NET
    uses by default. However, any prefix can be used
    (xsd is another common one), so long as its
    namespace value is the above URL.
  • This namespace is the namespace for the elements
    used to denote an XML Schema elementType,
    simpleType, element, attributeType, etc.

57
Specifying Elements
  • To add an element to an XML documents
    definition, use the ltelementgt element.
  • ltelementgt elements have name and type attributes.
  • The name specifies the name of the element,
  • Type specifies the elements type.

58
Specifying Attributes
  • Attributes are specified in a similar fashion
  • ltxsattribute name"name" type"type"gt
  • Specifying that an attribute belongs to an
    element is a bit more complex, though well
    examine how to do this shortly

59
Primitive Types
  • The previous example denoted that the ltemployeegt
    element could accept string text content using
    the type xsstring.
  • Elements (and attributes) both have a type
    associated with them, which can be a primitive
    type, a simple type, or a complex type.

60
Primitive Types
  • Primitive types are those types that are built-in
    to the XML Schema. These include, but are not
    limited to
  • string
  • date
  • integer
  • positiveInteger

61
Primitive Types
The following is the datatype hierarchy for XML
Schemas primitive types
62
Using Primitive Types
  • To specify that an element or attribute is of a
    primitive type, simply specify the primitive
    types name in the type attribute. For example
  • ltxselement name"title" type"xsstring" /gt
  • -or-
  • ltxsattribute name"price" type"xsdecimal" /gt

63
Simple Types
  • Simple types are named primitive types that can
    have one or more facets.
  • A facet describes a finer characteristic of the
    data type it is defined on
  • For example, the integer data type allows for
    positive or negative whole numbers. A facet
    might specify that the value of the data type not
    only be an integer, but also between the values
    100 and 100.

64
Creating Simple Types
  • Simple types can be created with the ltsimpleTypegt
    element. They have the form
  • ltxssimpleType name"name"gt
  • ltxsrestriction base"type"gt
  • facet
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt

65
Specifying Facets
  • Facets are declared via the following syntax
  • ltxsfacetName value"value"gt
  • There are a number of available facets, each
    designed to describe various characteristics for
    a variety of data types.

66
Pattern Facets
  • The ltpatterngt facet allows for a regular
    expression to be used to indicate the legal
    string values for a type. For example, the
    following simple type would require a string
    input that is of the form 5 digits followed by a
    hyphen followed by 4 digits

ltxssimpleType name"USZipCode"gt
ltxsrestriction base"xsstring"gt ltxspattern
value"\d5-\d4"gt lt/xsrestrictiongt lt/xs
simpleTypegt
67
Length-Restricting Facets
  • The ltlengthgt, ltmaxLengthgt, and ltminLengthgt facets
    specify the length of a string.

ltxslength value"n" /gt ltxsmaxLength value"n"
/gt ltxsminLength value"n" /gt
ltlengthgt specifies the exact length a string must
be, while ltminLengthgt and ltmaxLength specify the
lower and upper bounds.
68
Example
  • The following simple type requires that the
    string be between 4 and 8 characters long.

ltxssimpleType name"pwd"gt ltxsrestriction
base"xsstring"gt ltxsminLength value"4"gt
ltxsmaxLength value"8"gt lt/xsrestrictiongt lt/xs
simpleTypegt
69
Numeric Facets
  • There are a number of facets for numeric data as
    well.
  • lttotalDigitsgt specifies the exact number of
    digits allowed
  • ltmaxExclusivegt/ltmaxInclusivegt specify the maximum
    numeric value
  • ltminExclusivegt/ltminInclusivegt specify the minimum
    numeric value

70
Example
  • What does the following simple type allow?

ltxssimpleType name"???"gt ltxsrestriction
base"xsinteger"gt ltxsminInclusive
value"0"gt ltxsmaxExclusive value"120"gt
lt/xsrestrictiongt lt/xssimpleTypegt
71
Enumeration Facets
  • The ltenumerationgt facet can be used to specify
    that a value can only come from a set of legal
    values.

ltxsenumeration value"allowedValue1"
/gt ltxsenumeration value"allowedValue2"
/gt ... ltxsenumeration value"allowedValueN" /gt
72
Example
  • The following simple type only allows for a
    subset of values

ltxssimpleType name"favTech"gt ltxsrestriction
base"xsstring"gt ltxsenumeration
value"ASP.NET"gt ltxsenumeration
value"JSP"gt ltxsenumeration value"PHP"gt
lt/xsrestrictiongt lt/xssimpleTypegt
73
Assigning Simple Types to Elements and Attributes
  • There are two methods for assigning a simple type
    to an element/attribute
  • Named simple types first, create the simple
    type using the ltsimpleTypegt element, setting the
    name attribute. Then, refer to the name
    attribute in the element/attributes type
    attribute.
  • Use an anonymous type in the element/attribute
    element.

74
Example of Using Named Simple Types
ltxssimpleType name"favTech"gt ltxsrestriction
base"xsstring"gt ltxsenumeration
value"ASP.NET"gt ltxsenumeration
value"JSP"gt ltxsenumeration value"PHP"gt
lt/xsrestrictiongt lt/xssimpleTypegt ltxselement
name"tech" type"favTech" /gt
75
Example of Using Anonymous Types
ltxselement name"tech"gt ltxssimpleTypegt
ltxsrestriction base"xsstring"gt
ltxsenumeration value"ASP.NET"gt
ltxsenumeration value"JSP"gt
ltxsenumeration value"PHP"gt
lt/xsrestrictiongt lt/xssimpleTypegt lt/xselementgt
76
The Benefit of Named Types
  • The benefit of named types is that multiple
    ltelementgt elements can use the same simple type
    definition as opposed to each having to repeat
    the type definition anonymously.

77
Complex Types
  • Complex types are similar to simple types in the
    declaration syntax
  • ltxscomplexType name"name"gt
  • ltelement Children Typegt
  • One to Many ltelementgt tags
  • lt/element Children Typegt
  • lt/xscomplexTypegt

78
Defining Element Children
  • All elements that contain children might have a
    set of children, all are required in a specific
    order.
  • An element might require that only one child from
    a list of legal children be present.
  • Or an element might allow for its children to
    optionally appear, and to appear in any order.

79
Using Sequences
  • To specify that all children must be present in
    the specified order, use ltsequencegt (that is, all
    elements with type mailType in the XML document
    must first have a lttogt element, then a ltfromgt
    element, and finally a ltbodygt element)
  • ltxscomplexType name"mailType"gt
  • ltxssequencegt
  • ltxselement name"to" type"xsstring" /gt
  • ltxselement name"from" type"xsstring" /gt
  • ltxselement name"body" type"xsstring" /gt
  • lt/xssequencegt
  • lt/xscomplexTypegt

80
Using Sequences
  • Assume an ltelementgt element is used defining and
    element named ltmailgt of type mailType. Would the
    following XML document be schema-valid?
  • ltmailgt
  • ltfromgtbob_at_bob.comlt/fromgt
  • ltbodygtHi, Bob!lt/bodygt
  • lt/mailgt

No! Because the lttogt element is missing. All
children elements must be present for ltsequencegt.
81
Using Sequences
  • What about the following XML document would it
    be schema-valid?
  • ltmailgt
  • ltfromgtbob_at_bob.comlt/fromgt
  • ltbodygtHi, Bob!lt/bodygt
  • lttogtmarios_at_marios.comlt/togt
  • lt/mailgt

No! Because the children elements do not appear
in the correct order (lttogt then ltfromgt then
ltbodygt)
82
Using Sequences
  • The following is an example of an schema-valid
    XML document.
  • ltmailgt
  • lttogtmarios_at_marios.comlt/togt
  • ltfromgtbob_at_bob.comlt/fromgt
  • ltbodygtHi, Bob!lt/bodygt
  • lt/mailgt

83
XML Schema Examples
  • In the next few slides, well look at some XML
    schemas and determine the XML data definition
    they provide.
  • Also, after these few examples, well build an
    XML Schema from scratch

84
ltxsschema targetNamespace"http//foo"
xmlnsxs"http//www.w3.org/2001/XMLSchema"gt
ltxselement name"books"gt ltxscomplexTypegt
ltxssequence maxOccurs"unbounded"gt
ltxselement name"book"gt
ltxscomplexTypegt ltxssequencegt
ltxselement name"title"
type"xsstring" /gt
ltxselement name"year"
type"xsinteger" /gt lt/xssequencegt
ltxsattribute name"price"
type"xsdecimal" /gt
lt/xscomplexTypegt lt/xssequencegt
lt/xscomplexTypegt lt/xselementgt lt/xsschemagt
85
Creating an XML Schema
  • Imagine that we wanted to create an XML file to
    store information about our music collection.
  • What elements/attributes will we use?
  • What facets?
  • Lets create an XML Schema from the ground up to
    define this XML document

86
Using Visual Studio .NET to Build an XML Schema
  • Visual Studio .NET provides an editor for
    creating XML Schemas.
  • To create a new XML Schema file go to
    File/New/File and choose the XML Schema file type

87
Using Visual Studio .NET to Build an XML Schema
  • The editor has two tabs Schema and XML.
  • The Schema tab allows you to drag and drop schema
    elements to generate the schema.
  • The XML tab shows the current XML Schemas
    content, and allows you to edit the content in
    this manner.

88
Using Visual Studio .NET to Build an XML Schema
  • Lets create a schema to describe XML information
    about orders made on an ecommerce system.
  • Start by creating our root element, ltordersgt, by
    dragging and dropping an element from the XML
    Schema toolbox onto the designer.
  • Enter the name orders for the newly created
    elements name.

89
Using Visual Studio .NET to Build an XML Schema
  • Now, we want to express that there can be 1 to
    many ltordergt elements as children of ltordersgt.
    So, we want to add a sequence.
  • Drag and drop a group element into the ltordersgt
    element in the designer. (Group elements are
    used to signify sequences, choice, and all
    groups.)
  • In the property window, set the maxOccurs
    property to unbounded for the sequence.
  • Finally, to create the ltordergt element, drag and
    drop an element from the toolbox into the Group
    box and name the element order.

90
Our Progress Thus Far
91
Using Visual Studio .NET to Build an XML Schema
  • Now, lets let each ltordergt have two children
    elements
  • ltitemsgt - a collection of items ordered
  • ltcustomergt - the customer who made the order.
  • Well add a sequence group to the ltordergt element
    to achieve this
  • Also, the ltordergt element will have a date
    attribute of type xsdate.

92
Using Visual Studio .NET to Build an XML Schema
  • To the newly created sequence group, add the date
    attribute.
  • To the newly added sequence group, add the
    ltitemsgt and ltcustomergt elements.
  • The ltitemsgt element will have a single child,
    ltitemgt, which can appear 1 to many times. Each
    ltitemgt element will have two attributes itemID,
    a string, and quantity, a positiveInteger.

93
Using Visual Studio .NET to Build an XML Schema
  • The ltcustomergt element will have three children
  • ltnamegt (type string)
  • ltaddressgt (type string)
  • ltphonegt (type phoneType create a simpleType
    with a pattern facet)

94
Our completed Schema
95
View the XML Schemas Content by Clicking on the
XML Tab
96
Using Visual Studio .NET to Build an XML Schema
  • VS.NET automatically gives the created schema a
    targetNamespace and xmlns of http//tempuri.org/XM
    LSchema.xsd and sets the elementFormDefault
    attribute to qualified.
  • Change the targetNamespace and xmlns to a
    different namespace, like http//www.cs.ucy.ac.cy/
    xmlfordotnet

97
Questions?
Write a Comment
User Comments (0)
About PowerShow.com