SXML Misc Utilities
sxml:  text*
sxpath1
sxpath/  e
sxpath1/  e
sxpath*
sxpath1*
node-has-classes?
node-has-class?
exn:  fail:  sxpath
8.12

SXML Misc Utilities🔗ℹ

kurinoku

 (require sxml/extra-utils) package: sxml-extra

A module that has some sxml functions that might be of use

procedure

(sxml:text* node-or-nodeset)  string

  node-or-nodeset : (or node nodeset?)
Like sxml:text, but retrieves all string contents from a node and its children in depth first post order, which is likely the rendered order and the expected one.

Consider the following

> (define tree
    '(body
      (div "a ")
      (div
       (div "string ")
       "in "
       (div (span "or") "der"))))
> (string-join ((sxpath "//text()") tree) "")

"a in string deror"

> (sxml:text tree)

""

> (sxml:text* tree)

"a string in order"

procedure

(sxpath1 path [ns-bindings])

  (or/c #f (-> (or/c node nodeset?) (or/c node #f)))
  path : (or/c list? string?)
  ns-bindings : (listof (cons/c symbol? string?)) = '()
Like sxpath, but when the resulting function is applied, it returns the first element found on a breadth first search or it returns #f.

procedure

(sxpath/e path [ns-bindings])  (-> (or/c node nodeset?) nodeset?)

  path : (or/c list? string?)
  ns-bindings : (listof (cons/c symbol? string?)) = '()

procedure

(sxpath1/e path [ns-bindings])

  (-> (or/c node nodeset?) (or/c node #f))
  path : (or/c list? string?)
  ns-bindings : (listof (cons/c symbol? string?)) = '()
Like sxpath and sxpath1, but throws exn:fail:sxpath instead of writing the error to (current-error-port) and returning #f.

procedure

(sxpath* path doc [ns-bindings])  nodeset?

  path : (or/c list? string?)
  doc : (or/c node nodeset?)
  ns-bindings : (listof (cons/c symbol? string?)) = '()

procedure

(sxpath1* path doc [ns-bindings])  (or/c node #f)

  path : (or/c list? string?)
  doc : (or/c node nodeset?)
  ns-bindings : (listof (cons/c symbol? string?)) = '()
Equivalent to ((proc path ns-bindings) doc) where proc is either sxpath/e or sxpath1/e respectively.

procedure

(node-has-classes? class-lst)  sxml-converter

  class-lst : (listof (or/c string? symbol?))
Behaves likes sxml:filter, filters nodes by the elements of its class attribute.

The class attribute is made into a list by means of string-split.

All classes in the list must be present in the node to match.

Some examples found in the test suite.
> (define tree
    '(body
      (div (@ (class "mt-2 mb-2")))
      (div (@ (class "mt-2")))
      (div (@ (class "mt-2 mb-2 p-1")))))
> ((node-join
    (sxpath '(//))
    (node-reduce
     (sxpath '(div))
     (node-has-classes? '("mt-2" "mb-2"))))
  tree)

'((div (@ (class "mt-2 mb-2"))) (div (@ (class "mt-2 mb-2 p-1"))))

> ((sxpath
    `(// div
         ,(node-has-classes? '("mt-2" "mb-2"))))
   tree)

'((div (@ (class "mt-2 mb-2"))) (div (@ (class "mt-2 mb-2 p-1"))))

> ((sxpath
    `(// (div
          (,(node-has-classes? '("mt-2" "mb-2"))))))
   tree)

'((div (@ (class "mt-2 mb-2"))) (div (@ (class "mt-2 mb-2 p-1"))))

procedure

(node-has-class? class)  sxml-converter

  class : (or/c string? symbol?)
Like node-has-classes?, but for only one class.

struct

(struct exn:fail:sxpath exn:fail ()
    #:transparent)
Exception thrown by sxpath/e and similar.