Java Tutorial - Java Scipt : Xalan

Java Tutorial - Java Scipt :

Xalan

Xalan is the Apache project that implements the XSL technologies. Two major specifications that fall under that heading are XSLT and XPath. XSLT is used to specify transformations on an XML document, while XPath is used to query and access information from an XML document. The following table provides a summary of Xalan.
XSLT is the language for specifying transformations. Like the documents it transforms, XSLT itself is based on XML. It uses a stylesheet to specify the transformation rules, which are basically XML elements within the XSLT namespace that contain a prerequisite pattern and a template. If an element in the source XML document matches that pattern, then the template is applied to generate the transformed document. We’ll see examples of XSLT in the Integration and Testing section later in this chapter.

XPath is one of the more promising XML technologies. It’s somewhat akin to a query language for XML, although there are competing standards coming down the pipeline. Writing queries in XPath is more natural and compact than using the programmatic interface. For example, the XPath query to find all nodes named line_item looks like this:
//line_item

XPath also supports conditionals and attributes. For example, to find all nodes named line_item with a count greater than 5, the XPath expression is as follows:

//line_item[ @count > 5 ]

Here’s the rough equivalent in JDOM without using XPath:

Collection oMatchingElements = new ArrayList();
Iterator itr = oElement.getChildren();
while( itr.hasNext() )
{
Element oChild = (Element) itr.next();
String sCount = oChild.getAttributeValue( “count” );
if( Integer.parseInt( sCount ) > 5 )
{
oMatchingElements.add( oChild );
}
}

As you can see, XPath syntax is much more compact than the comparable Java code using a programmatic model. In fact, the JDOM code only iterates through the first level of children—additional code would be needed to recurse through all children at any depth in order to match the functionality in the XPath expression. XPath has expressiveness of SQL and can be applied to similar types of situations.

Overall, Xalan provides high-quality implementations of XSLT and XPath. Because it is included with Java as of version 1.4, you can rely on Xalan being available for use in most environments. However, in most cases a higher-level XML tool, such as dom4j or JDOM, is a better choice for harnessing the power of XSLT and XPath. The drawback is that the application will then be relying on proprietary APIs and libraries, which may
not be possible in some cases.