herolab.XPath: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 2: | Line 2: | ||
|name=herolab.XPath | |name=herolab.XPath | ||
|trusted=true | |trusted=true | ||
|version=1.5 | |version=1.5.0 | ||
|description= | |description= | ||
Returns data results based on the passed in XPath expression from the XML statblock. This is a reliable and easier way to get stat data from a character than using regular expression parsing of Text stat blocks. | |||
|usage= | |usage= | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
herolab.XPath(XPath) | herolab.XPath(XPath) | ||
herolab.XPath(XPath, id) | herolab.XPath(XPath, id) | ||
</ | </syntaxhighlight> | ||
==== Options Coming in 1.8 ==== | |||
<syntaxhighlight lang="mtmacro" line> | |||
herolab.XPath(XPath, tokenRef, delim) | |||
herolab.XPath(XPath, tokenRef, "json") | |||
</syntaxhighlight> | |||
'''Parameters''' | '''Parameters''' | ||
{{param|XPath|The XPath expression to evaluate against the XML statblock.}} | {{param|XPath|The XPath expression to evaluate against the XML statblock.}} | ||
{{param|id| | {{param|tokenRef|Either the token [[getSelected|{{code|id}}]] or [[getTokenName|Token Name]] of the token. Defaults to the Current Token.}} | ||
{{param|delim|'''Coming in 1.8'''. Custom delimiter for the returned results. Defaults to {{code|", "}}. If equal to {{code|"json"}}, a [[JSON Array]] will be returned instead of a [[String List]].}} | |||
{{Note|Token IDs are unique, but Token Names can be duplicated. Using Token Name when more than one token has the same name can produce unexpected results.}} | |||
Returns the requested data. | Returns the requested data. | ||
|example= | |example= | ||
Get | Get various values from Hero Lab data (which is in XML). | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[race = herolab.XPath('/document/public/character/race/@name')] | [race = herolab.XPath('/document/public/character/race/@name')] | ||
[alignment = herolab.XPath('/document/public/character/alignment/@name')] | [alignment = herolab.XPath('/document/public/character/alignment/@name')] | ||
[improvedInit = herolab.XPath('boolean(/document/public/character/feats/feat[starts-with(@name,"Improved Initiative")])')] | [improvedInit = herolab.XPath('boolean(/document/public/character/feats/feat[starts-with(@name,"Improved Initiative")])')] | ||
</ | </syntaxhighlight> | ||
Returns: | Returns: | ||
< | <syntaxhighlight lang="mtmacro"> | ||
Human Neutral Good 1 | |||
</ | </sosyntaxhighlightrce> | ||
==== Changes Coming in 1.8 ==== | |||
Sometimes the data desired from herolab contains commas or is otherwise incompatible with the [[String List]] format and default (comma) separator. In the following example, the list of spells retrieved includes one called "Delay Poison, Communal" - as we see in the default output, this element would be incompatible with the standard String List format. | |||
<syntaxhighlight lang="mtmacro" line> | |||
[h: vXPath = "/document/public/character/spellsmemorized/spell[contains(@name,'De')]/@name"] | |||
Default: [r: herolab.XPath(vXPath)]<br /> | |||
Custom Delim: [r: herolab.XPath(vXPath, token.name, "|")]<br /> | |||
JSON: <pre>[r: json.indent(herolab.XPath(vXPath, currentToken(), "json"))]</pre> | |||
</syntaxhighlight> | |||
Returns: | |||
<syntaxhighlight lang="mtmacro"> | |||
Default: Delay Poison, Communal, Detect Magic, Detect Undead | |||
Custom Delim: Delay Poison, Communal|Detect Magic|Detect Undead | |||
JSON: | |||
[ | |||
"Delay Poison, Communal", | |||
"Detect Magic", | |||
"Detect Undead" | |||
] | |||
</syntaxhighlight> | |||
When dealing with more complicated text elements with potential delimiter conflicts, it will usually be better to request the JSON output. | |||
|also= | |also= | ||
[[Hero Lab Integration|Hero Lab Integration]] | [[Hero Lab Integration|Hero Lab Integration]] | ||
|changes= | |changes= | ||
* '''1. | * '''1.8''' - Added optional param for custom delimiter in returned results | ||
}} | }} | ||
[[Category:Hero Lab Function]] | [[Category:Hero Lab Function]] |
Latest revision as of 23:59, 17 May 2024
herolab.XPath() Function
Note: This function can only be used in a Trusted Macro
• Introduced in version 1.5.0
Returns data results based on the passed in XPath expression from the XML statblock. This is a reliable and easier way to get stat data from a character than using regular expression parsing of Text stat blocks.
Usage
herolab.XPath(XPath)
herolab.XPath(XPath, id)
Options Coming in 1.8
herolab.XPath(XPath, tokenRef, delim)
herolab.XPath(XPath, tokenRef, "json")
Parameters
XPath
- The XPath expression to evaluate against the XML statblock.tokenRef
- Either the tokenid
or Token Name of the token. Defaults to the Current Token.delim
- Coming in 1.8. Custom delimiter for the returned results. Defaults to", "
. If equal to"json"
, a JSON Array will be returned instead of a String List.
Token IDs are unique, but Token Names can be duplicated. Using Token Name when more than one token has the same name can produce unexpected results.
Returns the requested data.
Example
Get various values from Hero Lab data (which is in XML).
When dealing with more complicated text elements with potential delimiter conflicts, it will usually be better to request the JSON output.
[race = herolab.XPath('/document/public/character/race/@name')]
[alignment = herolab.XPath('/document/public/character/alignment/@name')]
[improvedInit = herolab.XPath('boolean(/document/public/character/feats/feat[starts-with(@name,"Improved Initiative")])')]
Returns:
Human Neutral Good 1
</sosyntaxhighlightrce>
==== Changes Coming in 1.8 ====
Sometimes the data desired from herolab contains commas or is otherwise incompatible with the [[String List]] format and default (comma) separator. In the following example, the list of spells retrieved includes one called "Delay Poison, Communal" - as we see in the default output, this element would be incompatible with the standard String List format.
<syntaxhighlight lang="mtmacro" line>
[h: vXPath = "/document/public/character/spellsmemorized/spell[contains(@name,'De')]/@name"]
Default: [r: herolab.XPath(vXPath)]<br />
Custom Delim: [r: herolab.XPath(vXPath, token.name, "|")]<br />
JSON: <pre>[r: json.indent(herolab.XPath(vXPath, currentToken(), "json"))]</pre>
Returns:
Default: Delay Poison, Communal, Detect Magic, Detect Undead
Custom Delim: Delay Poison, Communal|Detect Magic|Detect Undead
JSON:
[
"Delay Poison, Communal",
"Detect Magic",
"Detect Undead"
]
See Also
Version Changes
- 1.8 - Added optional param for custom delimiter in returned results