943,925 Members | Top Members by Rank

Ad:
Aug 12th, 2009
-1

Basic xpath question..

Expand Post »
I'm new to xpath.. probably very simple, but I haven't been able to figure it out. I'm working with the following xml structure:

<item dateExpires="0" doNotPublish="0" manufacturer="" productDetailURL="" id="BKIT1009"  type="2">
    <pricing price="29.99" retailMarkup="0" retailPrice="0" saleMarkDown="0" salePrice="0" saleType="0" stockPrice="29.99" taxableNational="1" taxableRegional="1"/>
    </availability>
    <accessoryGroup>misc</accessoryGroup>
    <name>2 Piece Kit</name>
    <briefDescription>2 Piece Kit</briefDescription>
    <description>2 Piece Kit</description>
    <invoiceDescription>2 Piece Kit</invoiceDescription>
    <moreDetailLink></moreDetailLink>
    <downloadStorage></downloadStorage>
  </item>

Now, I accessed this item from a list of items with xpath using the unique id and the nodeValue for <name> and <accessoryGroup>:

		   $feed = new DomDocument;
		   $feed->load('telusAccessories.xml');
		   $xpath = new DOMXPath($feed);
		  
		   if (count($accessories) > 1)
		   {
		   	  print "<strong>Accessories:</strong><br />";
			  foreach( $accessories as $ID )
			  {    
			   $includes = $xpath->query("/accessories/item[@sku='".$ID."']/accessoryGroup");
			   if (!($includes->item(0)->nodeValue)=="")
					print $includes->item(0)->nodeValue.": ";
			   $includes = $xpath->query("/accessories/item[@sku='".$ID."']/name");  
				if (!($includes->item(0)->nodeValue)=="")
					print $includes->item(0)->nodeValue."<br />";
			  }
	        }
			else
			{
				print "No accessories for selected item.";	
			}

The above code works fine, but when I try to get the attribute "price" from the child <pricing...>, I have a problem. I know it's probably something very simple, given the simplicity of xpath... but I'm retarded and i don't feel like ripping any more hair out. Below is the code that's not working:

$includes = $xpath->query("/accessories/item[@sku='".$ID."']/pricing[@price]");
print $includes->item(0)->nodeValue;

Thanks in advance,

Yitzhak
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yitzhak is offline Offline
2 posts
since Aug 2009
Aug 20th, 2009
0

Re: Basic xpath question..

Solved. My syntax was wrong.. but very close.

$includes = $xpath->query("/accessories/item[@sku='".$ID."']/pricing/@price");
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yitzhak is offline Offline
2 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: simple xpath question
Next Thread in XML, XSLT and XPATH Forum Timeline: Extracting xml with php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC