XML Expression Path Lookup
xexpr-path/  c
xexpr-path-list
xexpr-path-first
xexpr-path-text
8.12

XML Expression Path Lookup🔗ℹ

Jan Dvorak <mordae@anilinux.org>

 (require xexpr-path) package: xexpr-path

Utilities to find XML expression elements using a simple path.

Contract for XML Expression paths.

Path is a list of components. A valid componenet can be:

symbol?

 

to match tag name, '* matches any tag

(list/c symbol?)

 

to match an attribute

(list/c symbol? string?)

 

to match tags with given attribute value

procedure

(xexpr-path-list path xexpr)  (listof (or/c xexpr/c string?))

  path : xexpr-path/c
  xexpr : xexpr/c
Look up all matching elements.

Elements can be matched using tag names.

Example:
> (xexpr-path-list '(greeting)
                   '(greetings (greeting "Hello World!")
                               (greeting "Ahoj Světe!")))

'((greeting "Hello World!") (greeting "Ahoj Světe!"))

In some cases, it can be handy to further filter the elements by values of their attributes.

Example:
> (xexpr-path-list '(value (type "int"))
                   '(dataset (value ((type "int")) "42")
                             (value ((type "str")) "foo")
                             (value ((type "int")) "13")))

'((value ((type "int")) "42") (value ((type "int")) "13"))

procedure

(xexpr-path-first path xexpr)  (or/c xexpr/c string? #f)

  path : xexpr-path/c
  xexpr : xexpr/c
Look up first matching element, return #f when none match.

Examples:
> (xexpr-path-first '(item) '(items (item "one") (item "two")))

'(item "one")

> (xexpr-path-first '(item) '(items))

#f

procedure

(xexpr-path-text path xexpr)  (or/c string? #f)

  path : xexpr-path/c
  xexpr : xexpr/c
Look up all matching elements, convert them to text using xexpr->string and concatenate them together.

Keep in mind that this function does not return plain string, but rather valid XML representation of the matched elements.

Example:
> (xexpr-path-text '(prop (id "name") *)
                   '(object (prop ((id "kind")) "show")
                            (prop ((id "name")) "Lucky" 9734 "Star")))

"Lucky&#9734;Star"