0

So I have this:

<Presentation>
  <Info attr1="red" attr2="blah"/>
  <Info attr1="big" attr2="iasjd"/>
  <Info attr1="funky" attr2="asf"/>
  ...
  <Info attr1="damn" attr2"..."/>
</Presentation>

How can I check if a node exists, that has value of attr1 as funky, i.e.? I can do that by checking if the returned string in my code is empty, or by using count Xpath function to check if it returns 0, but is there some other function (or way) to check if that certain node with that certain attribute exists. Some more elegant way?

Example: As I can have the case:

<Presentation>
  <Info attr1="red" attr2="blah"/>
  <Info attr1="big" attr2="iasjd"/>
  ...
  <Info attr1="damn" attr2"..."/>
</Presentation>

And as I don't have the node with attribute value funky - I do some other stuff in my code.

UPDATE:

I am doing that in PHP. I need to check what is the value of this attr1 as string. So first I want to check if there is an Info node, where the attr1 value is exactly "funky". If yes - then do some logic in PHP. If not - then do some other stuff (in PHP). I am using the function xpath() on a SimpleXMLElement object, so I get the result as an array. The xpath() function is made so if the XPath expression doesn't work, a boolean(false) is returned, instead of an array. But that is not a safe enough check in my case.

Syspect
  • 921
  • 7
  • 22
  • 50
  • It depends on how you're evaluating the XPath expression, and in particular what data type you're treating the result as. If you have an expression that returns a node set and you evaluate it as a boolean, then you get `false` if the set is empty and `true` if it is not empty. What programming language/library/application are you using to evaluate your XPaths? – Ian Roberts Oct 28 '13 at 10:26
  • @IanRoberts - Good point! I updated my question. Check it out. :) – Syspect Oct 28 '13 at 11:07
  • Please specify *more elegant* in your question, so far you only have provided sample XML chunks but no PHP code. The elegance is normally with the PHP code and more elegant than nothing is actually nothing (or everything), so the question should answer itself in it's current form. What you *might* be looking for is type-juggling, e.g. array and booleans: http://php.net/language.types.type-juggling – hakre Oct 30 '13 at 07:52

2 Answers2

1

Don't know where you are using XPath. But within XPath itself it is possible to test if a node exists. If you are using XSLT you always need to do a test for example with <xsl:if>, see next answer:

XPath will always return the node to find or an empty node.

To get the node for the XML in your question, the following XPath is needed:

/Presentation/Info[@attr1 = 'funky']
Community
  • 1
  • 1
Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66
  • Check the update of my question. :) And also, I don't want to make my code more complicated, by using XSLT. – Syspect Oct 28 '13 at 11:11
  • @Syspect: That is just exemplary, instead of an `` you can naturally use a PHP if: http://php.net/if - so please do not make things more complicated than it needs to be by reading an answer non-exemplary. – hakre Oct 30 '13 at 08:35
1

The xpath() function is made so if the XPath expression doesn't work, a boolean(false) is returned, instead of an array. But that is not a safe enough check in my case.

You can use an "elvis" operator (x ?: y, equivalent to x ? x : y) in PHP 5.3 or later:

$xml->xpath("/Presentation/Info[@attr1 = 'funky']") ?: array()

to ensure that you get an array in all cases (non-empty if the expression matches something, empty if it doesn't).

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183