tags:

views:

204

answers:

3

I don't know why following query doesn't work:

//a/@href[@class='specified_string']
+2  A: 

Try it the other way round:

//a[@class='specified_string']/@href

After all, class is an attribute of the <a> element, not an attribute of the href attribute.

ndim
Yes I think it works, but the problem is that query returns attributes, and I want to retrieve attributes' values. I'm writing it in Java and it returned me NodeList of length 2. When I try to print it out, it just prints two xml headers:<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>two and there is only two links that query will match, so it seems working. But I want to get values of this hrefs. How to do that?
l245c4l
That `NodeList` should contain the list of `Node`s representing the `href` attributes. So just running `getNodeValue()` on those nodes should give you the `href` attributes' values.
ndim
+1  A: 

You basically say that you are looking for an attribute named href, whose attribute (this is the error) class should be equal to specified_string.

But you need to find the attribute href of an element a, whose attribute class is specified_string.

(ndim's answer overlapped mine)

Patrick
A: 

An attribute cannot have attributes. Only elements can have attributes.

The original XPath expression:

//a/@href[@class='specified_string'] 

selects any href attribute of any a element, such that the href attribute has an attribute class whose value is 'specified_string'.

What you want is:

//a[@class='specified_string']/@href 

that is: the href attribute of any a element that has class atribute with value 'specified_string'.

Dimitre Novatchev