Note that there are some explanatory texts on larger screens.

plurals
  1. POphp xpath select nodes based on values of two attributes (edit) AND its parents attributes
    text
    copied!<p>xpath 1.0 - I had to change the approach, because the XML has changed. </p> <p>I need to pull the </p> <pre><code>t-Attribute FROM the &lt;d&gt;-node WHERE its raw-Attribute = 10 AND its parent-node's &lt;scale&gt; gender-Attribute = "*" OR "m" AND the age-Attribute = "*" OR "39-59" &lt;scales&gt; &lt;scale id="1" gender="*" age="*"&gt; &lt;d scid="hi" raw="10" t="76" /&gt; &lt;d scid="pn" raw="12" t="80" /&gt; &lt;/scale&gt; &lt;scale id="2" gender="m" age="*"&gt; &lt;d scid="hi" raw="8" t="79" /&gt; &lt;d scid="pn" raw="2" t="50" /&gt; &lt;/scale&gt; &lt;scale id="3" gender="*" age="19-39"&gt; &lt;d scid="hi" raw="0" t="48" /&gt; &lt;d scid="pn" raw="10" t="49" /&gt; &lt;/scale&gt; &lt;/scales&gt; </code></pre> <p>For my original objective, Passerby's solution worked like a charm, but I can't figure out how to solve the additional task... </p> <pre><code>$result=$xml-&gt;xpath('//scale[@gender="*" or @gender="m"][@age="*" or @age="39-59"]'); </code></pre> <p>Here's my original task...</p> <p>I want to select nodes from xml where two attributes hold specific values. I am using simplexml, so the xpath has to be 1.0.</p> <p>XML snippet:</p> <pre><code>&lt;scales&gt; &lt;scale id="1" gender="*" age="*"&gt; &lt;d&gt;5&lt;/d&gt; &lt;d&gt;9&lt;/d&gt; &lt;/scale&gt; &lt;scale id="2" gender="m" age="*"&gt; &lt;d&gt;3&lt;/d&gt; &lt;d&gt;0&lt;/d&gt; &lt;/scale&gt; &lt;scale id="3" gender="*" age="19-39"&gt; &lt;d&gt;12&lt;/d&gt; &lt;d&gt;19&lt;/d&gt; &lt;/scale&gt; &lt;/scales&gt; </code></pre> <p>Now, I want to select all <code>&lt;scale&gt;</code> that have... </p> <pre><code>(gender="*" OR gender="m") AND (age="*" OR age="39-59") </code></pre> <p>in my example the ones with id=1 and id=2</p> <p>I know how to do it with 1 attribute, but without the OR / AND conditions...</p> <pre><code>$scales=$xml-&gt;xpath("//scale[@gender='m']"); </code></pre>
 

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