Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

c# - SelectNodes with XPath ignoring cases

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...