velocity-user mailing list archives

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

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.
   http://www.dom4j.org/
   http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/   specifically the java-binding

To get some hints on stylesheet transformations you can get the specs
with good (but compact) examples at:
   http://www.w3.org/TR/xslt
   http://www.w3.org/TR/xpath

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 http://www.w3.org/TR/xslt#strip

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
here:
   http://www.w3.org/TR/xslt#element-for-each
Alternatively, you can do a check with an if directive:
>>>   #foreach ( $field in $node.children() )
#if( $field.getNodeTypeName() == "Element" )
>>>     <br>
>>>     $velocityCount
>>>     $field.name()
>>>     $field.value()
#end
>>>   #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()

     Returns:
         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: $field.class.name
#foreach( $method in $field.getMethods() )
   $method.returnType.name ${method.name}(#*
     *##set($sep="")#*
     *##foreach($p in $method.parameterTypes)$sep$p.name#set($sep=", ")#end#*
   *#)
#end

> 
> Thank's Soeren Præstegaard
> 

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

Ciao,
Christoph

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


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message