简体   繁体   English

如何在XPath中检查多个属性?

[英]How to check for multiple attributes in XPath?

I'd like to select stylesheets in an XHTML document, which contain not only description, but also href. 我想在XHTML文档中选择样式表,它不仅包含描述,还包含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? 在那儿?

Add and @href to the attribute expression: and @href添加到属性表达式:

//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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM