XML Namespaces

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

Because users define an XML document's element names, it's possible that many developers will use the same names. XML namespaces allow developers to write a unique name and avoid conflicts between element names with other developers. With the help of URI, a namespace ensures the uniqueness of XML elements, tags, and attributes.

To declare namespaces, you can use default or explicit names. When you define your own namespace. The W3C recommends you control the URI and point to the same location consistently.

The scope of a document's elements depends on the URI. Listing 6-6 shows an example of XML document with namespace. In this example, <book> and its attributes and tags belong to the http://www.c-sharpcorner.com/Images URI.

Listing 6-6. XML namespace declaration example


<?
xml version="1.0" encoding="utf-8" ?>
<
book xmlns = "http://www.c-sharpcorner.com/Images" >
  <
title>The autobiography of Benjamin Franklin</title>
  <
author>
    <
first-name>Benjamin</first-name>
    <
last-name>Franklin</last-name>
  </
author>
  <
price>8.99</price>
</
book>

Document type Definition (DTD) and schemas

A Documents Type Definition (DTD) defines a document structure with a list of legal elements. You can declare DTDs inline or as a link to an external file. You can also use DTDs to validate XML documents. This is an example of a DTD:


<!
ELEMENT Two (#PCDATA)>
<!
ELEMENT one (B)>
<!
ATTLIST one c CDATA # REQUIRED>

This DTD defines a format of a data. The following XML is valid because the tag<Two> is inside the tag <one>:


<
One c="Attrib">
  <
Two> Something here</Two>
</
One>

An XML schema describes the relationship between a document's elements and attributes. XML schemas describe the rules, which can be applied to any XML document, for elements and attributes. If an XML document references a schema and it doesn't meet the criteria XML parser will give an error during parsing.

You need a language to write schemas. These languages describe the syntaxes for each schema (XML document) you write. There are many schema languages, including DTD, XML Data Reduced (XDR), and simple object XML (SOX).

Similar to an XML document, an XML schema always starts with statement <?xml version ="1.0" ?>, which specifies the XML version.

The next statement of a schema contains an xsd:schema statement, xmlns, and target namespace. The xsd: schema indicates that file is a schema.

A schema starts with a <xsd:schema> tag and ends with a </xsd:schema>tag. All schema items have the prefix xsd. The xmlns ="http://www.w3.org/2001/XMLschema" is a http://www.W3c.org URI, which indicates the schema should be interpreted according to the default, namespace of the W3C. The next piece of this line is the target namespace, which indicates the location of a machine (a URI). Listing 6-7.is a schema representation for the document in Listing 6-5.

Listing 6-7. XML schema example


<
xsd:schema xmlns:xsd ="http://www.w3.org/2001/XML Schema">
  <
xsd:element name = "bookstore" type = "bookstoreType"/>
   <
xsd: ComplexType="" name ="bookstoreType">
    <
xsd: squence="" maxOccurs = "unbounded">
      <
sdx: element="" name = "book" type = "bookType"/>
    </
xsd: sequence>
  </
xsd: complexType>
  <
xsd: ComplexType="" name = "bookType">
    <
xsd: sequence="">
      <
xsd: element="" name = "title" type = "xsd:string:"/>
      <
xsd: element="" name = "author" type = "authorName"/>
      <
xsd: element="" name = "price"  type = "xsd:decimal"/>
    </
xsd: sequence>
  </
xsd: attribute name = "genre" type = "xsd:string"/>
  </
xsd: complexType>
  <
xsd: complexType="" name = "authorName">
    <
xsd: sequence="">
      <
xsd: element="" name ="first-name" type ="xsd:string"/>
      <
xsd: element="" name = "last–name" type= "xsd:string"/>
    </
xsd: sequence>
  </
xsd: complexType>
</
xsd:schema>

In this listing, <ComplexType>, <sequence> and<element> are schema elements. An element is a simple item with a single element. The ComplexType element is a set of attributes that denotes that element has children. Some other schema items are <all>, <annotation>, <any>, <anyAttribute>, <attribute>, <choice>, <documentation>, <field>, <group>, <include>, <key>, <length>, <maxLength>, <minLegth>, <selection>, <pattern>, <simpleType>, <unique>, and so on.

Elements and attributes are basic building block of a schema. An element is a tag with data. An element can have nested elements and attributes. Elements with one or more elements or attributes are ComplexType elements. An element contains a name and a data type. For example, the element price is of type decimal in the following line:


<
xsd:element name ="price" type = "xsd:decimal"/>

This definition of the element price makes sure that it can only store a decimal type of a value. Other types of values are invalid values. For example, this is valid:


<
price>19.95</price>

But this example is invalid:


<
price>something</price>

Schema attributes are similar to XML attributes, but you can also define them using an xsd:attribute item. For example:


<
xsd: ComplexType="" name= "bookstoreType">
  or

<
xsd: attribute="" name = "bookstoreType" type ="xsd:string"/>

A full discussion of these items is beyond the scope of this article: however, I'll describe any items I use in any of the samples.

Extensible Hypertext markup language (XHTML)

Extensible Hypertext Markup Language (XHTML) is a next-generation language of HTML. In January 2000, XHTML 1.0 became a W3C recommendation. XHTML is a better and improved version of HTML; however, it does have some restrictions.

XHTML is a combination of XML and HTML. XHTML uses elements of HTML 4.01 and rules of XML to provide a more consistent, well-formed and organized language.

Conclusion


Hope this article would have helped you in understanding XML namespace, DTD and schemas and Extensible Hyper Text Markup Language. See other articles on the website also for further reference.

adobook.jpg This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.