XPath to find an element whose attribute contains

2019-02-14 03:37发布

问题:

    <root>
// other nodes
    <a href="/PatternFramework/Pages/LogOut.aspx?np=/sites/novatestsite/Home.aspx" slick-uniqueid="92">Sign out</a>
    </root>

How to write an xpath which returns the a tag whose href contains "logout.aspx"?

For instance something like

//a[@href[contains[., "logout.aspx"]]

回答1:

Case-sensitive:

//a[contains(@href,'logout.aspx')] 

Case-insensitive for XPath 2.0:

//a[contains(lower-case(@href),'logout.aspx')] 

Case-insensitive for XPath 1.0:

//a[contains(translate(@href, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'logout.aspx')] 


标签: xml xslt xpath