0

I would like to find:

<div style="text-align:center;" >
<div style="text-align: center;" >
<div style="text-align:center" >
<div style="text-align: center" >

So an optional space before center and an optional semicolon at the end.

I can do:

//div[@style=’text-align:center;’ or @style=’text-align: center;’ or @style=’text-align:center’ or @style=’text-align: center’]

But is there a “cleaner” way? And able to take many more optional characters without getting too long?

Vixxs
  • 569
  • 7
  • 21
  • Presumably you're in XPath 1.0, otherwise you would just be using matches()? But if you need an XPath 1.0 solution, *please say so*! – Michael Kay Apr 01 '17 at 07:57
  • @MichaelKay I don't know, Im using PHP 5.6.23 and I plan to upgrade to 7 soon. The only thing I see is: DOM/XML API Version 20031129, libxml Version 2.9.4, HTML Support enabled, XPath Support enabled. – Vixxs Apr 01 '17 at 10:20
  • That's version 1.0. Sadly the XML libraries in PHP haven't kept up with the latest standards. We're trying to correct that by providing Saxon/C with a PHP interface. – Michael Kay Apr 01 '17 at 18:03

1 Answers1

1

You can first remove the optional characters f.e space and semicolon, assuming they aren't used in the required text, using translate() function, and then check whether the result equals only the required text f.e 'text-align:center' :

//div[translate(@style, ' ;', '') = 'text-align:center']

Or, when the pattern gets more complex, you can use regex in your XPath via PHP preg_match :

$xp->query("//div[php:function('preg_match', '~text-align:\s*center;*~', string(@style))]");

See full example demonstrating how to call PHP function from XPath in my older post : Get hrefs that match regex expression using PHP & XPath.

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137