So I have the following XML file where I’m only interested in the <respcondition> elements where there is only the <or> element in and not the <and> element.
I tried in PHP the following statement:
<!-- language: lang-php -->
foreach ($item->resprocessing->children() as $child) {
if ((string) $child->getName() == 'respcondition') {
if ((string) $child->respcondition->children()->getName() == 'conditionvar') {
if ((string) $child->respcondition->conditionvar->children()->getName() == 'or') {
$responseID = $child->respcondition->conditionvar->or->varequal->attributes()->respident;
//some code
}
}
}
}
}
But I only come in the first IF, so I figured out that children() doesn’t contain the entire XML from that element. Are there some alternatives I can use?
<!-- language: lang-xml -->
<resprocessing>
<outcomes>
<decvar varname="SCORE" vartype="Decimal" defaultval="0" minvalue="0.0" maxvalue="1.0" cutvalue="1.0"/>
</outcomes>
<respcondition continue="Yes" title="Mastery">
<conditionvar>
<or>
<varequal respident="1000006631" case="Yes"><![CDATA[Mars]]></varequal>
</or>
</conditionvar>
<setvar varname="SCORE" action="Add">1.0</setvar>
</respcondition>
<respcondition continue="Yes" title="Mastery">
<conditionvar>
<or>
<varequal respident="1000006678" case="Yes"><![CDATA[Jupiter]]></varequal>
</or>
</conditionvar>
<setvar varname="SCORE" action="Add">1.0</setvar>
</respcondition>
<respcondition title="Mastery" continue="Yes">
<conditionvar>
<and>
<or>
<varequal respident="1000006421" case="Yes"><![CDATA[planets]]></varequal>
</or>
<or>
<varequal respident="1000006468" case="Yes"><![CDATA[sun]]></varequal>
</or>
<or>
<varequal respident="1000006558" case="Yes"><![CDATA[Mercury]]></varequal>
</or>
</and>
</conditionvar>
<displayfeedback feedbacktype="Response" linkrefid="Mastery"/>
</respcondition>
</resprocessing>
Kind regards
Have a look at extracting it via xpath;
The following will return you all the respcondition nodes, having a conditionvar node, with only “or” childnodes in it. In your xml example, this will not return anything, which i believe you’re aiming at.
Or, if you would like to extract the respcondition nodes, but now baring the “and” childnode in mind, use;
xpath is an easy and powerful way of navigating and extracting xml document, use it if possible.