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.
|
| valueOf | Evaluates 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.
|
| choose | Behaves like the corresponding XSLT tag. A choose tag contains zero or more when tags together with an optional otherwise tag. |
| when | Behaves like the corresponding XSLT tag |
| otherwise | Behaves like the corresponding XSLT tag. |
| break | Behaves like the Java 'break' statement causing the current foreach tag iteration to terminate. |
| if | Behaves 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.
|
| style | Performs an XSL transformation on the given XML document. |
| stylesheet | Performs a stylesheet on the current context. The body of this tag defines the stylesheet |
| template | 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.
|
| applyTemplates | Performs an apply templates like the XSLT tag. |
| element | Like the XSLT tag, creates a new XML element. |
| attribute | Like the XSLT tag, creates a new attribute for the outer element tag. |
| copyOf | Generates 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 Class | org.apache.taglibs.xtags.tags.ParseTag |
| Tag Body | JSP |
| Script Variable | If the id attribute is set the variable with that id will be defined |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| id | false | false |
|
| uri | false | true |
|
| url | false | true |
|
| reader | false | true |
|
|
| Example |
-
<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"/>
|
|
|
|
| Evaluates the given XPath expression on the current context node and outputs the result as text |
| |
| Tag Class | org.apache.taglibs.xtags.tags.ValueOfTag |
| Tag Body | empty |
| Script Variable | No |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| select | true | true |
|
| context | false | true |
|
|
| Example |
-
<xtags:valueOf select="//author/@surname" />
|
|
|
|
|
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 Class | org.apache.taglibs.xtags.tags.ForEachTag |
| Tag Body | JSP |
| Script Variable | If the id attribute is set the variable with that id will be defined |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| select | true | true |
|
| sort | false | true |
|
| distinct | false | true |
|
| context | false | true |
|
| id | false | false |
|
| type | false | false |
|
|
| Example |
-
<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 Class | org.apache.taglibs.xtags.tags.ChooseTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | |
| Attributes | None |
| Example |
-
<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 Class | org.apache.taglibs.xtags.tags.WhenTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | Should be used within a <xtags:choose> tag |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| test | true | true |
|
| context | false | true |
|
|
| Example |
-
|
|
|
|
| Behaves like the corresponding XSLT tag. |
| |
| Tag Class | org.apache.taglibs.xtags.tags.OtherwiseTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | Should be used within a <xtags:choose> tag |
| Attributes | None |
| Example |
-
|
|
|
break
| Availability: version 1.0 |
|
| Behaves like the Java 'break' statement causing the current foreach tag iteration to terminate. |
| |
| Tag Class | org.apache.taglibs.xtags.tags.BreakTag |
| Tag Body | empty |
| Script Variable | No |
| Restrictions | Should be used within a <xtags:choose> tag |
| Attributes | None |
| Example |
-
|
|
|
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 Class | org.apache.taglibs.xtags.tags.IfTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| test | true | true |
|
| context | false | true |
|
|
| Example |
-
<xtags:if select="@location='UK'">
UK based
</xtags:if>
|
|
|
|
|
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 Class | org.apache.taglibs.xtags.tags.VariableTag |
| Tag Body | empty |
| Script Variable | If the id attribute is set the variable with that id will be defined |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| id | true | false |
|
| type | false | false |
|
| select | true | true |
|
| context | false | true |
|
|
| Example |
-
<xtags:variable id="surname" select="@surname"/>
|
|
|
style
| Availability: version 1.0 |
|
| Performs an XSL transformation on the given XML document. |
| |
| Tag Class | org.apache.taglibs.xtags.tags.StyleTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| document | false | true |
|
| xml | false | true |
|
| xmlReader | false | true |
|
| xmlSource | false | true |
|
| xsl | false | true |
|
| xslReader | false | true |
|
| xslSource | false | true |
|
| result | false | true |
|
| writer | false | true |
|
| outputMethod | false | true |
|
|
| Example |
-
<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>
|
|
|
|
| Performs a stylesheet on the current context. The body of this tag defines the stylesheet |
| |
| Tag Class | org.apache.taglibs.xtags.tags.StylesheetTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| context | false | true |
|
|
| Example |
-
<xtags:stylesheet>
<xtags:template match="author" jsp="author.jsp"/>
<xtags:template match="painter" jsp="painter.jsp"/>
</xtags:stylesheet>
|
|
|
|
| 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 Class | org.apache.taglibs.xtags.tags.TemplateTag |
| Tag Body | JSP |
| Script Variable | |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| match | true | true |
|
| jsp | false | true |
|
| action | false | true |
|
|
| Example |
|
|
|
|
| Performs an apply templates like the XSLT tag. |
| |
| Tag Class | org.apache.taglibs.xtags.tags.ApplyTemplatesTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| select | false | true |
|
| mode | false | true |
|
|
| Example |
-
<xtags:copyOf select="." />
|
|
|
|
| Like the XSLT tag, creates a new XML element. |
| |
| Tag Class | org.apache.taglibs.xtags.tags.ElementTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| name | true | true |
|
|
| Example |
-
<xtags:element name="author"/>
|
|
|
|
| Like the XSLT tag, creates a new attribute for the outer element tag. |
| |
| Tag Class | org.apache.taglibs.xtags.tags.AttributeTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | Only usable inside an <xtags:element> tag |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| name | true | true |
|
|
| Example |
-
<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 Class | org.apache.taglibs.xtags.tags.CopyOfTag |
| Tag Body | JSP |
| Script Variable | No |
| Restrictions | |
| Attributes | |
| |
| Name | Required | Runtime Expression Evaluation |
| select | true | true |
|
| context | false | true |
|
|
| Example |
-
<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.