Hi Peter,
Please see my comments bellow:
On Nov 2, 2011, at 6:06 AM, Peter Secomb wrote:
> Hi everyone,
>
> I'm running into some difficulty with xpath_eval. I have a function that
> loops through the results of xpath_eval when the index is set to 0 to have it
> return a vector of results. However I'm getting unexpected behaviour. My
> test function is below. The function is commented to indicate what I am
> expecting vs what I am getting.
>
> create procedure test_xml_parser()
> {
>
> declare xml_str any;
> declare i int;
> declare xml_inner any;
>
> i:= 0;
>
> xml_str :=
> xtree_doc('<output><person><pin>98765</pin></person></output><output><person><pin>12345</pin></person></output>');
>
> dbg_printf('\nStart');
>
> dbg_obj_print(xml_str);
>
> -- Create a vector of <output> tags from xml_str and loop through them
> foreach(varchar xml_output IN xpath_eval('//output',xml_str,0)) do {
>
> dbg_obj_print(xml_output);
>
> -- dbg_printf(xml_output); This line errors. xml_output is still considered
> an xml_entity despite casting to varchar as part of the foreach
the 'foreach' do not cast, it declare a local variable xml_output. note that in
Virtuoso/PL you can assign to a variable of typeX value of typeY , this is not
considered as an error, it is a sql warning.
>
> dbg_printf(cast(xml_output as varchar)); --This line returns the value
> contained between the <pin> tags and not the expected <output> tags.
>
The cast to varchar do not serialize xml entity it takes the text nodes only,
use serialize_to_UTF8_xml (in xml_entity) instead.
> dbg_printf('Person start');
>
> dbg_obj_print(xpath_eval('//person', xml_output,0)); --Returns all
> instances of <person> found in xml_str and NOT the single instance of
> <person> found in xml_output
>
The results from xpath_eval are references to the source xml entity thus as
xpath scope is from root will get all of them, to get a copy should do :
declare x any;
x := xml_cut (xml_output);
then do over x e.g. xpath_eval('//person', x)
or you can specify xpath expression relative to the context e.g.
xpath_eval('./person', xml_output) .
HTH
Best Regards,
Mitko
> dbg_printf('Person end');
>
> }
>
> I am quite confused about what is going on. My expectation is that for each
> loop of the outer foreach I will see 1 instance of the <person> tags returned
> as only 1 instance of <person> is contained in any given <output></output>
> set.
>
> Regards,
> Peter Secomb
> clearbluewater.com.au
> ------------------------------------------------------------------------------
> RSA® Conference 2012
> Save $700 by Nov 18
> Register now!
> http://p.sf.net/sfu/rsa-sfdev2dev1_______________________________________________
> Virtuoso-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/virtuoso-users
--
Mitko Iliev
Developer Virtuoso Team
OpenLink Software
http://www.openlinksw.com/virtuoso
Cross Platform Web Services Middleware