XPath: Get nodes where id contains a specific amount of letters

I'm using HTMLAgilityPack to retrieve data from a website. The information I want is in div tags where their id is "solicitudesXXXXXX", where X are uppercase letters and there are always 6.

This is what I have so far

//div[contains(@id,'solicitudes']

Is there any function in XPath or anything else to specify the amount of letters I want the id to have?

Upvotes: 2

Views: 579

Answers (1)

gtlambert
gtlambert

Reputation: 11961

Since the IDs are always of the same length, you could actually just use the XPath function starts-with():

//div[starts-with(@id, 'solicitudes')]

This will select all div elements with an id that starts with 'solicitudes'. If you do need to select IDs that are of a certain length then you could do the following:

//div[starts-with(@id, 'solicitudes') and string-length(@id)=17]

Note: both starts-with() and string-length() are compatible with all versions of XPath (1.0, 2.0 and 3.0).

Upvotes: 4

Related Questions