AxKit.org [logo curtesy of http://xml.com]
--sep--
Start Navigation
About AxKit
Index
xml.apache.org
Features
Live Sites
Installation
Documentation
Daily Churn
Getting AxKit
License
Download
Mailing List
Contribute
CVS
Support
Bugs
End Navigation
Prev Top Next

More XPathScript Details

XPathScript aims to provide the power and flexibility of XSLT as an XML transformation language, without the restriction of XSLT's XML based syntax. Unlike XSLT, XPathScript only outputs plain text (XSLT has special modes for outputting in text, XML and HTML). This has advantages in being a lot easier to learn than XSLT for people coming from a Perl background, however XPathScript is not a W3C specification (although XPath, which XPathScript uses, is a W3C recommendation).

XPathScript follows the basic ASP syntax for introducing code, and outputting code to the browser; use <% %> to introduce Perl code, and <%= %> to output a value.

The XPathScript API

Along with the code delimiters XPathScript provides stylesheet developers with a full API for accessing and transforming the source XML file. This API can be used in conjunction with the delimiters above to provide a stylesheet language that is as powerful as XSLT, and yet provides all the features of a full programming language (in this case, Perl, but I'm certain that other implementations such as Python or Java would be possible).

Extracting Values

A simple example to get us started, is to use the API to bring in the title from a docbook article. A docbook article title looks like this:

<article>
 <artheader>
  <title>XPathScript - A Viable Alternative to XSLT?</title>
  ...

The XPath expression to retrieve the text in the title element is:

/article/artheader/title/text()

Putting this all together to make this text into the HTML title we get the following XPathScript stylesheet:

<html>
<head>
 <title><%= findvalue("/article/artheader/title") %></title>
</head>
<body>
  This was a DocBook Article. 
  We're only extracting the title for now!
<p>
The title was: <%= findvalue("/article/artheader/title") %>
</body>
</html>

Again we see the XPath syntax being used to find the nodes in the document, along with the function findvalue(). Similarly a list of nodes can be extracted (and thus looped over) using the findnodes() function:

...
<%
for my $sect1 (findnodes("/article/sect1")) {
  print $sect1->findvalue("title"), "<br>\n";
  for my $sect2 ($sect1->findnodes("sect2")) {
    print " + ", $sect2->findvalue("title"), "<br>\n";
    for my $sect3 ($sect2->findnodes("sect3")) {
      print " + + ", $sect3->findvalue("title"), "<br>\n";
    }
  }
}
%>
...

Here we see how we can apply the find* functions to individual nodes as methods, which makes the node the context node to search from, so $node->findnodes("title") finds <title> child nodes of $node.

Declarative Templates

We have already seen declarative templates in our "First AxKit Page" above. The $t hash is the key to declarative templates. The apply_templates() function iterates over the nodes of your XML file, applying the templates defined in the $t hash reference as it meets matching tags. This is the most important feature of XpathScript, because it allows you to define the appearance for individual tags without having to do your own iteration logic. We call this declarative templating.

The keys of $t are the names of the elements, including namespace prefixes where appropriate. When apply_templates() is called, XPathScript tries to find a member of $t that matches the element name.

The following sub-keys define the transformation:

  • pre

    the output to occur before the tag.

  • post

    the output to occur after the tag.

  • prechildren

    the output to occur before the children of this tag are written.

  • postchildren

    the output to occur after the children of this tag are written.

  • prechild

    output to occur before every child element of this tag.

  • postchild

    output to occur after every child element of this tag.

  • showtag

    set to a false value (generally zero) to disable rendering of the tag itself.

  • testcode

    code to execute upon visiting this tag.

More details about XPathScript can be found on the AxKit web page, at http://axkit.org/.


Prev Top Next

Printer Friendly
Raw XML