使用XPath的SelectNodes忽略个案(SelectNodes with XPath ignoring cases)

系统教程 行业动态 更新时间:2024-06-14 17:01:31
使用XPath的SelectNodes忽略个案(SelectNodes with XPath ignoring cases)

在XPath中找到包含特定字符串而忽略字符外框的元素时遇到问题。

我想在HTML页面中找到所有带有id的节点都包含文本“footer”,忽略它以大写或小写写入。

在我的例子中,我有一个不同的html文本,如下所示:

<div id="footer">some text</div> <div id="anotherfooter">some text</div> <div id="AnotherFooter">some text</div> <div id="AnotherFooterAgain">some text</div>

我需要使用XPath选择所有节点(或任何组合中的“footer”)。

目前我正在使用此xpath,但不适用于UpperCase ID

"//*[contains(./@id, 'footer')]/@id"

我用translate()做了几个测试,但是没有按我的预期工作。

任何想法?

编辑:我使用HtmlAgilityPack与XPath 1.0版本的作品。

I have a problem finding elements in XPath that's contains a certain string ignoring character casing.

I want to find in a HTML page all the nodes with id contains the text "footer" ignoring it's write in uppercase or lowercase.

In my example I have a different html text like this:

<div id="footer">some text</div> <div id="anotherfooter">some text</div> <div id="AnotherFooter">some text</div> <div id="AnotherFooterAgain">some text</div>

I need to select all nodes (or any combination in any case with the word "footer" in the id) with a XPath.

Currently I'm using this xpath but doesn't work for the UpperCase id's

"//*[contains(./@id, 'footer')]/@id"

I've done several tests with translate() but doesn't work as I expected.

Any idea?

EDIT: I'm using HtmlAgilityPack with works with the XPath 1.0 version.

最满意答案

不知道你是否已经尝试过这种方式,但这是我为区分大小写而包含搜索的做法:

//*[contains(translate(./@id,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'), 'footer')]/@id

我看到你找到了你的解决方案,所以我发布了这个答案,以防其他人有同样的问题。

Not sure if you've tried this yet, but this is what I do for case sensitive contains searches:

//*[contains(translate(./@id,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'), 'footer')]/@id

I saw you have found your solution, so I'm posting this answer in case others have the same issue.

更多推荐

本文发布于:2023-04-20 16:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4e839aa4ad9e6993cff5aa8e81090d3e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个案   SelectNodes   XPath   ignoring   cases

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!