STWilson
STWilson

Reputation: 1718

HtmlAgilityPack Wildcard Search in Powershell

How could I shorten the following?

$contactsBlock is an HTMLAgilityPack node, XPath: /html[1]/body[1]/div[3]/div[2]/div[2]/div[1]/div[1]/div[2]/div[1]/div[3]/div[5]/div[1]/div[2]

$contactsBlock.SelectSingleNode(".//table").SelectSingleNode(".//table")

Results in desired XPath: /html[1]/body[1]/div[3]/div[2]/div[2]/div[1]/div[1]/div[2]/div[1]/div[3]/div[5]/div[1]/div[2]/table[1]/tr[2]/td[1]/div[1]/div[2]/table[1]

The second table is nested in the first, and I'd like to shorten the above SelectSingleNode twice to something like this

$contactsBlock.SelectSingleNode(".//table/*/table") and skip the in-between.

Is there a way to wild-card like this?

Upvotes: 1

Views: 212

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

An XPath expression .//table//table should match all tables nested within other tables under the current node. Double forward slashes match arbitrary length paths.

.//table/*/table is unlikely to give you a match, because the asterisk wildcard matches one node (i.e. one level of hierarchy), so the nested table would have to be a grandchild node of the first table:

<table>
  <tr>
    <table>...</table>   <!-- nested table would have to go here -->
  </tr>
</table>

which would be quite unusual. Doesn't match the structure suggested by the XPath expression from your question, too.

Upvotes: 1

Related Questions