| Querying using
Query language |
Query language can be used to access a portion
of an XML document and process it. Queries can be used to
retrieve row-wise information as shown. The star (*) symbol
implies that all the columns of the records that satisfy the
where condition are retrieved.
Select * from <table>
where <column>=value"
Queries can be used to retrieve column-wise
information as shown. Here only columns column1, column2,
column3 of all rows are retrieved.
Select column1, column2,
column3 from <table>
Summarized information of a computational
column can be obtained as shown.
Select sum(computational_column) from <table>
Joins can be performed. That is, columns
from more than one table can be retrieved as shown.
Select column1,column2,column3, column4 from
<table1>,<table2>
One of the general forms of manipulating
table contents using standard Structured Query Language is
as shown.
Update <table>
set <column>=<value1>,
<column2>=<value2>
| Querying XML
using XPath and XSLT |
In 1998, a couple of groups started working
on proposals to the W3C mechanism to query XML documents.
Two earlier query languages submitted for proposals were XML-QL
and XQL. But there were some short- comings in those querying
languages.
The current state of querying technology
uses XSLT (Extensible style sheet language transformation)
and XPath to query XML documents. XSLT may be used to transform
one XML document into another, addressing some of the querying
needs.
XPath provides flexible mechanism for addressing
individual portions of an XML document to any granularity,
even down to individual characters in text elements. XPointer
extends the functionality of XPath in two ways.
XPointer extends XPath to allow for some
additional functionality. It introduces the concept of a point
location, as distinct from a node.
XPointer also defines the concept of a range
location, which is defined to be the XML structure and content
between two points. The ability to specify ranges would allow
a pointer to point to all occurrences of a particular word
in a target document.
#xpointer (<locator>
to <locator>)
The extensible style sheet language (XSL)
is an XML language designed to transform an XML document into
another XML document or to transform an XML document into
rendition objects.
XSLT is written in XML. This means that an XSLT sheet is in
itself a well-formed XML document.
XPath is used to access individual nodes
of an XML document at the same time, preserving the hierarchy
and structure of the original document.XSLT uses XPath to
filter nodes out of document, while XSLT is used to present
the data in any number of forms.
| Row-wise restriction
of returned information |
Here is the code "catalog.xml".
We shall later see the various queries performed on this XML
document.
<?XML version="1.0" encoding="utf-8"
standalone="yes"?>
<Catalog>
<Book>
<Title> C++
Programmers' reference </Title>
<Authors>
<Author>
Stroup stru </Author>
</Authors>
<Publishers> Timman
Press, Ltd.</Publisher>
<PubDate>August
1999</PubDate>
<Abstract> Object
oriented programming using C++ </Abstract>
<Pages> 480
</Pages>
<ISBN> 1-861001-57-6 </ISBN>
<RecSubjCategories>
<Category> Object
oriented </Category>
<Category> Applications
</Category>
</RecSubjCategories>
<Price>$49.99</Price>
</Book>
<Book>
</Book>
<Catalog>
In XML, the equivalent of row-wise restriction
of information would be the restriction of elements based
on their content. XPath expression may be used to look for
a specific value in a field. The following style sheet can
be used to return all books in our catalog with an author
called Bjarne Strustroup. The portion of the template that
performs row-wise selection is the <xsl:for-each> block
in the first template.
<xsl:stylesheet xmnls:xsl=http://www.w3.org/1999/XSL /Transform
version="1.0">
<xsl:template
match="/Catalog">
<xsl:copy>
<xsl:for-each
select="//Book[Author='Bjarne Stroustroup']">
<xsl:copy>
<xsl:apply-templates
name="childnodes"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template
name="childnodes" match="*">
<xsl:copy>
<xsl:apply-templates
name="childnodes"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The output of this script when run against
catalog.xml document returns three books, whose author is
Strustroup.
<?xml version="1.0" encoding
="utf-8"?>
<Catalog>
<Book>
<Title>C++
Programmer ' s reference </Title>
<Authors>
<Author>
Bjarne Stroustroup </Author>
</Authors>
<Publishers>Timman
Press, Ltd.</Publisher>
<PubDate>August
1999</PubDate>
<Abstract> Object
oriented programming using C++ </Abstract>
<Pages> 480
</Pages>
<ISBN> 1-861001-57-6 </ISBN>
<RecSubjCategories>
<Category> Object
oriented </Category>
<Category> Applications
</Category>
</RecSubjCategories>
<Price>$49.99</Price>
</Book>
<Book>
<Title Professional C++</Title>
</Book>
<Book>
<Title> C++ for beginners
</Title>
</Book>
<Catalog>
For XML, column-wise restriction of returned
information is achieved by restricting the text-level elements
or attributes returned by a style sheet. Here we display only
the titles of the book written by Bjarne Stroustroup. We use
the style sheet specified here.
<xsl:stylesheet xmnls:xsl=http://www.w3.org/1999/XSL
/Transform version="1.0">
<xsl:template
match="/Catalog">
<xsl:copy>
<xsl:for-each
select="//Book[./Authors/Author = 'Bjarne Strustroup']">
<xsl:copy>
<xsl:apply-templates
select ="Title" name="childnodes"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template
name="childnodes" match="*">
<xsl:copy>
<xsl:apply-templates
name="childnodes"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The output of the query associated with
the xml document, catalog.xml is as shown.
<?xml version="1.0" encoding
="utf-8"?>
<Catalog>
<Book>
<Title>C++'
s reference </Title>
</Book>
<Book>
<Title> Professional
C++</Title>
</Book>
<Book>
<Title> C++
for Beginners </Title>
</Book>
</Catalog>
Let us sort the titles of the books written
by the author Bjarne Strustroup. The following style sheet
is applied across the catalog.xml document. We have simply
added <xsl:sort> element to the <xsl:for-each>
element and hence would be sorted by Title.
<xsl:stylesheet xmnls:xsl=http://www.w3.org/1999/XSL
/Transform version="1.0">
<xsl:template
match="/Catalog">
<xsl:copy>
<xsl:for-each
select="//Book[./Authors/Author = 'Bjarne Strustroup']">
<xsl:sort
select ="Title"/>
<xsl:copy>
<xsl:apply-templates
select ="Title" name="childnodes"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template
name="childnodes" match="*">
<xsl:copy>
<xsl:apply-templates
name="childnodes"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Final output of the transformation is as
shown. Thus we see that the titles of the books are ordered.
Similarly we can perform all the standard operations of SQL
on an XML document.
<?xml version="1.0" encoding
="utf-8"?>
<Catalog>
<Book>
<Title> C++
for Beginners </Title>
</Book>
<Book>
<Title> C++'
s reference </Title>
</Book>
<Book>
<Title> Professional
C++ </Title>
</Book>
</Catalog>
Copyrights : Layout Galaxy All Rights Reserved
No part of this tutorial may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, electrostatic, magnetic tape, mechanical or otherwise, without prior permission in writing from Layout Galaxy.
|
|