You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Ralph Goers <ra...@dslextreme.com> on 2013/10/02 17:21:07 UTC

foreach with XMLTool

I have an XML document of the form

<data>
  <details>
    <row>
      <item1>
      <item2>
    </row>
    <row>
      <item1>
      <item2>
    </row>
  </details>
</data>

I cannot seem to find a way to successfully iterate over the row elements.  For some reason the data from each of the rows is being co-mingled.  Does anyone have an example of iterating over xml elements using the XMLTool?

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


Re: foreach with XMLTool

Posted by Ralph Goers <ra...@dslextreme.com>.
I suspect the solution is going to require changing get() to do


        if (s.indexOf('/') < 0)
        {
            s = "./" + s;
        }

instead of 

        if (s.indexOf('/') < 0)
        {
            s = getPath()+'/'+s;
        }

I'm not sure why it makes sense to have get always start at the root instead of at the current node.

Ralph

On Oct 2, 2013, at 11:23 AM, Nathan Bubna <nb...@gmail.com> wrote:

> Hmm. Sounds less than ideal. I don't have time to improve how the
> XMLTool handles this, but if you're up for filing a bug and perhaps
> even contributing a patch, that'd be great. :)
> 
> On Wed, Oct 2, 2013 at 11:05 AM, Ralph Goers <ra...@dslextreme.com> wrote:
>> I found that the problem isn't in the foreach.  It is that when you do
>> 
>> #foreach($row in $xml.details.row)
>>   $row.item1
>> #end
>> 
>> It results in item1 printing the item1 value for all rows.  This is because get() calls find("item1") which then calls node.selectNodes() in dom4j.  dom4j returns all the the nodes.  The only way I have found to fix this is to do
>> 
>>   $row.find("./item1")
>> 
>> This returns the value for only the current row.
>> 
>> Ralph
>> 
>> 
>> On Oct 2, 2013, at 8:21 AM, Ralph Goers <ra...@dslextreme.com> wrote:
>> 
>>> I have an XML document of the form
>>> 
>>> <data>
>>> <details>
>>>   <row>
>>>     <item1>
>>>     <item2>
>>>   </row>
>>>   <row>
>>>     <item1>
>>>     <item2>
>>>   </row>
>>> </details>
>>> </data>
>>> 
>>> I cannot seem to find a way to successfully iterate over the row elements.  For some reason the data from each of the rows is being co-mingled.  Does anyone have an example of iterating over xml elements using the XMLTool?
>>> 
>>> Ralph
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


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


Re: foreach with XMLTool

Posted by Nathan Bubna <nb...@gmail.com>.
Hmm. Sounds less than ideal. I don't have time to improve how the
XMLTool handles this, but if you're up for filing a bug and perhaps
even contributing a patch, that'd be great. :)

On Wed, Oct 2, 2013 at 11:05 AM, Ralph Goers <ra...@dslextreme.com> wrote:
> I found that the problem isn't in the foreach.  It is that when you do
>
> #foreach($row in $xml.details.row)
>    $row.item1
> #end
>
> It results in item1 printing the item1 value for all rows.  This is because get() calls find("item1") which then calls node.selectNodes() in dom4j.  dom4j returns all the the nodes.  The only way I have found to fix this is to do
>
>    $row.find("./item1")
>
> This returns the value for only the current row.
>
> Ralph
>
>
> On Oct 2, 2013, at 8:21 AM, Ralph Goers <ra...@dslextreme.com> wrote:
>
>> I have an XML document of the form
>>
>> <data>
>>  <details>
>>    <row>
>>      <item1>
>>      <item2>
>>    </row>
>>    <row>
>>      <item1>
>>      <item2>
>>    </row>
>>  </details>
>> </data>
>>
>> I cannot seem to find a way to successfully iterate over the row elements.  For some reason the data from each of the rows is being co-mingled.  Does anyone have an example of iterating over xml elements using the XMLTool?
>>
>> Ralph
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>

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


Re: foreach with XMLTool

Posted by Ralph Goers <ra...@dslextreme.com>.
I found that the problem isn't in the foreach.  It is that when you do

#foreach($row in $xml.details.row)
   $row.item1
#end

It results in item1 printing the item1 value for all rows.  This is because get() calls find("item1") which then calls node.selectNodes() in dom4j.  dom4j returns all the the nodes.  The only way I have found to fix this is to do

   $row.find("./item1")

This returns the value for only the current row.

Ralph


On Oct 2, 2013, at 8:21 AM, Ralph Goers <ra...@dslextreme.com> wrote:

> I have an XML document of the form
> 
> <data>
>  <details>
>    <row>
>      <item1>
>      <item2>
>    </row>
>    <row>
>      <item1>
>      <item2>
>    </row>
>  </details>
> </data>
> 
> I cannot seem to find a way to successfully iterate over the row elements.  For some reason the data from each of the rows is being co-mingled.  Does anyone have an example of iterating over xml elements using the XMLTool?
> 
> Ralph


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