Note that there are some explanatory texts on larger screens.

plurals
  1. POxpath - finding text within a node - matching the whole word
    text
    copied!<p>Here's an example of the XML file output - obviously scaled down and some information changed:</p> <pre><code>&lt;directory&gt; &lt;company&gt; &lt;id&gt;5002&lt;/id&gt; &lt;company_name&gt;Clothing Store&lt;/company_name&gt; &lt;address&gt;123 street&lt;/address&gt; &lt;latitude&gt;51.123456&lt;/latitude&gt; &lt;longitude&gt;-113.12345432&lt;/longitude&gt; &lt;phone&gt;1234567890&lt;/phone&gt; &lt;fax/&gt; &lt;website/&gt; &lt;logo_url/&gt; &lt;categories&gt; &lt;category&gt; &lt;name&gt;Retail Fashion&lt;/name&gt; &lt;sub_categories&gt; &lt;category_sub&gt; &lt;id&gt;5056&lt;/id&gt; &lt;name&gt;Her Style / Ladies Wear&lt;/name&gt; &lt;/category_sub&gt; &lt;/sub_categories&gt; &lt;/category&gt; &lt;/categories&gt; &lt;/company&gt; &lt;company&gt; &lt;id&gt;5003&lt;/id&gt; &lt;company_name&gt;Hardware Store&lt;/company_name&gt; &lt;address&gt;123 street&lt;/address&gt; &lt;latitude&gt;51.123456&lt;/latitude&gt; &lt;longitude&gt;-113.12345432&lt;/longitude&gt; &lt;phone&gt;1234567890&lt;/phone&gt; &lt;fax/&gt; &lt;website/&gt; &lt;logo_url/&gt; &lt;categories&gt; &lt;category&gt; &lt;name&gt;Retail&lt;/name&gt; &lt;sub_categories&gt; &lt;category_sub&gt; &lt;id&gt;5001&lt;/id&gt; &lt;name&gt;Hardware&lt;/name&gt; &lt;/category_sub&gt; &lt;/sub_categories&gt; &lt;/category&gt; &lt;/categories&gt; &lt;/company&gt; &lt;company&gt;...&lt;/company&gt; &lt;/directory&gt; </code></pre> <p>So, here's the issue. I've got an xml file for a business directory. I need to do text searches on it based on categories and pull only the businesses that have those categories within their <code>&lt;company&gt;</code> node. So, let's say I search for "Retail", I would need any business that has "Retail" as a category, but I need to return all the child nodes within the <code>&lt;company&gt;</code> node - so, everything from <code>&lt;id&gt;</code> to <code>&lt;categories&gt;</code></p> <p>I actually have everything working properly, except when more than one category share a specific word. So, the current example I have right now is "Retail" - there is a category for "Retail" and "Retail Fashion" - the way I'm doing my xpath it's pulling in all the businesses from both categories because it's not looking for the whole word, or doing any regex searches. I have a feeling that I need to use <code>matches</code> but have yet to be successful in implementing it correctly. I'm a total xpath noob - I'm sure this is an easy answer, but I can't find a good example for what I'm trying to do anywhere - or at least one that works for me.</p> <p>Here's what I'm doing for the xpath:</p> <pre><code>$results = $xml-&gt;xpath("//company[contains(categories/*,'Retail')]"); </code></pre> <p>Like I said, this returns everything as it should, except it's including both "Retail" and "Retail Fashion" categories.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload