Xpath - Selecting attributes using starts-with

Marek picture Marek · Oct 14, 2014 · Viewed 22.9k times · Source

I am trying to write an xpath expression that selects all div tags that have an attribute id that start with CompanyCalendar. Below is a snippet of the HTML that I am looking at:

<td class="some class" align="center" onclick="Calendar_DayClicked(this,'EventCont','Event');">
        <span class="Text"></span>
        <div id="CompanyCalendar02.21" class="Pop CalendarClick" style="right: 200px; top: 235px;"></div>

There are multiple divs that have an id like CompanyCalendar02.21 but for each new month in the calendar, they change the id. For example, the next month would be CompanyCalendar02.22. I would like to be able to select all of the divs that are equal to CompanyCalendar*

I am rather new at this so I was using some example off the net to try and get my xpath expression to work but to no avail. Any help would be greatly appreciated.

Answer

Mathias M&#252;ller picture Mathias Müller · Oct 14, 2014

I am trying to write an xpath expression that selects all div tags that have an attribute id that start with CompanyCalendar.

The following expression is perhaps what you are looking for:

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

What it does, in plain English, is

Return all div elements in the XML document that have an attribute id whose attribute value starts with "CompanyCalendar".