Building an AjaxPro.NET Based Search Engine into Your Website - Introduction to XML/XSLT Techniques
(Page 2 of 4 )
As is now well known, XML has become one of the standard formats for transforming, storing, and rendering data. Thus, we will not dwell on it here; instead, we'll discuss XSLT.
In fact, XSLT is a kind of language expressed as a well-formed XML document. However, the XSLT-defined elements are distinguished by belonging to a specific XML namespace (i.e. XSLT namespace).
In practical use, XSLT is responsible for transforming various kinds of popular data. Thus, an ".xslt" file defines a series of rules for transforming a source data tree into a result data tree. The transformation is achieved by associating patterns with templates. A pattern is matched against elements in the source data tree. A template is instantiated to create part of the result data tree.
The result data tree is separate from the source data tree. The structure of the result data tree can be completely different from the structure of the source data tree. In constructing the result data tree, elements from the source data tree can be filtered and re-ordered, and arbitrary structure can be added.
A transformation expressed in XSLT is called a stylesheet, and contains a set of template rules. A template rule has two parts: a pattern which is matched against nodes in the source data tree and a template which can be instantiated to form part of the result data tree. This allows a stylesheet to be applicable to a wide class of documents that have similar source data tree structures.
XSLT makes use of the expression language defined by XPath for selecting elements for processing, for conditional processing and for generating text.
XPath is the result of an effort to provide a common syntax and semantics for functionality shared between XSL Transformations and XPointer. The primary purpose of XPath is to address parts of an XML document. To support this purpose, it also provides basic facilities for manipulation of strings, numbers and booleans. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values.
The primary syntactic construct in XPath is the expression. An expression matches the production Expr. An expression is evaluated to yield an object, which has one of the following four basic types:
- node set (an unordered collection of nodes without duplicates).
- boolean (true or false).
- number (a floating-point number).
- string (a sequence of UCS characters).
Putting aside the theories now, let’s consider a short example. The following illustrates a part of an ".xml" file which contains a reference to /category/items/category:
<category>
<items>
<category name="popular">
<!-- The XPath expression matches this category node -->
...
</category>
<category name="extras">
<!-- The XPath expression matches this category node -->
...
</category>
</items>
</category>
As you can see from the above snippet, a stylesheet is composed of rules. Each rule has a pattern which defines when it applies, and a template which defines what to output. Continuing with the example, here's a full rule defined in an ".xslt" file:
<xsl:template match="/category/items/category">
<div class="category"
onclick="retrieveCategory('{@name}')">
<xsl:value-of select="@name"/>
</div>
</xsl:template>
When each node is reached, the template body is outputted. @name refers to the name attribute on the category tag. So when the related processing engine reaches the following XML segment:
<category name="extras">
the following HTML will be output:
<div class="category"
onclick="retrieveCategory('{extras}')">
extras
</div>
The following HTML will be output:
<div class="category"
onclick="retrieveCategory('{extras}')">
extras
</div>
This wraps up the XML/XSLT discussion. Next, let’s continue with another interesting framework.
Next: Introduction to Google AJAXSLT >>
More .NET Articles
More By Xianzhong Zhu