2012-01-23 15 views
7

Me gustaría seleccionar hojas de estilo en un documento XHTML, que contienen no solo descripción, sino también href.¿Cómo verificar los atributos múltiples en XPath?

Por ejemplo

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

debe ser seleccionado, y

<link rel="stylesheet"/> 

no debería.

En la actualidad, lo estoy haciendo de esta manera:

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

pero sospecho que hay una mejor manera de hacer esto. ¿Esta ahí?

+0

Segunda parte de la prueba debe leer 'n.Attributes [ "href"] .Value == null' :) –

Respuesta

7

Añadir and @href a la expresión de atributo:

 
//link[@rel='stylesheet' and @href] 

Esto debería permitirle omitir la comprobación del todo:

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