5

Let's say I have the below XML

<root>
   <element class="Page" style="background: url(/images/RlEguQY3_ghsdr.png?1324483033) repeat left top;" />
   <element class="User" />
   <element class="Image" src="/images/bg.png" />
</root>

I am looking for a xpath expression which 1) matches all elements that have /images in the style attribute and 2) matches all Image elements that have /images in the src attribute

Any help greatly appreciated

Thanks

2
  • What does the XPath expression you are already using look like? What unexpected results are you seeing when you try to use the XPath expression you have devised yourself? Commented Dec 21, 2011 at 16:19
  • The truth is, that I have not anything Commented Dec 21, 2011 at 16:20

3 Answers 3

10
 //element[contains(@style, '/images') or (@class='Image' and contains(@src, '/images'))]

(or something similar) should do it.

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

Comments

1

I am looking for a xpath expression which 1) matches all elements that have /images in the style attribute

Directly translated to XPath:

/*/element[contains(@style, '/images')]

. . .

...

and 2) matches all Image elements that have /images in the src attribute

There are no Image elements in the provided XML document. Probably you meant: element elements with class attribute with value "Image":

/*/element[@class='Image'][contains(@src, '/images')]

Combined together, the two XPath expressions above give this:

/*/element
    [contains(@style, '/images')
   or
    @class='Image' and contains(@src, '/images')
    ]

It seems to me that most probable for the second requirement above you actually want the value of the src attribute to actually start with "/images". If so, the more precise XPath expression is:

/*/element
    [contains(@style, '/images')
   or
    @class='Image' and starts-with(@src, '/images')
    ]

1 Comment

Thanks Dimitre. Very nice description
0

You need to use predicates, your predicate expressions will use the contains function. The union operator might also come in handy.

Comments

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.