0

I am working with XML that has a node titled <SCOPE>.
If the Scope is equal to "teaching," the code follows one path; if the Scope is not equal to "teaching," the code follows another path. I would like to also check to see if Scope is equal to either.
Is this possible in using a variable?
Here is my code (below). However, this returns no results, whereas using the separate values (either "Teaching" or "Scholarship/Research" return values).

$strXpathVar = "Teaching|Scholarship/Research"; $xml_awards_honor_scope_all = $xml_award_honor[0]->xpath('a:AWARDHONOR[a:SCOPE="'.$strXpathVar.'"]'); 
1
  • 'a:AWARDHONOR[a:SCOPE="Teaching" or a:SCOPE="Scholarship/Research"]' Commented Jul 27, 2021 at 18:41

1 Answer 1

2

The | allows to combine multiple Xpath expressions. So this is possible.

$scopes = ["Teaching", "Scholarship/Research"]; $xml_awards_honor_scope_all = $xml_award_honor[0]->xpath( 'a:AWARDHONOR[a:SCOPE="'.$scopes[0].'"]|a:AWARDHONOR[a:SCOPE="'.$scopes[1].'"]' ); 

However the conditions support operators like or. Most of the time a better solutition would be like this:

$scopes = ["Teaching", "Scholarship/Research"]; $xml_awards_honor_scope_all = $xml_award_honor[0]->xpath( 'a:AWARDHONOR[a:SCOPE="'.$scopes[0].'" or a:SCOPE="'.$scopes[1].'"]' ); 
Sign up to request clarification or add additional context in comments.

5 Comments

If you don't know how many terms will be in the $scopes array, would it be possible to write a condition that checked each term in the $scopes array?
In other words, would it be possible to write a condition that would compare SCOPE one time to all elements in the array?
Generate the expression in a loop (it is a string), make sure that the words do not include a quote char.
Would I somehow generate the entire expression earlier in the script, or plug in the string into this expression?
You build the expression up as a variable using string operators and functions. Then you use the variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.