1.0 1.1 xtags http://jakarta.apache.org/taglibs/xtags-1.0 XTags is a JSP tag library for working with XSLT and XPath. XTags lets you navigate, process and style XML documents directly in 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, you can do the following (provided you are using a JSP 1.2 container) <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. Accessing JSP scopes from XPathXPath 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. A prefix can be used to denote the exact JSP scope from which a variable comes from such as $request:foo to denote request scope. There now follows a full breakdown of all the current JSP scopes $foomaps to pageContext.findAttribute("foo")$page:foomaps to page scope$request:foomaps to request scope$session:foomaps to session scope$app:foomaps to application scope$param:foomaps to request.getParameter("foo")$initParam:foomaps to request.getInitParameter("foo")$cookie:foomaps to the cookie's value for name foo$header:foomaps to request.getHeader("foo") 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. style org.apache.taglibs.xtags.xslt.StyleTag JSP 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 param org.apache.taglibs.xtags.xslt.ParamTag JSP name true true value false true parse org.apache.taglibs.xtags.xpath.ParseTag org.apache.taglibs.xtags.xpath.ParseTagExtraInfo JSP id false false uri false true url false true reader false true validate false true valueOf org.apache.taglibs.xtags.xpath.ValueOfTag empty select true true context false true forEach org.apache.taglibs.xtags.xpath.ForEachTag org.apache.taglibs.xtags.xpath.ForEachTagExtraInfo JSP select true true sort false true distinct false true ascending false true context false true id false false type false false choose org.apache.taglibs.xtags.xpath.ChooseTag JSP when org.apache.taglibs.xtags.xpath.WhenTag JSP test true true context false true otherwise org.apache.taglibs.xtags.xpath.OtherwiseTag JSP break org.apache.taglibs.xtags.xpath.BreakTag empty if org.apache.taglibs.xtags.xpath.IfTag JSP test true true context false true variable org.apache.taglibs.xtags.xpath.VariableTag org.apache.taglibs.xtags.xpath.VariableTagExtraInfo empty id true false type false false select true true context false true stylesheet org.apache.taglibs.xtags.xpath.StylesheetTag JSP context false true template org.apache.taglibs.xtags.xpath.TemplateTag JSP match true true mode false true avt false true action false true applyTemplates org.apache.taglibs.xtags.xpath.ApplyTemplatesTag JSP select false true mode false true element org.apache.taglibs.xtags.xpath.ElementTag JSP name true true attribute org.apache.taglibs.xtags.xpath.AttributeTag JSP name true true output org.apache.taglibs.xtags.xpath.OutputTag JSP method false true indent false true omitXmlDeclaration false true copy org.apache.taglibs.xtags.xpath.CopyTag JSP select true true context false true copyOf org.apache.taglibs.xtags.xpath.CopyOfTag JSP select true true context false true context org.apache.taglibs.xtags.xpath.ContextTag JSP select true true context false true remove org.apache.taglibs.xtags.xpath.RemoveTag empty select true true add org.apache.taglibs.xtags.xpath.AddTag jsp before false true after false true replace org.apache.taglibs.xtags.xpath.ReplaceTag jsp