Xml Module
Hosted interpreter permissions: Every function and value on this page requires no host permissions. Higher-order functions still enforce the requirements of their callbacks. These checks do not apply to CLI execution or compilation. See Host permissions.
Use the Xml path-query syntax to extract element text and attribute values.
Xml.queryValues : string -> string -> string list option
None
Argument order is query -> xml. Returns Some values for a valid query/document, including Some [] when nothing is selected. Invalid queries or malformed XML return None.
let xml = "<projects><project name=\"api\">ready</project><project name=\"web\">pending</project></projects>"
let names = Xml.queryValues "projects/project/@name" xml
let states = Xml.queryValues "//project/text()" xml
// names = Some ["api"; "web"]
// states = Some ["ready"; "pending"]
| Query part | Meaning |
|---|---|
root/child | Walk named elements; the first step can name the root or a direct child of it |
Leading //name | Find matching elements anywhere, including the root, then walk remaining child steps |
Final /@attribute | Extract an attribute; elements missing it contribute no value |
Final /text() | Extract trimmed element text, including descendant text; empty text is omitted |
A query must contain at least one element step and a final selector. Namespace prefixes, wildcards, predicates, . and .. steps are unsupported. Results follow document traversal order. There is no public Xml.serialize or Xml.deserialize function.