0% found this document useful (0 votes)
18 views

Module 8 (XML)

Uploaded by

pg.piyush18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Module 8 (XML)

Uploaded by

pg.piyush18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

WEB TECHNOLOGY AND ITS

APPLICATIONS
MODULE 7 – XML

• Introduction to XML,uses of XML


• simple XML,XML key components
• DTD and Schemas, Well formed, using XML with
application.
• XML, XSL and XSLT.
• Introduction to XSL
• XML transformed simple example
• XSL elements, transforming with XSLT.
XML Introduction
XML is a text based markup language that is fast becoming a
standard of data interchange
An open standard from W3C
A direct descendant from SGML

Example: Product Inventory Data


<Product>
<Name>Refrigerator</Name>
<Model Number>R3456d2h</Model Number>
<Manufacturer>General Electric</Manufacturer>
<Price>1290.00</Price>
<Quantity>1200</Quantity>
</Product>
uses of XML
▪ Electronic data interchange is critical in today’s
networked world and needs to be standardized
➢ Examples:
– Banking: funds transfer
– Education: e-learning contents
– Scientific data
– Chemistry: ChemML, …
– Genetics: BSML (Bio-Sequence Markup Language), …

▪ Each application area has its own set of


standards for representing information
▪ XML has become the basis for all new
generation data interchange formats (markups)
uses of XML
▪ Earlier electronic formats were based on plain text
with line headers indicating the meaning of fields
➢ Does not allow for nested structures, no standard “type”
language
➢ Tied too closely to low level document structure (lines, spaces,
etc)
▪ Each XML based standard defines what are valid
elements, using XML type specification languages (i.e.
grammars) to specify the syntax
➢ E.g. DTD (Document Type Descriptors) or XML Schema
➢ Plus textual descriptions of the semantics
▪ XML allows new tags to be defined as required
▪ A wide variety of tools is available for parsing, browsing
and querying XML documents/data
uses of XML
▪ SGML is more difficult
➢ XML implements a subset of its features
▪ HTML will not do it …
➢ HTML is very limited in scope, it’s a language
(vocabulary) for delivering (interactive) web pages
➢ XML is Extensible, unlike HTML
➢ Users can add new tags, and separately specify how
the tag should be handled for display
➢ XML is a formalism for defining vocabularies (i.e. a
meta-language), HTML is just a SGML vocabulary
Simple XML and XML Key
components
HTML vs. XML

• Both are markup languages


HTML has fixed set of tags
XML allows user to specify the tags based on requirements
• Usage
HTML tags specify how to display data
XML tags specify semantics of the data
• Tag Interpretation
HTML specifies what each tag and attribute means
XML tags delimit data & leave interpretation to the parsing application
• Well formedness
HTML very tolerant of rule violations (nesting, matching tags)
XML very strictly follows rules of well formedness
XML Documents Must Have a
Root Element
• XML documents must contain one root element that is the
parent of all other elements:
• <root>
<child>
<subchild>.....</subchild>
</child>
</root>

• XML Tags are Case Sensitive


• XML tags are case sensitive. The tag <Letter> is different from the
tag <letter>.
• Opening and closing tags must be written with the same case:
Prolog
<?xml version=“1.0” encoding=“UTF-8” standalone=“yes” ?>
• Contains eclaration that identifies a document as xml
• Version
• Version of XML markup language used in the data
• Not optional
• Encoding
• Identifies the character set used to encode the data
• Default compressed Unicode: UTF-8
• Standalone
• Tells whether or not this document references external entity
Structure of XML
1. Prolog
• Instructs the parser as to what it it parsing
• Contains processing instructions for processor
2. Body
• Tags - Entities
• Attributes - Properties of Entities
• Comments - Statements for clarification in the document
Example
<?xml version=“1.0” encoding=“UTF-8” standalone=“yes” ?>  Prolog
<contact>
<name>
<first name>Sanjay</first name>
<last name>Goel</last name>
</name>
<address>  Body
<street>56 Della Street</street>
<city>Phoenix</city>
<state>AZ</state>
<zip>15784</zip>
</address>
</contact>
XML Attribute Values Must
Always be Quoted

XML elements can have attributes in name/value pairs just


like in HTML.
In XML, the attribute values must always be quoted:

<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
Entity References
Some characters have a special meaning in XML.

If you place a character like "<" inside an XML element, it will generate
an error because the parser interprets it as the start of a new element.

This will generate an XML error:


<message>salary < 1000</message>

To avoid this error, replace the "<" character with


an entity reference:
<message>salary &lt; 1000</message>
There are 5 pre-defined entity
references in XML:

&lt; < less than


&gt; > greater than
&amp; & ampersand
&apos; ' apostrophe
&quot; " quotation mark
What is an XML
document ?
▪ An XML document is a content marked up
with XML (can be a file, a string, a message
content or any other sort of data storage)
▪ There are 2 levels of conforming documents:
➢ Well-formed: respects the XML syntax
➢ Valid: In addition, respects one (or more)
associated grammars (schemas).
What is a valid XML
document?
▪ Parser (i.e. that program that reads the XML)
can check markup of individual document
against rules expressed in a schema (DTD, XML
Schema, etc.)
▪ Typically, a schema (grammar):
➢ Defines available elements
➢ Defines attributes of elements
➢ Defines how elements can be embedded
➢ Defines mandatory and optional information
▪ Authoring tools usually can enforce rules of
DTD/Schema while document is edited
Example:
1. XML Document
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>John</to>
<from>Jane</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

2.DTD (Schema)
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
XML information structures (1)
Example 1: A possible book structure

Book
FrontMatter
BookTitle
Author(s)
PubInfo
Chapter(s)
ChapterTitle
Paragraph(s)
BackMatter
References
Index

http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
XML information structures (2)
▪ Premise: A text is the sum of its component
parts
➢ A <Book> could be defined as containing:
<FrontMatter>, <Chapter>s, <BackMatter>
➢ <FrontMatter> could contain:
<BookTitle> <Author>s <PubInfo>
➢ A <Chapter> could contain:
<ChapterTitle> <Paragraph>s
➢ A <Paragraph> could contain:
<Sentence>s or <Table>s or <Figure>s …
▪ Components chosen for book markup language
should reflect anticipated use ….

http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
XML information structures (3)

A corresponding XML fragment


(based on a corresponding XML application):
begin end
<Book> element elemen
<FrontMatter> t
<BookTitle>XML Is Easy</BookTitle>
<Author>Tim Cole</Author>
<Author>Tom Habing</Author>
<PubInfo>CDP Press, 2002</PubInfo>
</FrontMatter>
<Chapter>
<ChapterTitle>First Was SGML</ChapterTitle>
<Paragraph>Once upon a time …</Paragraph>
</Chapter>
</Book>

http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
XML Syntax: Elements & Attributes
• Uses less-than and greater-than characters (<…>) as
delimiters
• Every opening tag must having an accompanying closing
tag
– <First Name>Sanjay</First Name>
– Empty tags do not require an accompanying closing tag.
– Empty tags have a forward slash before the greater-than sign e.g.
<Name/>
• Tags can have attributes which must be enclosed in double
quotes
– <name first=“Sanjay” last=“Goel”)
• Elements should be properly nested
– The nesting can not be interleaved
– Each document must have one single root element
• Elements and attribute names are case sensitive
Tree Structure - Elements
• XML documents have a tree structure containing multiple levels of nested
tags.
– Root element is a single XML element which encloses all of the other
XML elements and data in the document
– All other elements are children of the root element

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


<contact>  Root Element
<name>
<first name>Sanjay</first name>
<last name>Goel</last name>
</name>
<address>
<street>56 Della Street</street>  Child Elements
<city>Phoenix</city>
<state>AZ</state>
<zip>15784</zip>
</address>
</contact>
Attributes
•Attributes are properties associated with an element
•Each attribute is a name value pair
–No element may contain two attributes with same name
–Name and value are strings
Example
<?xml version=“1.0” encoding=“UTF-8” standalone=“yes” ?>
<contact>
<name first=“Sanjay” last=“Goel”></name>  Attributes
<address>
<street>56 Della Street</street>  Nested Elements
<city>Phoenix</city>
<state>AZ</state>
<zip>15784</zip>
</address>
</contact>
XML information structures (4)
Example 2: Movies
▪ Elements can have attributes attribute
<movies>

<movie genre="action" star="Halle Berry">


<name>Catwoman</name>
<date>(2004)</date>
<length>104 minutes</length>
</movie>

<movie genre="horror" star="Halle Berry">


<name>Gothika</name>
<date>(2003)</date>
<length>98 minutes</length>
</movie>
<movie genre="drama" star="Halle Berry">
<name>Monster&apos;s Ball</name>
<date>(2001)</date>
<length>111 minutes</length>
</movie>
</movies>

http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
Comments
• XML comments begin with “<!--”and end with “-->”
– All data between these delimiters is discarded
– <!-- This is a list of names of people -->

– <!-- This is a comment -->

– Two dashes in the middle of a comment are not


allowed:
– <!-- This is an invalid -- comment -->
Comments
• XML comments begin with “<!--”and end with “-->”
– All data between these delimiters is discarded
– <!-- This is a list of names of people -->
• Comments should not come before XML declaration
• Comments can not be placed inside a tag
• Comments may be used to hide and surround tags
<Name>
<first>Sanjay</first>
<!-- <last>Goel</last> -->  Last tag is ignored
</Name>
• “--” string may not occur inside a comment except as part of its opening
and closing tag
– <!-- the Red door -- that is the second -->  Illegal
Namespaces
Basics
• XML documents come from different sources
– Combining elements from different sources can result in
name conflict
– Namespaces allow the interpreter to resolve the elements
• Namespaces
– Declared within element start-tag using attribute xmlns
– Represented as an actual URI (since namespaces are
globally unique)
– e.g. <Collection xmlns:book="http://www.mjyOnline.com/books"
xmlns:cd=http://www.mjyOnline.com/books>
– Here book and cd are short hands for the full namespace name
• Default namespace is used if no other namespace is defined
– It does not have any prefix associated with it
XML Namespaces (1)
▪ Various XML languages can be mixed
➢ However there can be a naming conflict, different
vocabularies (DTDs) can use the same names for
elements ! How to avoid confusion ?
▪ Namespaces:
➢ Qualify element and attribute names with a label
(prefix):
unique_prefix:element_name
➢ An XML namespace is a collection of names
(elements and attributes of a markup vocabulary)
➢ identified by xmlns:prefix=“URL reference”
xmlns:xlink="http://www.w3.org/1999/xlink"

http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
XML Namespaces provide a method to avoid element name conflicts.

▪ <h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
Display XML
Style Sheets

• A style sheet is a file that contains instructions for


rendering individual elements in an XML document
• Two kinds of style sheets exist
– Cascading Style Sheets (CSS)
– Extensible Stylesheet language (XSLT)
• Please refer to the following web site for
comprehensive information on style sheets
– http://www.w3schools.com/css/default.asp
Cascading Style Sheets
Example
<?xml version="1.0"?> <BOOK>
<!-- File Name: Inventory01.xml --> <TITLE>The Legend of Sleepy Hollow</TITLE>
<?xml-stylesheet type="text/css" <AUTHOR>Washington Irving</AUTHOR>
href="Inventory01.css"?> <BINDING>mass market paperback</BINDING>
<PAGES>98</PAGES>
<INVENTORY> <PRICE>$2.95</PRICE>
<BOOK> </BOOK>
<TITLE>The Adventures of Huckleberry <BOOK>
Finn</TITLE> <TITLE>The Marble Faun</TITLE>
<AUTHOR>Mark Twain</AUTHOR> <AUTHOR>Nathaniel Hawthorne</AUTHOR>
<BINDING>mass market paperback</BINDING> <BINDING>trade paperback</BINDING>
<PAGES>298</PAGES> <PAGES>473</PAGES>
<PRICE>$5.49</PRICE> <PRICE>$10.95</PRICE>
</BOOK> </BOOK>
<BOOK> <BOOK>
<TITLE>Leaves of Grass</TITLE> <TITLE>Moby-Dick</TITLE>
<AUTHOR>Walt Whitman</AUTHOR> <AUTHOR>Herman Melville</AUTHOR>
<BINDING>hardcover</BINDING> <BINDING>hardcover</BINDING>
<PAGES>462</PAGES> <PAGES>724</PAGES>
<PRICE>$7.75</PRICE> <PRICE>$9.95</PRICE>
</BOOK> </BOOK>
</INVENTORY>
Cascading Style Sheets
Example
/* File Name: Inventory02.css */ BINDING
BOOK {display:block;
{display:block; margin-left:15pt}
margin-top:12pt;
PAGES
font-size:10pt}
{display:none}
TITLE
{display:block; PRICE
font-size:12pt; {display:block;
font-weight:bold; margin-left:15pt}
font-style:italic}
AUTHOR
{display:block;
margin-left:15pt;
font-weight:bold}
Cascading Style Sheets
Display
Validated XML Document
Basics
• An XML document is valid if it conforms to the grammar of
the language
– Validity is different from well-formedness
• Two ways to specify the grammar of the language
– Document Type Definition (DTD)
– XML Schema
• Why bother with the language grammar
– It provides the blueprint of the language
– Ensures that the data is interchangable
– Eliminates processing errors in custom software which expects a
particular document content and structure
• Validity of the document is checked by using a validator
Document Type Declaration
Basics
• Document type declaration is a block of XML markup added
to the prolog of the document
– It has to follow the XML declaration
– It has to be outside of other markup language
• It defines the content and structure of the language
– Without a document type declaration or schema a document is merely
checked for well-formedness and not validity
• Why bother with the language grammar
– It provides the blueprint of the language
– Ensures that the data is interchangable
– Eliminates processing errors in custom software which expects a
particular document content and structure
• The form of a document type declaration is:
– <!DOCTYPE Name DTD>
– DTD is document type definition
– Name specifies the name of the document element
Types of Document Type Declaration

• Internal or embedded DTD-


– The DTD defined inside the XML
• External DTD
– The DTD defined externally.
– Where xml in one file and DTD in another file
Internal or embedded DTD
• <!DOCTYPE root [element declaration]>-
Syntax
• Example:note.xml
• <?xml version="1.0" encoding="UTF-8"?>
• <!DOCTYPE note
• [
• <!ELEMENT note (to,from,heading,body)>
• <!ELEMENT to (#PCDATA)>
• <!ELEMENT from (#PCDATA)>
• <!ELEMENT heading (#PCDATA)>
• <!ELEMENT body (#PCDATA)>
• ]>
<note>
<to>HI</to>
<from>HELLO</from>
<heading>GOOD MORNING</heading>
<body>HAPPY weekend!</body>
</note>
External DTD
• <!DOCTYPE root SYSTEM “filename.dtd”>-Syntax
• Example :note.xml
• <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM “note.dtd">
<note>
<to>HI</to>
<from>HELLO</from>
<heading>GOOD MORNING</heading>
<body>HAPPY weekend!</body>
</note>
External DTD
• The purpose of a DTD is to define the structure and the legal
elements and attributes of an XML document:
• note.dtd:

• <!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
The DTD above is interpreted like this:
•!DOCTYPE note - Defines that the root element of
the document is note
•!ELEMENT note - Defines that the note element
must contain the elements: "to, from, heading,
body"
•!ELEMENT to - Defines the to element to be of type
"#PCDATA"
•!ELEMENT from - Defines the from element to be
of type "#PCDATA"
•!ELEMENT heading - Defines the heading element
to be of type "#PCDATA"
•!ELEMENT body - Defines the body element to be
of type "#PCDATA"
XML Schema
• An XML Schema describes the structure of an
XML document, just like a DTD.
• An XML document with correct syntax is
called "Well Formed".
• An XML document validated against an XML
Schema is both "Well Formed" and "Valid".
XML Schema
• XML Schema is an XML-based alternative to
DTD:
• <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>
XML Schema
• The Schema above is interpreted like this:
• <xs:element name="note"> defines the element called
"note"
• <xs:complexType> the "note" element is a complex type
• <xs:sequence> the complex type is a sequence of
elements
• <xs:element name="to" type="xs:string"> the element
"to" is of type string (text)
• <xs:element name="from" type="xs:string"> the
element "from" is of type string
• <xs:element name="heading" type="xs:string"> the
element "heading" is of type string
• <xs:element name="body" type="xs:string"> the
element "body" is of type string
XML Schema Data types
• There are two types of data types in XML schema.
• simpleType
– The simpleType allows you to have text-based
elements. It contains less attributes, child elements,
and cannot be left empty.
• complexType
– The complexType allows you to hold multiple
attributes and elements. It can contain additional sub
elements and can be left empty.
Key Concepts in XML Schemas
(XSD):

• Elements: Define individual data fields in XML.


• <xs:element name="name" type="xs:string"/>

This defines an element <name> that must contain a string


value.
Key Concepts in XML Schemas
(XSD):

• Attributes: Define properties of elements.


• <xs:attribute name="age" type="xs:int"
use="required"/>
This defines an attribute age that is an integer and is required.
Key Concepts in XML Schemas
(XSD):

• Complex Types: Group elements and attributes


together.
<xs:complexType name="personType">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
</xs:sequence>
<xs:attribute name="age" type="xs:int"/>
</xs:complexType>
Key Concepts in XML Schemas
(XSD):

• Simple Types: Define restrictions on values like


patterns, ranges.
• <xs:simpleType name="genderType">
<xs:restriction base="xs:string">
<xs:enumeration value="Male"/>
<xs:enumeration value="Female"/>
</xs:restriction>
</xs:simpleType>
Key Concepts in XML Schemas
(XSD):

Choice: Allows one element from a defined set.

<xs:choice>
<xs:element name="phone" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:choice>
Sample XML Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person" type="personType"/>

<xs:complexType name="personType">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="gender" type="genderType"/>
</xs:sequence>
<xs:attribute name="age" type="xs:int"/>
</xs:complexType>

<xs:simpleType name="genderType">
<xs:restriction base="xs:string">
<xs:enumeration value="Male"/>
<xs:enumeration value="Female"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Introduction to XSLT

XSL (eXtensible Stylesheet Language) is a styling language for XML.


XSLT stands for XSL Transformations.
XSLT <xsl:template> Element
• An XSL style sheet consists of one or more set of rules
that are called templates.
• A template contains rules to apply when a specified node
is matched.
• The <xsl:template> element is used to build templates.
• The match attribute is used to associate a template with
an XML element. The match attribute can also be used to
define a template for the entire XML document. The
value of the match attribute is an XPath expression (i.e.
match="/" defines the whole document).
Example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The <xsl:value-of> Element
The <xsl:value-of> element can be used to extract the value of an XML element and add it to the
output stream of the transformation>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td><xsl:value-of select="catalog/cd/title"/></td>
<td><xsl:value-of select="catalog/cd/artist"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The <xsl:for-each> Element
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The <xsl:for-each> Element
The corresponding xml code is :
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>
XSLT <xsl:sort> Element
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>
</catalog>
XSLT <xsl:sort> Element
The corresponding xml code is :
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>

You might also like