Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I get get "parallel" attributes from xml?

33 views
Skip to first unread message

Juge

unread,
Sep 22, 2015, 7:52:46 AM9/22/15
to
I have following stuff in XML


<obj id="objectid_1" type="Object">
<attr name="name">result</attr>
<attr name="label">Result Label</attr>
<attr max="200.0" min="0.0" name="xAxis" nominal="100.0"
precision="0" qt="Frequency" units="Hz">100.0 Hz</attr>
<attr max="10" min="5" name="yAxis"
nominal="7.5" precision="0" qt="Damping" units="dB">7.5 Hz</attr>
</obj>

I am trying following in tdom:

set answer [http::data $token]
http::cleanup $token
dom parse $answer dom
$dom documentElement doc
set CurveList [$doc selectNodes {/objList/obj}]
set returnList ""
foreach prj $CurveList {
set oid [lindex [$prj selectNodes {@oid}] 0 1]
set name [[$prj selectNodes {attr[@name='name']}] asText]
set label [[$prj selectNodes {attr[@name='label']}] asText]
set xaxis [$prj selectNodes {attr/@qt}]
set yaxis [[$prj selectNodes {attr[@name='xAxis']}] asText]
}

the stuff I find with set xaxis [$prj selectNodes {attr/@qt}] is both
answers I want, but I am looking for a formulation where I can get directly
qt for name='xAxis' and qt for name='yAxis' but I do not have a clue how
to find a "parallel" attributes to the name?

heinrichmartin

unread,
Sep 22, 2015, 11:07:25 AM9/22/15
to
I am not sure whether I understood your request entirely. XPath is quite mighty, but might not return useful and reliable results.

expect:/tmp$ package require tdom
0.8.2
expect:/tmp$ dom parse {<obj id="objectid_1" type="Object">
> <attr name="name">result</attr>
> <attr name="label">Result Label</attr>
> <attr max="200.0" min="0.0" name="xAxis" nominal="100.0"
> precision="0" qt="Frequency" units="Hz">100.0 Hz</attr>
> <attr max="10" min="5" name="yAxis"
> nominal="7.5" precision="0" qt="Damping" units="dB">7.5 Hz</attr>
> </obj>} dom
domDoc0x256f4e0
expect:/tmp$ $dom documentElement doc
domNode0x25a7858
expect:/tmp$ $doc selectNodes {/obj/attr[substring(@name,2)='Axis']/@qt}
{qt Frequency} {qt Damping}
expect:/tmp$ $doc selectNodes {/obj/attr[@name='xAxis' or @name='yAxis']/@qt}
{qt Frequency} {qt Damping}
expect:/tmp$ lmap cur [$doc selectNodes {/obj/attr[@name='xAxis' or @name='yAxis']/@qt}] {lindex $cur 1}
Frequency Damping

What happens to your implementation, when someone changes the order of attr-elements? It looks like the chosen format is meant to be de-serialized and then to be read from some data structures ...

heinrichmartin

unread,
Sep 22, 2015, 11:11:34 AM9/22/15
to
On Tuesday, September 22, 2015 at 5:07:25 PM UTC+2, heinrichmartin wrote:
> I am not sure whether I understood your request entirely. XPath is quite mighty, but might not return useful and reliable results.

XPath is quite mighty, but might not return useful and reliable results _in your case_.

>
> expect:/tmp$ package require tdom
> 0.8.2
> expect:/tmp$ dom parse {<obj id="objectid_1" type="Object">
> > <attr name="name">result</attr>
> > <attr name="label">Result Label</attr>
> > <attr max="200.0" min="0.0" name="xAxis" nominal="100.0"
> > precision="0" qt="Frequency" units="Hz">100.0 Hz</attr>
> > <attr max="10" min="5" name="yAxis"
> > nominal="7.5" precision="0" qt="Damping" units="dB">7.5 Hz</attr>
> > </obj>} dom
> domDoc0x256f4e0
> expect:/tmp$ $dom documentElement doc
> domNode0x25a7858
> expect:/tmp$ $doc selectNodes {/obj/attr[substring(@name,2)='Axis']/@qt}
> {qt Frequency} {qt Damping}
> expect:/tmp$ $doc selectNodes {/obj/attr[@name='xAxis' or @name='yAxis']/@qt}
> {qt Frequency} {qt Damping}
> expect:/tmp$ lmap cur [$doc selectNodes {/obj/attr[@name='xAxis' or @name='yAxis']/@qt}] {lindex $cur 1}
> Frequency Damping

expect:/tmp$ lmap qt [$doc selectNodes {/obj/attr[@name='xAxis' or @name='yAxis']/@qt}] axis {x y} {list $axis [lindex $qt 1]}
{x Frequency} {y Damping}
0 new messages