Jump to content

PHP xpath query - xml


prometheos

Recommended Posts

hey,

im having trouble with this dom xpath query....

heres the layout of my xml

<items>
   <item>
        <itemName>
        </itemName>
        <requested>
             <by>
             </by>
             <confirmed>
             </confirmed>
        </requested>
   </item>
   <item>
   </item>
    .
    .
    .
</items>

 

i want it so depending if the item name is a certain thing, to query the 'requested/by' field of that same item? can any1 help me?

heres the code i have

$dom = new DOMDocument();							
	$dom->load($forfilename);
	$xpath = new DOMXPath($dom);
	$a = $xpath->query('item/[itemName='.$itemN.']/../requested/by');

 

any ideas welcome,

thanks in advance:)

Link to comment
https://forums.phpfreaks.com/topic/197009-php-xpath-query-xml/
Share on other sites

Your XML is wrong

 

<item name="whatever">
    <request by="someone" confirmed="true"/>
</item>

 

Then you can use:

 

/item[name="whatever"]

 

Now you have to query all by:

 

/item/request/by | //by

 

And go over each element, retrieve it's value and check if it equals

Link to comment
https://forums.phpfreaks.com/topic/197009-php-xpath-query-xml/#findComment-1034292
Share on other sites

prometheos, your XPath query is problematic.  One which should retrieve the elements that you are looking for is:

 

/items/item[itemName=...]/requested/by

 

So in PHP, something like:

 

$nodes = $xpath->query('/items/item[itemName="'.$itemN.'"]/requested/by');

 

 

P.S. There is nothing wrong with the structure of the XML, to my eye at least.

Link to comment
https://forums.phpfreaks.com/topic/197009-php-xpath-query-xml/#findComment-1034313
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.