Finding elements by attribute

18 views
Skip to first unread message

David C

unread,
Jun 26, 2013, 5:59:41 AM6/26/13
to support-...@googlegroups.com

Hi there

I've only just found this support group, but having posted something on stackoverflow yesterday, I thought I would post it here too as obviously this community is far more closely involved with QueryPath!

Apologies for cross-posting, but I'd flummoxed and I need to get an answer.


I'm fairly new to QueryPath but so far it seems great. However I've gotten stuck on trying to find an element which has a particular attribute. I'm looping over some data and enriching an XML document. I've already set up a structure successfully, an extract of which looks like this:

 <countries>
    <country iso2="AF">
      <name>AFGHANISTAN</name>
      <iso2>AF</iso2>
    </country>
    <country iso2="AX">
      <name>&#xC5;LAND ISLANDS</name>
      <iso2>AX</iso2>
    </country>
...

Now I'm trying to add more data to these elements, using the following: ($xml is my main QueryPath object to which I'm trying to add more data)

foreach ($countryIban as $iban){
    $stuffToAdd = qp('<?xml version="1.0"?><iban><structure></structure><key></key></iban>');
    $stuffToAdd->find('structure')->text($iban['structure']);
    $stuffToAdd->find('key')->text($iban['key']);
    $xml->top()->find('country[iso2="'.$iban['country_code'].'"]');
    $xml->append($stuffToAdd);
}

This is not working!
I've tried both find and xpath ways of searching.

$xml->top()->xpath('//*[@iso2="'.$iban['country_code'].'"]');

I've also tried a wildcard search for any element with that attribute, specifying a more specific CSS heirarchy, adding a ->children() after in case I was appending to the wrong place, searching from ->top() or as :root, and so on. As far as I know this xpath query is valid as I've checked it withxpathtester.com and it works there. What ends up happening is there is a whole bunch of iban entities added AFTER all the country entities... what I was aiming to do was identify each individual country entity by its iso2 attribute and then add an iban element below it, with associated structure and key elements below that. What I end up with is this:

    ...
      <country iso2="ZW">
      <name>ZIMBABWE</name>
      <iso2>ZW</iso2>
    </country>
  </countries>
  <iban>
    <structure>ADkk BBBB SSSS CCCC CCCC CCCC</structure>
    <key>B = National bank code&lt;br&gt;S = branch code&lt;br&gt;C = account No.</key>
  </iban>

I've tried lots... and lots... of variations on this theme. I'm sure I'm doing something amazingly shortsighted and I've not spotted this yet. Any tips would be greatly appreciated! Cheers

TechnoSophos

unread,
Jun 26, 2013, 11:27:29 AM6/26/13
to support-...@googlegroups.com
Which version of QueryPath are you using? If you're on version 2, you might want to switch find() to branch().


--
You received this message because you are subscribed to the Google Groups "support-querypath" group.
To unsubscribe from this group and stop receiving emails from it, send an email to support-queryp...@googlegroups.com.
To post to this group, send email to support-...@googlegroups.com.
Visit this group at http://groups.google.com/group/support-querypath.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
http://technosophos.com
http://querypath.org
Reply all
Reply to author
Forward
0 new messages