【问题标题】:How to check for multiple attributes in XPath?如何检查 XPath 中的多个属性?
【发布时间】:2012-01-23 12:08:28
【问题描述】:

我想在 XHTML 文档中选择样式表,其中不仅包含描述,还包含 href。

例如

<link rel="stylesheet" href="123"/> 

应该被选中,并且

<link rel="stylesheet"/>  

不应该。

目前,我是这样做的:

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

但我怀疑有更好的方法来做到这一点。有吗?

【问题讨论】:

  • 测试的第二部分应该是n.Attributes["href"].Value==null :)

标签: c# xml xpath


【解决方案1】:

在属性表达式中添加and @href

//链接[@rel='stylesheet' 和@href]

这应该允许您完全省略检查:

foreach (XmlNode n in xml.SelectNodes(@"//link[@rel='stylesheet' and @href]"))
{
    var l = Web.RelativeUrlToAbsoluteUrl(stuffLocation, n.Attributes["href"].Value);
}

【讨论】:

    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多