velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christoph Reck <>
Subject Re: To many iterations in DVSL foreach directive
Date Tue, 13 Jul 2004 07:15:05 GMT

DVSL goes along the line of XSLT, with a different syntax/markup.
So you have the selectNodes and applyTemplates methods, the
match directive declarations, together with XPath statements
to do your chore. The objects being manipulated are dom4j nodes.

So the first place to look at is the DOM4J (or w3c DOM) documentation.   specifically the java-binding

To get some hints on stylesheet transformations you can get the specs
with good (but compact) examples at:

Soren Praestegaard wrote:
> It seems a little illogical to me, that the text value of an element is
> returned as one of the nodes, cause in my first example, where I did not
> alter the source code, the iteration looped three times pr. Element
> node, witch means that the DVSL engine treats all white spaces as one
> node each. That can't be so, or what?

Many XML parsers read every line as a text node. So the whitespace
before and after an element is each place in its own text node.

When you have an XML document without DTD (which would defines where
text is expected), you can "normalize" a XML document, merging
text nodes and whitespaces as a HTML parser/renderer does (consecutive
spaces into one and text nodes together.). In XSLT you have the
xsl:strip-space and xsl:preserve-space directive to swith this. See
"Whitespace Stripping" at

If in your example you would have processed the nodes each with a match
directive, you would not have notices the whitespace. Since you did the
looping yourself, you need to take care of them. One way is described
Alternatively, you can do a check with an if directive:
>>>   #foreach ( $field in $node.children() )
#if( $field.getNodeTypeName() == "Element" )
>>>     <br>
>>>     $velocityCount
>>>     $
>>>     $field.value()
>>>   #end

> BTW, how do you check that the node is an element? It doesn't say so on
> the DVSL homepage, and is there any other good resource on the not,
> concerning DVSL?

public String getNodeTypeName()

         the name of the type of node such as "Document", "Element", "Attribute" or "Text"

More resources listed above.

In Velocity, you can call any public method on objects in the context.
For the fun of it, you could even do:
Class: $
#foreach( $method in $field.getMethods() )
   $ ${}(#*
     *##foreach($p in $method.parameterTypes)$sep$$sep=", ")#end#*

> Thank's Soeren Præstegaard

So your documentation resources are the specs, api-docs, and some examples.


> Christoph Reck wrote:
>>the iterator is also returning the text nodes (containing whitespaces).
>>So check that it is an element node before accessing $

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message