Jakarta Project: XTags Library

Version: 1.0

Table of Contents

Overview
Requirements
Configuration
Tag Summary
Tag Reference
Examples
Javadocs
Revision History

Overview

XTags implements an XSLT-like JSP tag library to allow navigating, processing and styling of XML documents directly in JSP. In many ways XTags is like XSLT implemented in JSP allowing you to seamlessly work with JSP, custom tags, JavaBeans and the whole J2EE platform from inside a single piece of JSP!

XTags makes heavy use of the XPath expression language. For a good tutorial on XPath you could try the either the Zvon tutorial or the specification.

XTags is currently built on top of dom4j the flexible open source XML framework for the Java platform. Though increasingly XTags will use a pluggable XPath engine to support the travesal of DOM and Java Beans too.

To begin with you need to parse an XML document from somewhere. You can parse a variety of sources of XML documents from local resources, the output of JSP or external web services.

You can parse the body of the tag

    <xtags:parse>
        <root>
            <child/>
        </root>
    </xtags:parse>

Or parse an absolute URL via the "url" attribute

    <xtags:parse url="http://something.com"/>

You can parse a web app resource using an absolute URI relative to the web-app context using the "uri" attribute

    <xtags:parse uri="/data/foo.xml"/>

Or you can use a URI relative to the the current JSP file

    <xtags:parse uri="foo.xml"/>

Then for more complex requirements such as parsing the output of a piece of JSP, when we get to JSP 1.2, we'll be able to do...

    <xtags:parse>
        <jsp:include page="foo.jsp"/>
    </xtags:parse>

Until then you can use the IO tag library to make these requests such as

    <xtags:parse>
        <io:request url="/foo.jsp"/>
    </xtags:parse>

Though the above would result in a seperate HTTP request which would loose all page, request and session scope state. So if it must be in the same request the following should work though care should be taken to avoid scripting variable clashes

    <xtags:parse>
        <%@ include file="/foo.jsp" %>
    </xtags:parse>

To parse the output of an XML-RPC (or SOAP) call, using the IO taglib you could do the following.

    <xtags:parse>
        <io:xmlrpc url="/xmlrpc_echo.jsp">
         <io:pipe>
          <methodCall>
             <methodName>examples.getStateName</methodName>
             <params>
                <param>
                   <value><i4>41</i4></value>
                   </param>
                </params>
             </methodCall>
         </io:pipe>
        </io:xmlrpc>

    </xtags:parse>

Once you have a document parsed you can navigate around its structure using XPath expressions in a similar manner to that used in XSLT.

Loops are as follows (an optional variable id can be specified to define a scriptlet expression inside the loop):-

  <xtags:forEach select="expression">
    ...
  </xtags:forEach>

Simple conditional branching is:-

  <xtags:if test="booeanExpression">
    ...
  </xtags:if>

More complex conditional branching is:-

  <xtags:choose>
    <xtags:when test="booeanExpression">
      ...
    </xtags:when>
    <xtags:when test="booeanExpression2">
      ...
    </xtags:when>
    <xtags:otherwise>
      ...
    </xtags:otherwise>
  </xtags:choose>

Expression evaluation

  <xtags:valueOf select="expression"/>

Defining scriptlet variables

  <xtags:variable id="variableName" select="expression"/>

All these tags are very similar to their XSLT equivalents, so anyone who's done XSLT before should find them familiar. There's also an <xtags:style> tag which performs complete XSL transform in one tag.

XPath expressions can use variables with the syntax $foo. XTags binds these variables to either page/request/session/application scope attributes or request parameters which allows xtags to be used as a conditional logic scripting language too - even without the existence of XML documents.

For example, the following JSP would branch logically based on the value of the (say) request parameter "param":-

  <xtags:choose>
    <xtags:when test="$param='a'">
      current param is 'a'
    </xtags:when>
    <xtags:when test="$param='b'">
      current param is 'b'
    </xtags:when>
    <xtags:otherwise>
      no valid param selected
    </xtags:otherwise>
  </xtags:choose>

XTags even supports the <xtags:stylesheet> <xtags:template> and <xtags:applyTemplates> tags from XSLT too though the body of a template must be an Action object or a seperate JSP file.

Requirements

This custom tag library depends on a servlet container that supports the JavaServer Pages Specification, version 1.1 or higher and also requires a distribution of dom4j and log4j. To be able to use the <xtags:style> tag then a distribution of JAXP is required along with an XSLT implementation such as xalan.jar and crimson.jar

Configuration

Follow these steps to configure your web application with this tag library:

To use the tags from this library in your JSP pages, add the following directive at the top of each page:

<%@ taglib uri="http://jakarta.apache.org/taglibs/xtags-1.0" prefix="xtags" %>

where "xtags" is the tag name prefix you wish to use for tags from this library. You can change this value to any prefix you like.

Tag Summary

parse Parsers some XML either from a given relative "uri", an explicit "url" or from the body of this tag. The "uri" is relative to the current web application.
valueOfEvaluates the given XPath expression on the current context node and outputs the result as text
forEach Evaluates the given XPath expression and iterates over the result, setting the context node to each element in the iteration. If the "id" attribute is specified then a scripting attribute will be defined with the current item of the iteration.
chooseBehaves like the corresponding XSLT tag. A choose tag contains zero or more when tags together with an optional otherwise tag.
whenBehaves like the corresponding XSLT tag
otherwiseBehaves like the corresponding XSLT tag.
breakBehaves like the Java 'break' statement causing the current foreach tag iteration to terminate.
ifBehaves like the corresponding XSLT tag, the body is evaluated if the XPath selection finds one or more nodes
variable Defines a scripting variable with the given id and the the value of the given XPath expression. By default the type will be a String. If the "type" attribute specifies a type of 'node' it convert the result to an org.dom4j.Node otherwise the result will be an Object or List.
stylePerforms an XSL transformation on the given XML document.
stylesheetPerforms a stylesheet on the current context. The body of this tag defines the stylesheet
templateDefines a template rule in an XSLT stylesheet. For each node which matches the pattern the JSP will be called. If no JSP file is specified then the value is copied using an XSLT style copy-of operation.
applyTemplatesPerforms an apply templates like the XSLT tag.
elementLike the XSLT tag, creates a new XML element.
attributeLike the XSLT tag, creates a new attribute for the outer element tag.
copyOfGenerates a deep copy of the current context like the XSLT <xsl:copy-of/> tag.
 

Tag Reference

parse Availability: version 1.0
Parsers some XML either from a given relative "uri", an explicit "url" or from the body of this tag. The "uri" is relative to the current web application.
 
Tag Classorg.apache.taglibs.xtags.tags.ParseTag
Tag BodyJSP
Script VariableIf the id attribute is set the variable with that id will be defined
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
idfalsefalse
urifalsetrue
urlfalsetrue
readerfalsetrue
Example
  1. <%-- --%>

                                                    
    <xtags:parse id="doc1">
      <root>
        <author firstName="James" surname="Strachan"/>
      </root>
    </xtags:parse>
    
    <xtags:parse id="doc2" url="http://www.acme.com/foo/bar.xml"/>
    
    <xtags:parse id="doc3" uri="/foo/bar.xml"/>
          
                                                

valueOf Availability: version 1.0
Evaluates the given XPath expression on the current context node and outputs the result as text
 
Tag Classorg.apache.taglibs.xtags.tags.ValueOfTag
Tag Bodyempty
Script VariableNo
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
selecttruetrue
contextfalsetrue
Example
  1. <%-- --%>

                                                    
          <xtags:valueOf select="//author/@surname" /> 
          
                                                

forEach Availability: version 1.0
Evaluates the given XPath expression and iterates over the result, setting the context node to each element in the iteration. If the "id" attribute is specified then a scripting attribute will be defined with the current item of the iteration.
 
Tag Classorg.apache.taglibs.xtags.tags.ForEachTag
Tag BodyJSP
Script VariableIf the id attribute is set the variable with that id will be defined
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
selecttruetrue
sortfalsetrue
distinctfalsetrue
contextfalsetrue
idfalsefalse
typefalsefalse
Example
  1. <%-- --%>

                                                    
    <xtags:forEach select="//author">
      <xtags:valueOf select="@name"/>      
    </xtags:forEach>
           
                                                

choose Availability: version 1.0
Behaves like the corresponding XSLT tag. A choose tag contains zero or more when tags together with an optional otherwise tag.
 
Tag Classorg.apache.taglibs.xtags.tags.ChooseTag
Tag BodyJSP
Script VariableNo
Restrictions
AttributesNone
Example
  1. <%-- --%>

                                                    
    <xtags:choose>
      <xtags:when test="firstName">
        Hello <xtags:valueOf select="@firstName"/>
      </xtags:when>
      <xtags:otherwise>
        Hello there friend
      </xtags:otherwise>
    </xtags:choose>
           
                                                

when Availability: version 1.0
Behaves like the corresponding XSLT tag
 
Tag Classorg.apache.taglibs.xtags.tags.WhenTag
Tag BodyJSP
Script VariableNo
RestrictionsShould be used within a <xtags:choose> tag
Attributes 
 
NameRequiredRuntime Expression Evaluation
testtruetrue
contextfalsetrue
Example
  1. <%-- --%>

                                                    
          
                                                

otherwise Availability: version 1.0
Behaves like the corresponding XSLT tag.
 
Tag Classorg.apache.taglibs.xtags.tags.OtherwiseTag
Tag BodyJSP
Script VariableNo
RestrictionsShould be used within a <xtags:choose> tag
AttributesNone
Example
  1. <%-- --%>

                                                    
          
                                                

break Availability: version 1.0
Behaves like the Java 'break' statement causing the current foreach tag iteration to terminate.
 
Tag Classorg.apache.taglibs.xtags.tags.BreakTag
Tag Bodyempty
Script VariableNo
RestrictionsShould be used within a <xtags:choose> tag
AttributesNone
Example
  1. <%-- --%>

                                                    
          
                                                

if Availability: version 1.0
Behaves like the corresponding XSLT tag, the body is evaluated if the XPath selection finds one or more nodes
 
Tag Classorg.apache.taglibs.xtags.tags.IfTag
Tag BodyJSP
Script VariableNo
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
testtruetrue
contextfalsetrue
Example
  1. <%-- --%>

                                                    
    <xtags:if select="@location='UK'">
      UK based
    </xtags:if>
          
                                                

variable Availability: version 1.0
Defines a scripting variable with the given id and the the value of the given XPath expression. By default the type will be a String. If the "type" attribute specifies a type of 'node' it convert the result to an org.dom4j.Node otherwise the result will be an Object or List.
 
Tag Classorg.apache.taglibs.xtags.tags.VariableTag
Tag Bodyempty
Script VariableIf the id attribute is set the variable with that id will be defined
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
idtruefalse
typefalsefalse
selecttruetrue
contextfalsetrue
Example
  1. <%-- --%>

                                                    
          <xtags:variable id="surname" select="@surname"/> 
          
                                                

style Availability: version 1.0
Performs an XSL transformation on the given XML document.
 
Tag Classorg.apache.taglibs.xtags.tags.StyleTag
Tag BodyJSP
Script VariableNo
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
documentfalsetrue
xmlfalsetrue
xmlReaderfalsetrue
xmlSourcefalsetrue
xslfalsetrue
xslReaderfalsetrue
xslSourcefalsetrue
resultfalsetrue
writerfalsetrue
outputMethodfalsetrue
Example
  1. <%-- --%>

                                                    
    <xtags:style xml="foo.xml" xsl="bar.xsl"/>
    
    <xtags:style context="<%= myDoc %>" xsl="bar.xsl"/>
    
    <xtags:style xsl="bar.xsl"/>
        <!-- this is the XML which will get styled -->
        <root>
            <foo bar="123"/>
        </root>
    </xtags:style>
          
                                                

stylesheet Availability: version 1.0
Performs a stylesheet on the current context. The body of this tag defines the stylesheet
 
Tag Classorg.apache.taglibs.xtags.tags.StylesheetTag
Tag BodyJSP
Script VariableNo
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
contextfalsetrue
Example
  1. <%-- --%>

                                                    
    <xtags:stylesheet>
      <xtags:template match="author" jsp="author.jsp"/>
      <xtags:template match="painter" jsp="painter.jsp"/>
    </xtags:stylesheet>
          
                                                

template Availability: version 1.0
Defines a template rule in an XSLT stylesheet. For each node which matches the pattern the JSP will be called. If no JSP file is specified then the value is copied using an XSLT style copy-of operation.
 
Tag Classorg.apache.taglibs.xtags.tags.TemplateTag
Tag BodyJSP
Script Variable
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
matchtruetrue
jspfalsetrue
actionfalsetrue
Example
applyTemplates Availability: version 1.0
Performs an apply templates like the XSLT tag.
 
Tag Classorg.apache.taglibs.xtags.tags.ApplyTemplatesTag
Tag BodyJSP
Script VariableNo
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
selectfalsetrue
modefalsetrue
Example
  1. <%-- --%>

                                                    
          <xtags:copyOf select="." /> 
          
                                                

element Availability: version 1.0
Like the XSLT tag, creates a new XML element.
 
Tag Classorg.apache.taglibs.xtags.tags.ElementTag
Tag BodyJSP
Script VariableNo
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
nametruetrue
Example
  1. <%-- --%>

                                                    
          <xtags:element name="author"/> 
          
                                                

attribute Availability: version 1.0
Like the XSLT tag, creates a new attribute for the outer element tag.
 
Tag Classorg.apache.taglibs.xtags.tags.AttributeTag
Tag BodyJSP
Script VariableNo
RestrictionsOnly usable inside an <xtags:element> tag
Attributes 
 
NameRequiredRuntime Expression Evaluation
nametruetrue
Example
  1. <%-- --%>

                                                    
          <xtags:attribute name="location">UK</xtags:attribute> 
          
                                                

copyOf Availability: version 1.0
Generates a deep copy of the current context like the XSLT <xsl:copy-of/> tag.
 
Tag Classorg.apache.taglibs.xtags.tags.CopyOfTag
Tag BodyJSP
Script VariableNo
Restrictions
Attributes 
 
NameRequiredRuntime Expression Evaluation
selecttruetrue
contextfalsetrue
Example
  1. <%-- --%>

                                                    
          <xtags:copyOf select="@*|text()" /> 
          
                                                

Examples

See the example application xtags-examples.war for examples of the usage of the tags from this custom tag library.

Java Docs

Java programmers can view the java class documentation for this tag library as javadocs.

Revision History

Review the complete revision history of this tag library.