tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Elson <>
Subject Re: xtag:context and xtag:variable
Date Fri, 13 Jul 2001 09:51:43 GMT
"William C. Robertson" wrote:

> Hello all,
> <xtag:context> does not seem to behave the same as it's cousin
> <xtag:forEach>.  For the same path, forEach works but context doesn't.  Is
> there something I'm missing?

Do you have any examples? I'm not sure what sort of behaviour your expecting to
be the same.

Originally we had only <xtags:forEach> which would iterate over a node set, and
didn't really need <xtags:context> as you could use an <xtags:forEach> on a
single node to change the context. However, it could be confusing to a page
developer as the forEach implied the author expected a multiple node set. Hence
the use of <xtags:context> for selecting a single node:

          <description>Some text</description>

<xtags:forEach select="/foo/bar">
    <xtags:valueOf select="description"/>

<xtags:context select="/foo/bar">
    <xtags:valueOf select="description"/>

...the description will be output the same twice.

However, the forEach tag will always have a single node as the context in the
body, whereas <xtags:context> may set a list of nodes.

It's also useful when you have a dom4j Document object from somewhere else:

<% Document doc = myBean.getADocument(); %>

<xtags:context context="doc" select="/">

(although it looks as though this hasn't been fully implemented yet.. the
"context" attribute is listed in the TLD, but has no setter in the ContextTag
class. James S?)

> <xtag:variable> will return "an Object or List" if "node" is not specified.
> Is the list a list in the sense of a "linkedList" or part of the io package?
> Where can I find out about how to use it?

It'll be a java.util.List (is that right James S?). You can specify the type of
the variable to be "string" (which creates a java.lang.String object with the
value of the node(s), "node" which returns an org.dom4j.Node (useful for further
dom4j API manipulation, or setting a context later), or if you specify something
else, you'll get a java.util.List of Nodes. If you don't specify a type, you'll
get the textual String value.

<xtags:forEach select="//foo">
    <xtags:variable id="attr" select="@attr"/>
    The attribute value is <%=attr%>. That's <%=attr%> I say.

Very useful if you need to use the value of a node as a parameter to another
custom tag (as you can't use custom tags here), and to reduce the verbosity of a


View raw message