xpath to validate text before and after <br/> - xpath

following is my html table structure and i want to validate the complete text inside td using x-path <tr><td>Sagar Nair<br/><b>Owner</b> - Verified</td></tr>
can anyone help for this.

When the tr element in your example is the current element, then the XPath expression string(.) will have as its value the string you say you would like to validate. For the actual validation of the string you are going to need some language other than XPath; since you don't mention a programming language, however, I assume that once you get the string you know what to do with it.

Related

How to get element value in xpath

I am trying to get an XML element value out of a string but is doesn't work yet.
Normally i only extract the xpath text values but now i want to get the EAN code of the following string:
<div data-retailrocket-markup-block="60c8602d97a528373cb51d77" data-product-id="8710429017146"></div>
Is this possible? And if so how? Thanks a lot!

Xpath to strip text using substring-after

I have the following which is the second span in html with the class of 'ProductListOurRef':
<span class="ProductListOurRef">Product Code: 60076</span>
Ive tried the following Xpath:
(//span[#class="ProductListOurRef"])[2]
But that returns 'Product Code: 60076'. But I need to use Xpath to strip the 'Product Code: ' to just give me the result of '60076'.
I believe 'substring-after' should do it but i dont know how to write it
If you are using XPath 1.0, then the result of an XPath expression must be either a node-set, a single string, a single number, or a single boolean.
As shown in comments on the question, you can write a query using substring-after(), whose result is a string.
However, some applications expect the result of an XPath expression always to be a node-set, and it looks as if you are stuck with such an application. Because you can't construct new nodes in XPath (you can only select nodes that are already present in the input), there is no way around this.

How to write Xpath expressions to distinguish between results?

I am new to xpath expression. Need help on a issue
Consider the following Document :
<tbody><tr>
<td>By <strong>Bec</strong></td>
<td><strong>Great Support</strong></td>
</tr></tbody>
In this I have to find the text inside tags separately.
Following is my xpath expression:
//tbody//td//strong/text();
It evaluates output as expected:
Bec
Great Support
How can I write xpath expressions to distinguish between the results i.e Becand Great Support
It's rather unclear what you're trying to do, but the following should succeed in selecting them separately:
//tbody/tr/td[1]/strong
and
//tbody/tr/td[2]/strong
Note that the text() you had at the end is most likely not needed in this case.
Not sure I understand 100%, but if you're trying to get the text of the first and the second strong tags, you can use position (1 based index)
//tbody/td[position()=1]/strong/text() //first text
//tbody/td[position()=2]/strong/text() //second text
This solution only applies to the current sample though, where your strong tags are inside either the first or second td tag.
Not sure this is what you're looking for... anyway, assuming you're asking to retrieve a node based on its text you can look up for text content by doing something like:
//tbody//td//strong/text()[.="Bec"]
PS
in [.=""] the dot is an alias for text() self::node() (thanks JLRishe for pointing out the mistake).

Invalid Token when using XPath

I am making a modification to a web application using XPath, and when executed I get an error message - Invalid token!
This is basic what I am doing
public xmlNode GetSelection (SelectParams params, xmldocument docment)
{
xpathstring = string.format("Name =\'{0}' Displaytag = \'{1}' Manadatory=\'{2}', params.Name, params.Displaytag, params.Manadatory);
return document.selectsinglenode(xpathstring);
}
As you can see, I am making a string and setting values on the nodes I am trying to find against my xml document, and thus returning xml data that matches my parameters.
What is happening is that I am getting an xpathexeception error in Visual Studio and it says invalid token.
I do know that in the xml document that the parameters I am looking in the tags have double quotes, for example, Name="ABC". So, I thought the problem could be solved using an "\".
Can anyone help?
Update from comments
In the Xml Document, the tag has
attributes where they are set as
Name="ABC" Displaytag="ATag"
Manadatory="true".
I guess you need:
//*[#Name="ABC"][#Displaytag="ATag"][#Manadatory="true"]
Or
//*[#Name="ABC" and #Displaytag="ATag" and #Manadatory="true"]
Meaning: any element in the whole document having a Name attribute with "ABC" value, a Displaytag attribute with "ATag" value and a Manadatory attribute with "true" value.
The string passed as argument to SelectSingleNode() (BTW, the exact capitalization is important) is something like:
Name ='someName' Displaytag = 'someString' Manadatory='true'
This is extremely different than a syntactically legal XPath expression.
And the error message just reflects the fact that toxic food has been given to the XPath engine.
Solution: Do read at least a light XPath tutorial and then specify a correct XPath expression.
The string you are constructing is not a valid XPath expression. In fact, it is nothing like XPath at all.
Indeed, even if it were a valid XPath expression, constructing it this way by string concatenation is a very dangerous practice, because of the possibility of injection attacks. But I suspect that advice will fall on stony ground.

xpath expression to select text from link

I have such content of html file:
<a class="bf" title="Link to book" href="/book/229920/">book name</a>
Help me to construct xpath expression to get link text (book name).
I try to use /a, but expression evaluates without results.
If the context is the entire document you should probably use // instead of /. Also you may (not sure about that) need to get down one more level to retrieve the text.
I think it should look like this
//a/text()
EDIT: As Tomalak pointed out it's text() not text
Have you tried
//a
?
More specific is better:
//a[#class='bf' and starts-with(#href, '/book/')]
Note that this selects the <a> element. In your host environment it's easy to extract the text value of that node via standard DOM methods (like the .textContent property).
To select the actual text node, see the other answers in this thread.
It depends also on the rest of your document. If you use // in the beginning all the matching nodes will be returned, which might be too many results in case you have other links in your document.
Apart from that a possible xpath expression is //a/text().
The /a you tried only returns the a-tag itself, if it is the root element. To get the link text you need to append the /text() part.

Resources