Personally I wouldn't go with the xpath but instead create a list of the WebElements via driver.getElements(By.tagName("li")) but if you're hellbend on using xpaths you could use a simple try catch structure to see if the current element exists and let it return the last existing path. I'd try something along the lines of
static String getLastElement(){
String LastValidPath = "";
String xpathstart= ".//*[@id='name']/div/div[2]/fieldset/div[47]/ul/li[";
String xpathEnd="]/div/div[2]/div/div[2]/div[5]/span[1]/span[1]/span"))";
int value = 0;
Boolean found = false;
while(found == false){
String CurrentXpath = (xpathStart + value + xpathEnd);
try{
driver.findElement(By.xpath(CurrentXpath));
LastValidPath = CurrentXpath;
}catch(NoSuchElementException e){
found = true;
}
}
return LastValidPath;
}
This function looks up the given path and saves it as LastValidPath if the Path exists, if the Path doesn´t exist it throws a NoSuchElementException, breaks out of the loop and returns the last Path that existed.