Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / Hungry Minds - C# Bible

.pdf
Скачиваний:
224
Добавлен:
12.02.2016
Размер:
4.21 Mб
Скачать

<h1>Glossary</h1> <div align="left">

<table border="1" width="359" height="110"> <tr>

<td width="125" height="22">

<b><font size="4">Abbreviation</font></b> </td>

<td width="234" height="22">

<b><font size="4">Meaning</font></b> </td>

</tr>

<tr>

<td width="125" height="22"> ADO

</td>

<td width="234" height="22"> <b>A</b>ctive <b>D</b>ata <b>O</b>bjects

</td>

</tr>

<tr>

<td width="125" height="22"> SOAP

</td>

<td width="234" height="22">

<b>S</b>imple <b>O</b>bject <b>A</b>ccess <b>P</b>rotocol

</td>

</tr>

<tr>

<td width="125" height="22"> UDA

</td>

<td width="234" height="22"> <b>U</b>niversal <b>D</b>ata <b>A</b>ccess

</td>

</tr>

<tr>

<td width="125" height="22"> XML

</td>

<td width="234" height="22">

e<b>X</b>tensible <b>M</b>arkup <b>L</b>anguage </td>

</tr>

</table>

</div>

</body>

</html>

XML = HTML with User-Defined Tags

Now let us discuss XML and fill out the missing details so you can create your first XML document. An XML document consists of three parts: prologue, body, and epilogue. Only the body of the document is required. The prologue and epilogue may be omitted.

Here is the basic structure of an XML document:

Prologue:

XML Declaration (optional) <!-- Comments may go here -->

Document Type Declaration (optional) <!-- Comments may go here -->

Body:

Document Element <Document>

<!-- Document goes here --> </Document>

Epilogue:

<!-- Comments may go here -->

An XML starts with a prologue. If you exclude the optional comments, the prologue contains two main elements (which are each optional as well). The XML declaration has one required attribute used to specify the version of the XML specification to which the document conforms. The XML declaration also has two optional attributes: one to specify the character encoding used and one to specify whether the document relies on an external document type definition (DTD). Following is an example of a complete XML declaration, using all three attributes.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

The attributes in the XML declaration must be used in the order shown in the example. The version attribute is mandatory and must have the value of "1.0". The character encoding of XML documents and document type definitions are discussed below.

A root tag must enclose the document element. In the example above, this root tag is the <Document> tag, but you are free to use any tag to enclose the document element. Finally, all tags in an XML document must nest properly. If an element is contained in another element, then the contained element is called a child, while the containing element is called the parent. Here is an example:

<Book Category="Chess">

<Title>My System</Title> <Author>Aron Nimzowitsch</Author> </Book>

In the example above the <Book> tag is a parent to two children, the <Author> and <Title> elements. Proper nesting requires that children be always completely contained within their parent. In other words, the end tag of a child cannot appear after the end tag of the parent, as in the following:

<Book>

<Title>Improper Nesting in XML Explained </Book>

</Title>

The epilogue, which can contain only comments (as well as white space and processing instructions), is frequently omitted.

You are now ready for a first look at an XML document, shown in Listing A-3. The line numbers are not part of the document and are only there to make the line-by-line explanation below easier to follow.

Listing A-3: A Simple XML Document

1:<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

2:<!-- A list of recommended books on XML -->

3:<!-- Compiled on March 17, 2000 by PGB -->

4:<XMLBooks>

5:<Book ISBN="0-7897-2242-9">

6: <Title>XML By Example</Title>

7: <Category>Web Development</Category>

8: <Author>Benoit Marchal</Author>

9:</Book>

10:<Book ISBN="1-861003-11-0">

11: <Title>Professional XML </Title> 12: <Category>Internet</Category>

13: <Category>Internet Programming</Category>

14:<Category>XML</Category>

15: <Author>Richard Anderson</Author>

16: <Author>Mark Birbeck</Author>

17: <Author>Michael Kay</Author>

18: <Author>Steven Livingstone</Author>

19: <Author>Brian Loesgen</Author>

20: <Author>Didier Martin</Author>

21: <Author>Stephen Mohr</Author>

22: <Author>Nikola Ozu</Author>

23: <Author>Bruce Peat</Author>

24: <Author>Jonathan Pinnock</Author>

25: <Author>Peter Stark</Author>

26: <Author>Kevin Williams</Author>

27:</Book>

28:<Book ISBN="0-7356-0562-9">

29:

<Title>XML in Action</Title>

30:

<Category>Internet</Category>

31:

<Category>XML</Category>

32:

<Author>William J. Pardy</Author>

33:</Book>

34:</XMLBooks>

Line 1 in Listing A-3 contains a complete XML declaration, containing all three attributes. Lines 2 and 3 are comments used here to indicate the purpose of the document. The body of the XML document follows, starting at line 4 and ending with line 34. This document does not contain an epilogue, as is usually the case. The document element is enclosed by the <XMLBooks> tag (the starting tag is on line 4, the ending tag on line 34). The document element contains three children, each enclosed by a <Book> tag. Child 1 starts from line 5 and ends on line 9. Child 2 starts from line 10 and ends on line 27. Child 3 starts from line 28 and ends on line 33. Each <Book> element has an ISBN attribute and a number of children: one <Title>, one or more <Category> and one or more <Author>. Here you see a significant

advantage of XML over traditional text files: XML is well equipped to deal with parent/child structures.

It is worth pointing out that as the document creator I invented the tags and attributes used in this document (XMLBooks, Book, ISBN, Title, Category, Author). Another author may for example have preferred <ShelvingCategory> instead of <Category>. You can also judge by yourself how well the current XML specification does on reaching goal #6 (XML documents should be human-legible and reasonably clear).

Document Type Definitions

The XML document shown in Listing A-3 above has a more defined structure than the structure mandated by XML. A document type definition (DTD) provides a way to specify this structure, the data model corresponding to the data model. The comparison can be made with a database schema that defines the data model of a database. This comparison works well because both a database and an XML document contain structured data. The DTD is the schema corresponding to the XML document. Listing A-4 shows the DTD that corresponds to Listing A-3.

Listing A-4: DTD Schema Corresponding to XML Document

 

 

 

<?xml version="1.0"?>

 

<!--

The top-level element,

XMLBooks,

is a list of books -->

<!ELEMENT

XMLBooks

(Book+)>

<!--

A Book element

contains 1 Title, 1 or more Category, and 1 or more Author -->

<!ELEMENT

Book

(Title,Category*,Author+)>

<!--

A Book has 1

required attribute -->

<!ATTLIST

Book ISBN ID #REQUIRED>

<!--

The Title, Category, and Author

elements contain text -->

<!ELEMENT Title

(#PCDATA)>

<!ELEMENT Category

(#PCDATA)>

<!ELEMENT Author

(#PCDATA)>

 

 

 

The structure of the DTD is pretty close to the Extended Backus-Naur Form mentioned above. The DTD is a set of successive rules describing how to assemble the data into the XML document model. Every rule describes a specific element or attribute that the model can contain. An XML document is valid if it can be reduced to a single, specific rule in the DTD, with no input left, by repeated application of the rules.

Following is a description of the syntax used in this DTD. Note that DTDs use a different syntax than XML documents.

Each element is described using an element description line.

<!ELEMENT element_name (element_content)>

The element_name uses the tag to identify each element. In element_content, you either put other elements or #PCDATA to indicate that the element contains text. Leafs elements are elements that have no children. These elements are often specified as containing #PCDATA.

Special characters after an element name indicate the cardinality of the contained elements. The cardinality indicates how many of these elements can occur, and whether the element is optional or required. There are four ways to indicate cardinality.

A contained element without any special symbol — such as Title in Listing A-4 — must appear exactly once in the element being defined (cardinality: 1).

A contained element followed by a question mark (?) is optional and can appear only once in the element (cardinality: 0..1).

The next two ways define repeating elements, one for required and one for optional.

A contained element followed by a plus sign (+) — such as Book and Author in Listing A-4 — is required and can repeat (cardinality: 1..N).

A contained element followed by an asterisk (*) — such as Category in Listing A-4 — is optional and can repeat (cardinality: 0..N).

<!ATTLIST element_name attribute_name attribute_content optionality>

Attribute lists are defined in a separate line. The element_name is again the tag to which the attribute belongs. The attribute_name is the name of the attribute (for example, ISBN in Listing A-4). The attribute content is defined using a series of keywords. The most common is CDATA indicating that the attribute takes character data. The optionality is indicated by the keyword #REQUIRED for required attributes and #IMPLIED for optional attributes.

XML Schemas

On May 2, 2001, the standards body governing over the XML standard announced that an important member of the XML family has reached standard status (a proposed recommendation, as www.w3.org calls it). This standard is called XML Schemas and is poised to replace DTDs as the preferred way to validate XML documents.

XML Schemas offer two distinct advantages over DTDs:

An XML Schema is an XML document

XML Schemas allow you to specify data characteristics (such as type, size and precision) of elements and attributes

A schema documents looks as follows:

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<!-- schema content goes here -->

</xsd:schema>

The xmlns:xsd attribute of the schema element is a namespace declaration, which is covered in the next section. Note that the value of this attribute has changed over time, so if you encounter a schema with a different value for this attribute (for example,

www.w3.org/2000/10/XMLSchema), then this schema was properly created according to a draft version of the XML Schema standard.

The schema content consist of definitions for the elements and attributes that this schema can contain. An element is defined as follows

<xsd:element name="theElementName">

<! -- element specifics goes here -->

</xsd:element>

while an attribute is defined as follows

<xsd:attribute name="theAttributeName">

<! -- attribute specifics goes here -->

</xsd: attribute >

You can add documentation with comments or stick an annotation element inside the element or attribute definition. The annotation element contains a documentation element where you can document the specifics of the element or attribute.

<xsd:annotation>

<xsd:documentation>Some explanation here...</xsd:documentation> </xsd:annotation>

You can group elements and attributes by sticking them inside a complexType tag.

<xsd:complexType>

</xsd:complexType>

This type of grouping is required each time you see an element definition such as this:

<!ELEMENT Book

(Title,Category*,Author+)>

Elements grouped within a sequence need to be presented in the order they are defined.

<xsd:sequence>

</xsd:sequence>

So, if you define a Book element as follows, then a Book element needs to contain a Title, Category and Author element in this exact order (Title, Author and Category would for example not be valid).

<xsd:element name="Book"> xsd:complexType> <xsd:sequence> <xsd:element name="Title"> </xsd:element>

<xsd:element name="Category"/> <xsd:element name="Author"/> </xsd:sequence> </xsd:complexType> </xsd:element>

The cardinality of elements is assumed one. If you want to create a repeating element, you may do so by adding a maxOccurs="unbounded" attribute to the element definition. If you want to create an optional element, you may do so by adding a minOccurs="0" attribute to the element definition. You may of course combine these attributes to create an optional repeating element.

Finally, you can specify the data type of an element with a type="xsd:datatype" attribute. In our example, we use only the string data type. The XML Schema allows for a wide range of data types, such as integer, long, date, time, double, float, and so on.

Listing A-5 lists the XML Schema that corresponds to the DTD discussed above. XML Schemas have a .xsd file extension and are therefore sometimes called XSDs.

Listing A-5: XML Schema Corresponding to DTD

<?xml version="1.0" encoding="UTF-8"?> <!-- W3C Schema for a List of Books -->

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="XMLBooks">

<xsd:annotation>

<xsd:documentation>The top-level element, XMLBooks, is a list of books.</xsd:documentation>

</xsd:annotation>

<xsd:complexType>

<xsd:sequence>

<xsd:element name="Book"

maxOccurs="unbounded">

<xsd:annotation> <xsd:documentation>A Book

element contains 1 Title, 1 or more Category, and 1 or more Author.</xsd:documentation>

</xsd:annotation>

<xsd:complexType>

<xsd:sequence>

<xsd:element

name="Title" type="xsd:string">

<xsd:annotation>

<xsd:documentation>The Title, Category, and Author elements contain text.</xsd:documentation>

</xsd:annotation>

</xsd:element>

<xsd:element name="Category" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>

<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>

</xsd:sequence> <xsd:attribute name="ISBN"

type="xsd:string" use="required" id="isbn"> <xsd:annotation>

<xsd:documentation>A Book has 1 required attribute.</xsd:documentation>

</xsd:annotation>

</xsd:attribute>

</xsd:complexType>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

Listing A-6 shows how an XML document can refer to its associated XML Schema. Listing A-6: XML Document That Refers to Its Associated XML Schema

<?xml version="1.0" encoding="UTF-8"?>

<XMLBooks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./Books.xsd">

<Book ISBN="0-7897-2242-9"> <Title>XML By Example</Title>

<Category>Web Development</Category> <Author>Benoit Marchal</Author>

</Book>

<Book ISBN="0-7356-0562-9"> <Title>XML in Action</Title> <Category>Internet</Category> <Category>XML</Category>

<Author>William J. Pardy</Author> </Book>

</XMLBooks>

XML Namespaces

The extensibility of XML is both a blessing and a curse. Allowing anyone to create his or her own tags runs the risk of creating a Babylonian confusion. Luckily, the designers of the XML standards recognized the danger and came up with a solution, namely Namespaces. You are already familiar with the idea of namespaces through your study of C# (the same idea is present in C++, Java and the other .NET languages). The implementation varies a little from case to case, but the basic idea is always the same.

You associate a unique name with a prefix and use this prefix to qualify the names that might have collided without the prefix. Because XML is Web-based, the designers chose to use URL as the unique names.

You specify the namespace used in a XML Schema by adding a targetNamespace= "www.myurl.com" attribute to the schema. You define this namespace by adding a special xmlns attribute to the schema element. You may append the namespace prefix to this attribute by using a colon to separate the xmlns attribute and the prefix. It is the responsibility of the Schema designer to ensure that the value of this attribute is unique. This is often achieved by using the company's URL.

xmlns:prefix="http://www.myurl.com"

After you have defined a namespace prefix, you need to append it to all elements contained in the namespace.

<?xml version="1.0" encoding="UTF-8"?> <!-- W3C Schema for a List of Books --> <xsd:schema

targetNamespace="www.myurl.com"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:book="www.myurl.com"> <xsd:element name="XMLBooks">

<xsd:annotation>

<xsd:documentation>The top-level element, XMLBooks, is a list of books.</xsd:documentation>

</xsd:annotation>

<xsd:complexType>

<xsd:sequence>

<xsd:element name="Book" maxOccurs="unbounded">

<xsd:annotation> <xsd:documentation>A Book

element contains 1 Title, 1 or more Category, and 1 or more Author.</xsd:documentation>

</xsd:annotation>

<xsd:complexType>

<xsd:sequence>

<xsd:element name="Title" type="xsd:string">

<xsd:annotation>

<xsd:documentation>The Title, Category, and Author elements contain text.</xsd:documentation>

</xsd:annotation>

</xsd:element>

<xsd:element name="Category" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>

<xsd:element

name="Author" type="xsd:string" maxOccurs="unbounded"/> </xsd:sequence>

<xsd:attribute name="ISBN" type="xsd:string" use="required" id="isbn">

<xsd:annotation>

<xsd:documentation>A Book has 1 required attribute.</xsd:documentation>

</xsd:annotation>

</xsd:attribute>

</xsd:complexType>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

The following XML document shows how to create an XML document that refers to a schema using name spaces. This is done by adding three attributes to the root element. The first attribute defines the prefix used by the namespace and the unique string associated with this namespace. The second attribute specifies which version of the XML Schemas you are using. Finally, the third attribute tells you which namespace the XML Schema is using and where the XML Schema is located.

<?xml version="1.0" encoding="UTF-8"?> <book:XMLBooks

xmlns:book="www.myurl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.myurl.com .\Books.xsd"> <Book ISBN="0-7897-2242-9">

<Title>XML By Example</Title> <Category>Web Development</Category> <Author>Benoit Marchal</Author>

</Book>

<Book ISBN="0-7356-0562-9"> <Title>XML in Action</Title> <Category>Internet</Category> <Category>XML</Category>

<Author>William J. Pardy</Author> </Book>

</book:XMLBooks>

Because most elements in an XML document belong to the same namespace, it is possible to create a default namespace and omit the namespace prefix, for example, xmlns ="www.myurl.com".

Lastly, it is possible to have several namespace declarations in the same XML document. This is done by adding all the namespace attributes to the root element. Note that a document can only point to 1 XML Schema though.

Соседние файлы в папке c#