从 XML 中选择节点,在其属性之一中包含特定字符串 [英] select Nodes from an XML, that contains certain string in one of its attributes

查看:36
本文介绍了从 XML 中选择节点,在其属性之一中包含特定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图选择在其属性之一中包含给定字符串的节点,但似乎我只能在某个属性上执行此操作.

Im trying to select nodes that contain a given string in one of its attributes, but it seems that I can only do it on a certain attribute.

var tempUsers = xmlDocument.selectNodes("//Users/*[contains(@Id,'TEXT')]");

我想我可以写其他东西来检查节点的所有属性而不是 Id,而不是 @Id.

I guess that instead of @Id I can write something else to check all the attributes of a node and not only the Id.

谢谢.

推荐答案

你可以在 xpath 中使用 @* 来选择所有属性,但是很幼稚

You can use @* in xpath to select all attributes, but the naive

//Users/*[contains(@*,'TEXT')]

不会做你期望的事情.contains 函数期望它的参数是字符串,所以如果你给它一个节点集,它会首先将节点集转换为一个字符串(通过获取 first 节点),然后在函数中使用该值.相反,你需要说

will not do what you expect. The contains function expects its arguments to be strings, so if you give it a node set instead it will first convert the node set to a string (by taking the string value of the first node in the set) and then use that value in the function. Instead you need to say

//Users/*[@*[contains(.,'TEXT')]]

这将查找文档中的所有 Users 元素,然后选择它们的所有子元素,这些子元素的值包含子字符串 TEXT 的任何属性.

which will find all Users elements in the document and then select all their child elements that have any attribute whose value contains the substring TEXT.

这篇关于从 XML 中选择节点,在其属性之一中包含特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆