Xpath
Function Syntax
string[int] xpath(string htmldocument ,string xpathselector )
- htmldocument is a string representing an html document such as that returned from visit_url
- xpathselector is a standard xpath selector for selecting nodes in the document
This function returns an array of strings, for efficiency purposes, not a map, representing the node(s) which xpath has selected for you in the html document. Unlike a map, arrays will encounter an "out of bounds" exception when attempting to add new elements to it, do note the difference. This function will return an empty array if no nodes match the selector. In the event the selector uses invalid syntax, this function will error with "invalid xpath expression"
Code Sample
The following code, if you are stuck in a choice adventure, will output the options of that choice adventure.
string[int] textOptions;
string[int] valueOptions;
string[int] choiceAdventureNumber;
string[int] choiceAdventureName;
string page=visit_url('charsheet.php');
textOptions=xpath(page,'//form[@action="choice.php"]//input[@type="submit"]/@value');
valueOptions=xpath(page,'//form[@action="choice.php"]//input[@type="hidden"][@name="option"]/@value');
choiceAdventureNumber=xpath(page,'//form[@action="choice.php"]//input[@type="hidden"][@name="whichchoice"]/@value');
choiceAdventureName=xpath(page,'//tr/td/b/text()');
if(count(textOptions)==0 || count(valueOptions)==0 || count(choiceAdventureNumber)==0)
{
print('not in a choice adventure');
}else{
string choiceAdvName;
string choiceAdvNum=choiceAdventureNumber[0];
if(count(choiceAdventureName)==0)
{
choiceAdvName='unnamed choice adventure';
}else{
choiceAdvName=choiceAdventureName[0];
}
print('choice adventure #'+choiceAdvNum+':'+choiceAdvName);
for x from 0 to count(valueOptions)-1
{
print('option '+valueOptions[x]+':'+textOptions[x]);
}
}
For example, if you used "intriguing puzzle box" from your inventory and ran the above script, you'd see the following output:
choice adventure #525:Fiddling with a Puzzle option 1:Push the left face. option 2:Turn the right face clockwise. option 3:Turn the rear face counterclockwise. option 4:Push the right face. option 5:Turn the right face counterclockwise. option 6:Turn the left face counterclockwise. option 7:Stop messing with this dang puzzle.