Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

XML 1

XML
• XML is a simple text-based language which
was designed to store and transport data in
plain text format.
• It stands for Extensible Markup Language.
• You can define your own tags which is why it
is called extensible language.
XML Separates Data from HTML
• When displaying data in HTML, you should not
have to edit the HTML file when the data
changes.
• With XML, the data can be stored in separate
XML files.
Well-Formed Documents
Document Type Definition
• A DTD defines the valid building blocks of an
XML document.
• It defines the document structure with a list of
validated elements and attributes.
• A DTD can be declared inline inside an XML
document, or as an external reference.
• This DTD first declares the recipe language’s
elements. Element declarations take the form
<!ELEMENT name content-specifier>, where
name is any legal XML name (e.g., it cannot
contain whitespace), and content-specifier
identifies what can appear within the
element.
• The first element declaration states that exactly one recipe
element can appear in the XML document. Furthermore, this
element must include exactly one each of the title,
ingredients, and instructions child elements, and in that
order. Child elements must be specified as a
comma-separated list.
• The second element declaration states that the title element
contains parsed character data.
• The third element declaration states that at least one
ingredient element must appear in ingredients. The +
character is an example of a regular expression that means
one or more. Other expressions that may be used are * (zero
or more) and ? (once or not at all).
• The fourth and fifth element declarations are similar to the
second by stating that ingredient and instructions elements
contain parsed character data.
• DTD lastly declares the recipe language’s
attributes, of which there is only one: qty.
• Attribute declarations take the form
<!ATTLIST ename aname type default-value>,
where ename is the name of the element to
which the attribute belongs, aname is the
name of the attribute, type is the attribute’s
type, and default-value is the attribute’s
default value.
• The attribute declaration identifies qty as an
attribute of ingredient.
• It also states that qty’s type is CDATA and that
qty is optional, assuming default value 1 when
absent.
<?xml version="1.0"?>
<!DOCTYPE address [
<!ELEMENT address (name, email, phone, birthday)>
<!ELEMENT name (first, last)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT last (#PCDATA)> Internal DTD
<!ELEMENT email (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
Declaration - If
<!ELEMENT birthday (year, month, day)> the DTD is
<!ELEMENT year (#PCDATA)>
<!ELEMENT month (#PCDATA)>
declared inside
<!ELEMENT day (#PCDATA)> the XML file, it
]>
<address>
must be
<name> wrapped inside
<first>Rohit</first> the <!DOCTYPE>
<last>Sharma</last>
</name> definition.
<email>sharmarohit@gmail.com</email>
<phone>9876543210</phone>
<birthday>
<year>1987</year>
<month>June</month>
<day>23</day>
</birthday>
</address>
External DTD Declaration
• If the DTD is declared in an external file, the <!DOCTYPE> definition must
contain a reference to the DTD file.
• A system identifier enables you to specify the location of an external file
containing DTD declarations.

<?xml version="1.0"?>
<!DOCTYPE address SYSTEM "address.dtd">
<address>
<name>
<first>Rohit</first>
<last>Sharma</last>
</name>
<email>sharmarohit@gmail.com</email>
<phone>9876543210</phone>
<birthday>
<year>1987</year>
<month>June</month>
<day>23</day>
</birthday>
</address>
Namespace
• In XML, element names are defined by the developer.
• This often results in a conflict when trying to mix XML
documents from different XML applications.
• This XML carries HTML table information:
• If these XML fragments were added together, there
would be a name conflict.
• Both contain a <table> element, but the elements
have different content and meaning.
• A user or an XML application will not know how to
handle these differences.
Solving the Name Conflict Using a Prefix
• Name conflicts in XML can easily be avoided using a name prefix.
• This XML carries information about an HTML table, and a piece of furniture:

In the example, there will be no conflict because the two <table> elements have different
names.
XML Namespaces - The xmlns Attribute
• When using prefixes in XML, a namespace for the prefix must be defined.
• The namespace can be defined by an xmlns attribute in the start tag of an
element.
• The namespace declaration has the following syntax. xmlns:prefix="URI".
• In the example above:
• The xmlns attribute in the first <table>
element gives the h: prefix a qualified
namespace.
• The xmlns attribute in the second <table>
element gives the f: prefix a qualified
namespace.
• When a namespace is defined for an element,
all child elements with the same prefix are
associated with the same namespace.
XML Schemas
• Like DTDs, XML Schemas are used for defining XML
vocabularies. They describe the structure and content of XML
documents in more detail than DTDs.
BENEFITS OF XML SCHEMAS
• XML Schemas are created using basic XML, whereas DTDs
utilize a separate syntax.
• XML Schemas fully support the Namespace Recommendation.
• XML Schemas enable you to validate text element content
based on built-in and user-defined data types.
• XML Schemas enable you to more easily create complex and
reusable content models.
• XML Schemas enable the modeling of programming concepts
such as object inheritance and type substitution.
XML Schemas Use XML Syntax
• When creating an XML Schema, the syntax is
entirely in XML.
• But as with DTDs you are defining rules for
XML documents, so there are similarities.
XML Schema Namespace Support
• With XML Schemas you can define
vocabularies that use namespace declarations
and mix namespaces in XML documents with
more flexibility.
Creating a schema for recipe language
document
• The first step in creating this recipe language
schema is to identify all of its elements and
attributes.
• The elements are recipe, title, ingredients,
instructions, and ingredient; qty is the only
attribute.
• The next step is to classify the elements according
to XML Schema’s content model, which specifies
the types of child elements and text nodes that
can be included in an element.
• An element is considered to be empty when
the element has no child elements or text
nodes, simple when only text nodes are
accepted, complex when only child elements
are accepted, and mixed when child elements
and text nodes are accepted.
• The title, ingredient, and instructions
elements have simple content models; and
the recipe and ingredients elements have
complex content models.
• For elements that have a simple content model,
we can distinguish between elements having
attributes and elements not having attributes.
• XML Schema classifies elements having a simple
content model and no attributes as simple types.
• Furthermore, it classifies elements having a
simple content model and attributes, or elements
from other content models as complex types.
• Furthermore, XML Schema classifies attributes as
simple types because they only contain text
values—attributes don’t have child elements.
• title and instructions elements and its qty
attribute are simple types. recipe, ingredients,
and ingredient elements are complex types.
• The following code fragment presents the
introductory schema element:

• Next, we use the element element to declare


the title and instructions simple type
elements, as follows:
• XML Schema requires that each element have
a name and (unlike DTD) be associated with a
type, which identifies the kind of data stored
in the element.
• we now use the attribute element to declare
the qty simple type attribute, as follows:
• Now that we’ve declared the simple types, we can start to
declare the complex types. To begin, we’ll declare recipe, as
follows:

• This declaration states that recipe is a complex type (via the


complexType element) consisting of a sequence (via the
sequence element) of one title element followed by one
ingredients element followed by one instructions element. Each
of these elements is declared by a different element that’s
referred to by its element’s ref attribute.
• The next complex type to declare is ingredients. The following
code fragment provides its declaration:

• This declaration states that ingredients is a complex type


consisting of a sequence of one or more ingredient elements.
The “or more” is specified by including element’s maxOccurs
attribute and setting this attribute’s value to unbounded.
• The final complex type to declare is ingredient.
Although ingredient can contain only text nodes,
which implies that it should be a simple type, it’s the
presence of the qty attribute that makes it complex.

<xs:element name="ingredient">
<xs:complexType>
<xs:simpleContent>
<xs:attribute ref="qty"/>
</xs:simpleContent>
</xs:complexType>
</xs:element>
• The element named ingredient is a complex
type (because of its optional qty attribute).
The simpleContent element indicates that
ingredient can only contain simple content
(text nodes). Furthermore, ingredient is given
an additional qty attribute.
After creating the schema, you can reference it from a recipe document.
Accomplish this task by specifying xmlns:xsi and xsi:schemaLocation attributes
on the document’s root element start tag (<recipe>), as follows:
• https://www.ibm.com/docs/en/i/7.1?topic=to
olkit-xml-introduction

You might also like