You will need XPath 2.0.
For calculating the sum of the string lengths, you will need either
- need a concatenated string of all
@href
s to apply to string-lenght($string as xs:string)
(which only allows a single string as parameter), but concat(...)
only takes an arbitrary number of atomar strings, not a sequence of those; or
- apply
string-length(...)
on every @href as @Navin Rawat proposed - but using arbitrary functions in axis steps is a new feature of XPath 2.0.
If using XPath 2.0, there are functions avg(...)
and ends-with(...)
which help you in stripping down the expression to
avg(//a/@href[ends-with(., '.pdf')]/string-length())
If you have to stick with XPath 1.0, all you can do is using my expression below to fetch the URLs and calculate the average outside XPath.
Anyway, the subexpression you proposed will fail at URLs like http://example.net/myfile.pdf.txt
. Only compare the end of the URL:
//a[@href[substring(., string-length(.) - 3) = '.pdf']]/@href
And you missed a path step for the attribute, so you've been trying to average the string length of the link names right now.