List Info

Thread: How to select a node based on attribute values?




How to select a node based on attribute values?
user name
2006-11-29 00:57:58
Fist off, I'm a total newbie when it comes to libxml.  I've
been able to 
work my way through some examples, and produce working code,
but I'm not 
entirely happy with the result, and I would like some
advice.

I have an XML tree that looks like this:

<root>
    <plc id="1">
       <some stuff/>
    </plc>
    <plc id="2">
       <some other stuff/>
    </plc>
</root>

I have a relatively small number (< 50) of the plc nodes,
but the whole 
tree is pretty big.

What is the best (or simplest way) to retrieve the
information for plc 2?

Should I pull all of the plc elements using
xmlXPathEvalExpression, and 
then iterate over them with xmlGetProp looking for an id
attribute with 
a value of "2"?

Or is there some other way to directly specify, I want this
element with 
these attributes?

Thanks for any guidance....  The docs are really thorough,
but libxml is 
so rich that the learning curve is a killer.....

--Yan

_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xmlgnome.org
http://mai
l.gnome.org/mailman/listinfo/xml
How to select a node based on attribute values?
user name
2006-11-29 02:35:38
Fist off, I'm a total newbie when it comes to libxml.  I've been able to 
work my way through some examples, and produce working code, but I'm not 
entirely happy with the result, and I would like some advice.

I have an XML tree that looks like this:

<root&gt;
    <plc id="1">
       <some stuff/>
    </plc&gt;
    <plc id="2">
       <some other stuff/>
    </plc&gt;
</root&gt;

I have a relatively small number (< 50) of the plc nodes, but the whole 
tree is pretty big.

What is the best (or simplest way) to retrieve the information for plc 2?

xpath is the simplest and most powerful

Should I pull all of the plc elements using xmlXPathEvalExpression, and 
then iterate over them with xmlGetProp looking for an id attribute with 
a value of "2&quot;?

no need to iterate, just create an xpath expression which specifies id=2

off the top of my head the expression would be "/plc[id="2"]&quot;

just google xpath syntax or xpath examples, you'll turn up a ton

libxml2 xpath example C code here: http://xmlsoft.org/examples/index.html#XPath

The xpath spec has examples too: http://www.w3.org/TR/xpath

If you're comfortable with python or another scripting language you can construct a simple test program to exercise your xpath syntax in about 5 minutes.
-- 
John Dennis < jdennisredhat.com">jdennisredhat.com>
How to select a node based on attribute values?
user name
2006-11-29 09:04:47
On Tue, Nov 28, 2006 at 09:35:38PM -0500, John Dennis wrote:
> no need to iterate, just create an xpath expression
which specifies id=2

  Well libxml2 XPath implementation will iterate for you
more precisely.

> off the top of my head the expression would be
"/plc[id="2"]"
> 
> just google xpath syntax or xpath examples, you'll turn
up a ton
> 
> libxml2 xpath example C code here:
> http://x
mlsoft.org/examples/index.html#XPath
> 
> The xpath spec has examples too: http://www.w3.org/TR/xpath

> 
> If you're comfortable with python or another scripting
language you can
> construct a simple test program to exercise your xpath
syntax in about 5
> minutes.

  You can test that even faster using xmllint --shell
test.xml
and using the xpath command directly there

Daniel

-- 
Red Hat Virtualization group http://redhat.com/v
irtualization/
Daniel Veillard      | virtualization library  http://libvirt.org/
veillardredhat.com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ |
Rpmfind RPM search engine  http://rpmfind.net/
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xmlgnome.org
http://mai
l.gnome.org/mailman/listinfo/xml
How to select a node based on attribute values?
user name
2006-11-30 23:11:41
John Dennis wrote:

> On Tue, 2006-11-28 at 16:57 -0800, Yan Seiner wrote:
>
>>Fist off, I'm a total newbie when it comes to
libxml.  I've been able to 
>>work my way through some examples, and produce
working code, but I'm not 
>>entirely happy with the result, and I would like
some advice.
>>
>>I have an XML tree that looks like this:
>>
>><root>
>>    <plc id="1">
>>       <some stuff/>
>>    </plc>
>>    <plc id="2">
>>       <some other stuff/>
>>    </plc>
>></root>
>>
>>I have a relatively small number (< 50) of the
plc nodes, but the whole 
>>tree is pretty big.
>>
>>What is the best (or simplest way) to retrieve the
information for plc 2?
>>    
>>
>  
>
> xpath is the simplest and most powerful
>
>>Should I pull all of the plc elements using
xmlXPathEvalExpression, and 
>>then iterate over them with xmlGetProp looking for
an id attribute with 
>>a value of "2"?
>>    
>>
>
> no need to iterate, just create an xpath expression
which specifies id=2
>
> off the top of my head the expression would be
"/plc[id="2"]"
>
> just google xpath syntax or xpath examples, you'll turn
up a ton

OK, thanks a lot for the above!

I'm working with an embedded platform, so it took me a bit
to get all of 
the required libxml stuff together and rebuild PHP with
libxml support....

Now I have xpath working with PHP.   Yaay!

Ok, now for the PHP + xml question:

I am trying to get the base node of a schedule; I've
attached a small 
portion of my XML file below.  What I would like to do is to
get the 
root of a schedule, and then iterate with foreach through
all of the 
children.  This is the function I came up with and of course
it doesn't 
work.  I don't understand how I am supposed to work with the
object that 
the xpath_eval[_expression] returns.  I've also tried the
child_nodes 
method and that fails as well.

Hopefully there is a PHP expert on the list who can shed
some light on this.

Thanks,

--Yan

    function loadSchedule($pivotID, $schedName, $schedNum) {
    $dom = domxml_open_file("/tmp/test.conf");
    $xpath = xpath_new_context($dom);
    // find base node for our schedule
    $pat = '//root/pivot[id="' . $pivotID .
'"]/'
        . $schedName . '[id="' . $schedNum .
'"]';
    print $pat;
    $baseNode = xpath_eval($xpath,$pat);
    var_dump($baseNode);
    $lines =
xpath_eval_expression($xpath,'/line',$baseNode);
    var_dump($lines);
    print "nOKn";
    foreach($lines->nodeset as $line) {
        var_dump($line);
        }
    }

<root>
    <pivot id="1">
        <water id="1">
            <line id="1">
                <bearingTo>120.0</bearingTo>
                <WetDry>1</WetDry>
                <FwdRev>0</FwdRev>
                <event>0</event>
                <speed>10</speed>
            </line>
            <line id="2">
                <bearingTo>180.0</bearingTo>
                <WetDry>1</WetDry>
                <FwdRev>1</FwdRev>
                <event>0</event>
                <speed>45</speed>
            </line>
        </water>
    <pivot>
</root>
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xmlgnome.org
http://mai
l.gnome.org/mailman/listinfo/xml
How to select a node based on attribute values?
user name
2006-12-01 01:14:49
Yan,

I answered your question off list as it doesn't pertain to
libxml2 but 
rather coding with domxml from PHP 4.

Seeing that there happens to be a good number of PHP based
questions 
coming up, I'd like to make sure people are aware of the
general PHP 
mailing list (php-generallists.php.net). The people
that follow that 
list can probably better handled a lot of these questions.


Rob

Yan Seiner wrote:
>
> I'm working with an embedded platform, so it took me a
bit to get all of 
> the required libxml stuff together and rebuild PHP with
libxml support....
>
> Now I have xpath working with PHP.   Yaay!
>
> Ok, now for the PHP + xml question:
>
> I am trying to get the base node of a schedule; I've
attached a small 
> portion of my XML file below.  What I would like to do
is to get the 
> root of a schedule, and then iterate with foreach
through all of the 
> children.  This is the function I came up with and of
course it doesn't 
> work.  I don't understand how I am supposed to work
with the object that 
> the xpath_eval[_expression] returns.  I've also tried
the child_nodes 
> method and that fails as well.
>
>   
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xmlgnome.org
http://mai
l.gnome.org/mailman/listinfo/xml
[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )