Refactor XPath object creation to its own function