Jump to content
Sign in to follow this  
knutsford

$xml->xpath

Recommended Posts

I am trying to do a search to get a url in PHP

I have tried

 

 

        $the_id = $item['id'];
        $nodes = $xml->xpath("//item[contains(g:id,'$the_id')]");
        foreach($nodes as $node) {
            $url = $node->g:link;
        }

 

How do I do it?

$url = $node->g:link;  gives a syntax error

 

Do I ignore g: and use id and link

Thanks

 

 

The xml layout is

 

 

<item>
<g:id>2023</g:id>
<g:title> Croquet Set - Townsend </g:title>
<g:description>
Already a very popular set, the Townsend croquet set is now available in a more portable canvas carry bag. Perfect for people who wish enjoy croquet on the go. The Garden Games Townsend Croquet set has four beautifully crafted croquet mallets with attractive Rosewood mallet heads and solid Ash wood handles with leather bound grip for comfort, less vibration when hitting the solid 16oz composite regulation sized balls giving better accuracy.

This high quality croquet set is easy to store in the strong and durable canvas carry bag. The set easily packs back into its storage bag ready for the next game. 
</g:description>
<g:link>
</g:link>
<g:image_link>
https://www.biggamehunters.co.uk/acatalog/Townsend-in-a-bag_1.jpg
</g:image_link>
<g:condition>New</g:condition>
<g:availability>in stock</g:availability>
<g:price>164.99 GBP</g:price>
<!--
<g:shipping>
                <g:country></g:country>
                <g:service></g:service>
                <g:price> </g:price>
            </g:shipping>
            <g:shipping_weight> kg</g:shipping_weight>
-->
<g:gtin>5035874202309</g:gtin>
<g:brand>Garden Games</g:brand>
<!--
 The following attributes are required for apparel and accessories, media and software; recommended otherwise
-->
<g:google_product_category>Sporting Goods > Outdoor Recreation > Lawn Games</g:google_product_category>
<g:product_type>Croquet > Croquet Sets</g:product_type>
</item>

 

Share this post


Link to post
Share on other sites
<rss version="2.0">
<channel>
<title>Big Game Hunters Online Store</title>
<link>https://www.biggamehunters.co.uk/</link>
<description>Products available from Big Game Hunters</description>
<item>
<g:id>88205</g:id>
<g:title>
Bee Ball Basketball Stand - BB-05 (1.6m to 2.1m)
</g:title>
<g:description>
The BB-05 Stand by Bee Ball has a large rectangular backboard measuring 80cm x 58cm. This is similar to the shape to that of professional units and therefore offers better shooting practise than curved backboards. Depending on the age and height of your children you can adjust the ring between 1.6m and 2.1m. 

You can expect durability from the stand with a 16mm thick hoop which is 38cm wide. This is paired with an upgraded UV resistant net designed to last through all the seasons.

Stability is also an important consideration and the large base when weighed down will ensure the stand stays upright during play. The wheels at the front of the base make it easy to move the stand should you need to play in a different spot in the garden.
</g:description>
<g:link>
https://www.biggamehunters.co.uk/acatalog/BeeBall-Junior-Basketball-Stand.html
</g:link>
<g:image_link>
https://www.biggamehunters.co.uk/acatalog/Bee-Ball BB-05(1).jpg
</g:image_link>
<g:condition>New</g:condition>
<g:availability>in stock</g:availability>
<g:price>59.99 GBP</g:price>
<!--
<g:shipping>
                <g:country></g:country>
                <g:service></g:service>
                <g:price> </g:price>
            </g:shipping>
            <g:shipping_weight> kg</g:shipping_weight>
-->
<g:gtin>5035874881009</g:gtin>
<g:brand>Bee Ball</g:brand>
<!--
 The following attributes are required for apparel and accessories, media and software; recommended otherwise
-->
<g:google_product_category>
Sporting Goods > Athletics > Basketball > Basketball Hoops
</g:google_product_category>
<g:product_type>Basketball > Basketball Stands</g:product_type>
</item>

</channel>

</rss>

 

Is a full example with just one item

 

Edited by knutsford

Share this post


Link to post
Share on other sites

Where was $xml initialized? I can't say where the error is if I don't know what library you're using to read the XML.

Share this post


Link to post
Share on other sites

It is ok I have now managed it.

I needed

 

 

 $nodes = $xml->xpath("//item//g:id[contains(text(),'$code')]/following-sibling::g:link");
        
        $field = "g:link";
        $url = "";
        foreach($nodes as $node) {            
            $url = (string)$node[0];    
        }

It is ages since I have done anything with xml so I was rather rusty

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...