0

I am trying to parse a html file/strings for two things using php and xpath.

<DIV STYLE="top:110px; left:1280px; width:88px" Class="S0">Aug30</DIV>

I tried to look for an unknown value (here: Aug30) with knowing the style top and left value (here: 110px and 1280px).

And the other way. I know the value Aug30 but want to get its values of top and left.

Perhaps XPATH is not the best way to do this. Any idea on how to solve my problem?

Thanks in advance for your help!

1
  • Both with the contains() function, the first on the attribute, and fetch the string content of that node, the second one contains() on the string content, and just return the whole attribute & parse out the left & top with PHP, trying to do that with XPath is difficult and cumbersome at best, it there's a reasonable way at all. Commented Aug 14, 2014 at 21:58

1 Answer 1

2

To filter <div> element by style attribute value in XPath you can do something like this :

//div[contains(@style, 'top:110px') and contains(@style, 'left:1280px')]

Above XPath will search for <div> node having style attribute value contains two specific strings.

The other requirement isn't supported in XPath 1.0 as far as I can see. We can get the entire value of style attribute, but getting part of it is a dead end. There are some string functions we can use, even though returning a function's result isn't supported.

You'll need to do that using XPath 2.0 or using the host programming language (PHP in this case).

Sign up to request clarification or add additional context in comments.

5 Comments

What do you mean by returning a function's result isn't supported? This is of course supported, e.g. take fn:concat(...) and it will return a xs:string and is thereby a functions result, which can be returned.
hmm.. I meant something like //div[x='Aug30']/substring(...). You're right in that case, given only single node passed to the function
I still don't get it. Functions as path step are not supported by XPath 1.0, but you could easily do substring(//div[x='Aug30'], 1, 1). Also, you are not restricted to a single node, of course you can pass multiple nodes to a function if the function signature requires it. I really don't get what you are trying to say and it seems blatantly wrong to me.
Thank you. Sometimes it can be that easy. And I tried a lot more complex workarounds.
@dirkk Sorry if that isn't clear for you, making up a good English sentence is a hard work for me :) I meant if you have multiple nodes matched by //div[x='Aug30'], substring() will only operate on the first matched node and may cause confusion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.