I'd like to select stylesheets in an XHTML document, which contain not only description, but also href.
For example
<link rel="stylesheet" href="123"/>
should be selected, and
<link rel="stylesheet"/>
should not.
At present, I'm doing it like this:
foreach (XmlNode n in xml.SelectNodes(@"//link[@rel='stylesheet']"))
{
if (n.Attributes["href"]==null||n.Attributes[""].Value==null)
{
continue;
}
var l = Web.RelativeUrlToAbsoluteUrl(stuffLocation, n.Attributes["href"].Value);
}
but I suspect there's a much better way of doing this. Is there?
If an xpath refers multiple elements on the DOM, It should be surrounded by brackets first () and then use numbering. if the xpath refers 4 elements in DOM and you need 3rd element then working xpath is (common xpath)[3].
You can locate a web element by using its multiple attribute using "and" and "or".
Use following or preceding Advanced Xpath to combine the two xpath.
Add and @href
to the attribute expression:
//link[@rel='stylesheet' and @href]
This should allow you to omit the check altogether:
foreach (XmlNode n in xml.SelectNodes(@"//link[@rel='stylesheet' and @href]"))
{
var l = Web.RelativeUrlToAbsoluteUrl(stuffLocation, n.Attributes["href"].Value);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With