HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
PROFESSIONAL VISUAL BASIC 6 XML PART 1 - USING XML QUERIES AND TRANSFORMATIONS

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

Now we have an easy and platform-independent method of describing XML data, validating its type as we wish and modifying and reading it programmatically. So we basically have a transportable miniature database. No surprise then that when you start to work with it, you'll feel the need for a query mechanism. Using the DOM, you can get to each and every node in your document, but it can get tiresome, maneuvering through the hierarchies of children to find that single node you are interested in.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book Professional Visual Basic 6 for XML.


What we would like to have is an XML version of SQL. We would like to say "Get me all nodes of type X that have descendants of type Y". Many initiatives in this direction have been started up. There were some working groups specifying only a query language, but query mechanisms were also part of the drafts under development for transformation (XSLT) and linking technologies (XPointer). Then the W3C joined efforts with some of the working groups to specify XPath. XPath is a simple syntax to select a subset of the nodes in a document. It now has recommendation status and is used in both the XSLT and XPointer standards (as we'll see later in this chapter and in the next chapter).

Later in this chapter you will understand the importance of XPath in the context of transforming one document type to another, but first we will look at using XPath as a pure querying tool. In the initial release of IE5, a basic version of XPath implementation was included (then called XQL). Once XPath and XSLT gained recommendation status, Microsoft promised to deliver a fully compliant implementation of XPath and XSLT soon, and in January 2000 Microsoft shipped a developers preview of the MSXML library. In the appendices for XPath and XSLT, you can find exactly which features are supported in which releases.

We will work with the full version of XPath in this chapter. If you want to program for the MSXML library that came with IE5 originally (if you cannot update to the newer version on all installed versions), you are restricted to a subset of XPath. We will indicate what can be used in the earlier IE5 versions in a separate section.

Be aware of the fact that several (more powerful) XML query languages are still under development. These include a syntax called XQL, that has firm support from IBM and an initiative from the W3C, called XML Query, which is still in the first stages of specification. At the moment, XPath is the only way that has reached recommendation status and it looks like it will be a long time before anything else will.

This chapter will cover:

  • XPath for querying a document
  • XSLT for transforming a document
  • Styling a document with Cascading Style Sheets
  • Styling a document by using transformations (XSLT)

XPath Query Syntax

Before we get into the syntax of an XPath query, we have to discuss the concept of a context node. In XPath, a query is not automatically done over the whole of the content, but always has a starting point or context node. This can be any node in the node tree that constitutes the document. From this "fixed point" you can issue queries like "give me all your children". This kind of query only makes sense if there is a starting point defined. This starting point may be the root node, of course, which would query the entire document.

This may seem a bit abstract now, but just remember: an XPath query is done from a certain starting point in the document.

Different Axes

Have a look at the following XPath query:

descendant::TABLE

This query would translate to plain English as: "Get the TABLE elements from all descendants (children, children's children, etc) of the context node". The first part of this query, descendant, is called the axis of the query. The second part, TABLE, is called the node test. The axis is the searching direction; if a node along the specified axis conforms to the node test, it is included in the result set. These patterns can be very complex and can have subqueries in them. We will look at that later. First, we will list all available all axes that can be used in a query:

The ancestor, descendant, following, preceding and self axes partition the document. This means that these five axes together contain all nodes of the tree (except attributes and namespaces), but do not overlap. This means that an ancestor is not on the preceding axis and that a descendant is not on the following axis, as illustrated in the following diagram:

Different Node Tests

The sample we showed before used a literal name (TABLE) as a node test. This is only one of the ways to specify what a selected node should look like. Other valid values are:

  • text() - which is true for all text nodes.
  • * - which is true for any node of the principal type and every axis has its own principal node type. For most axes the principal node type is 'element', but for the attribute axis it is 'attribute' and for the namespace axis, the principal type is 'namespace'.
  • comment() - which is true for all comment nodes.
  • processing-instruction() - which is true for all processing instruction nodes.
  • node() - which is true for all nodes.

These node type tests take no arguments. Only the processing-instruction can be passed a literal; if an argument is passed, the node test is only true for a processing instruction that has a name equal to the argument.

The following are examples of XPath queries using different axes and node tests. This selects all descendant elements from the context node:

descendant::*

This selects the name attribute from the context node:

attribute::name

This selects the parent node of the context node:

parent::*

This selects all namespaces that are valid in the context node:

namespace::*

This means that it includes the default namespace, the xml namespace, any namespaces that are declared in the context node, and any namespaces declared in ancestors of the context node that have not been overruled by declarations in their children. The overruling of a namespace happens when one element declares a prefix to a certain URI and a child node declares a namespace with the same prefix, but with another URI. In this case, the first declaration is removed and becomes invisible from nodes that are descendants of the element wit the second declaration.

Finally, this query selects all comment nodes that are a direct child of the context node:

child::comment()

Continued...


NEXT PAGE



5 RELATED COURSES AVAILABLE
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
MICROSOFT INTERNET EXPLORER 6.0 INTERNET INTRODUCTION
This course provides readers with an introduction to the concept of the Internet and the opportunity to gain a br....
A+ MODULE 5 - THE INTERNET
At the end of this course you will be able to: describe the functions of an operating system, describe the featur....
JAVASCRIPT PROGRAMMING
This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as -....
I-NET+ MODULE 8 - DEVELOPING A WEB SITE
On completion of this module, readers will be able to: create HTML pages incorporating different document-, parag....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Saturday 22nd November 2008  © COPYRIGHT 2008 - VISUALSOFT