c# - Using XPath to select attributes with wildcards -
i got html need parse, , i'm using c# , html agility pack library selection of nodes. html either:
<input data-translate-atrr-placeholder="forgot_password.form.email">
or :
<h1 data-translate="forgot_password.form.email"></h1>
where data-translate-attr-****
new pattern of attributes need find
i use :
//[contains(@??,'data-translate-attr')]
but unfortunately, search value inside attribute. how attribute itself, wildcard?
update : @mathias muller
htmlagilitypack.htmldocument htmldoc // old code (returns nodes) var nodes = htmldoc.documentnode.selectnodes("//@data-translate"); // these suggestions return no nodes using same data var nodes = htmldoc.documentnode.selectnodes("//@*[contains(name(),'data-translate')]"); var nodes = htmldoc.documentnode.selectnodes("//@*[starts-with(name(),'data-translate')]");
update 2
this appears html agility pack issue more xpath issue, used chrome test xpath expressions , of following worked in chrome not in html agility pack :
//@*[contains(local-name(),'data-translate')] //@*[starts-with(name(),'data-translate')] //attribute::*[starts-with(local-name(.),'data-translate')]
my solution
i ended doing things old fashioned way...
var nodes = htmldoc.documentnode.selectnodes("//@*"); if (nodes != null) { foreach (htmlnode node in nodes) { if (node.hasattributes) { foreach (htmlattribute attr in node.attributes) { if (attr.name.startswith("data-translate")) { // code in here handle translation node } } } } }
use xpath functions contains()
or starts-with()
. need xpath expression like
//@*[contains(name(),'data-translate')]
or perhaps
//@*[starts-with(name(),'data-translate')]
which retrieves attribute nodes. above, @*
attribute wildcard looking for.
Comments
Post a Comment