On this page:
1.1 SXML Functions
sxml: element?
ntype-names??
ntype??
ntype-namespace-id??
sxml: node?
sxml: attr-list
sxml: attr-list-node
sxml: empty-element?
sxml: element-name
sxml: ncname
sxml: name->ns-id
sxml: content
sxml: text
sxml: attr
sxml: change-content
sxml: change-attrlist
sxml: change-name
sxml: set-attr
sxml: add-attr
sxml: change-attr
sxml: squeeze
sxml: clean

1 SXML

SXML is a representation of XML elements using s-expressions. The following grammar describes the structure of SXML:

  top = 
(*TOP* maybe-annotations
       PI ...
       comment ...
       element)
     
  element = (name maybe-annot-attributes child ...)
     
  annot-attributes = (@ attribute ... maybe-annotations)
     
  attribute = (name maybe-value maybe-annotations)
     
  child = element
  | character-data-string
  | PI
  | comment
  | entity
     
  PI = 
(*PI* pi-target
      maybe-annotations
      processing-instruction-content-string)
     
  comment = (*COMMENT* comment-string)
     
  entity = (*ENTITY* public-id-string system-id-string)
     
  name = local-name
  | exp-name
     
  local-name = symbol conforming to XML Namespace recommendation
     
  exp-name = symbol of the form namespace-id:local-name
     
  namespace-id = URI-symbol
  | user-ns-shortcut-symbol
     
  namespaces = (*NAMESPACES* namespace-assoc ...)
     
  namespace-assoc = (namespace-id uri-string maybe-original-prefix)
     
  annotations = (@ maybe-namespaces annotation ...)
     
  annotation = not yet specified

Some tools, such as SXPath, use the following coarse approximation of SXML structure for simplicity:

  node = (name . node-list)
  | string
     
  node-list = (node ...)
     
  name = local-name
  | exp-name
  | @
  | *TOP*
  | *PI*
  | *COMMENT*
  | *ENTITY*
  | *NAMESPACES*

Refer to the original SXML specification for a more detailed explanation of the representation, including examples.

In short, an XML element is represented as a list consisting of its tag name as a symbol followed by its children nodes. If the XML element has attributes, they come immediately after the tag symbol, in a list tagged by an @ symbol.

For example, the XML element

<abc>def<ghi />jkl</abc>

is represented by the SXML datum

'(abc "def" (ghi) "jkl")

and the XML element

<customer specialness="gazonga">Barry White</customer>

is represented by the SXML datum

'(customer (@ (specialness "gazonga")) "Barry White")

That’s the easy part. Things get more tricky when you start talking about documents and namespaces.

1.1 SXML Functions

(sxml:element? v)  boolean?
  v : any/c
Returns #t if v is a list starting with a symbol that is not a special symbol, #f otherwise.

Examples:

> (sxml:element? '(p "blah"))

#t

> (sxml:element? '(*COMMENT* "ignore me"))

#f

> (sxml:element? '(@ (href "link.html")))

#f

(ntype-names?? tags)  (-> any/c boolean?)
  tags : (listof symbol?)
Given a list of allowable tag names, returns a predicate that recognizes elements with those tags.

Examples:

> ((ntype-names?? '(a p)) '(p "blah"))

'(p)

> ((ntype-names?? '(a p)) '(br))

#f

(ntype?? crit)  (-> any/c boolean?)
  crit : symbol?
If crit is a special symbol, a predicate is returned that accepts the following classes of node:

Otherwise, it is an ordinary tag name, and a predicate is returned that recognizes elements with that tag.

Examples:

> ((ntype?? '*) "blah")

#f

> ((ntype?? '*) '(p "blah"))

#t

> ((ntype?? '*text*) "blah")

#t

> ((ntype?? '*text*) '(p "blah"))

#f

(ntype-namespace-id?? ns-id)  (-> any/c boolean?)
  ns-id : (or/c string? #f)
Returns a predicate that recognizes elements with tags belonging to the namespace ns-id. If ns-id is #f, the predicate recognizes elements whose tags have no namespace.

Examples:

> ((ntype-namespace-id?? "atom") '(atom:id "blah"))

#t

> ((ntype-namespace-id?? "atom") '(atomic "section"))

#f

> ((ntype-namespace-id?? #f) '(atomic "section"))

#t

(sxml:node? v)  boolean?
  v : any/c
Returns #t for anything except an attribute list (that is, a list whose first element is '@).

Note that the set of values accepted by sxml:node? is different from the non-terminal node.

Examples:

> (sxml:node? '(a (@ (href "link.html")) "blah"))

#t

> (sxml:node? '(@ (href "link.html")))

#f

(sxml:attr-list node)  (listof attribute)
  node : node
If node is an element, returns its list of attributes (or '()) if it has no attributes; for all other types of node, returns '().

Examples:

> (sxml:attr-list '(a (@ (href "link.html")) "blah"))

'((href "link.html"))

> (sxml:attr-list '(p "blah"))

'()

> (sxml:attr-list "blah")

'()

(sxml:attr-list-node elem)
  (or/c #f (cons/c '@ (listof attribute)))
  elem : sxml:element?
Returns an element’s attribute list node, or #f it is has none. Compare sxml:attr-list.

Examples:

> (sxml:attr-list-node '(a (@ (href "link.html")) "blah"))

'(@ (href "link.html"))

> (sxml:attr-list-node '(p "blah"))

#f

(sxml:empty-element? elem)  boolean?
  elem : sxml:element?
Returns #t if elem has no nested elements, text nodes, etc. The element may have attributes.

Examples:

> (sxml:empty-element? '(br))

#t

> (sxml:empty-element? '(p "blah"))

#t

> (sxml:empty-element? '(link (@ (rel "self") (href "here.html"))))

#t

(sxml:element-name elem)  symbol?
  elem : sxml:element?
Returns an element’s tag.

(sxml:ncname qualified-name)  string?
  qualified-name : symbol?
Returns the local part of a qualified name.

(sxml:name->ns-id qualified-name)  (or/c string? #f)
  qualified-name : symbol?
Returns the namespace part of a qualified name.

(sxml:content node-or-nodeset)  (listof node)
  node-or-nodeset : (or/c node nodeset?)
Returns the contents (elements and text nodes) of an element or nodeset.

(sxml:text node-or-nodeset)  string?
  node-or-nodeset : (or/c node nodeset?)
Returns a string consisting of all of the character data immediately within node-or-nodeset.

Example:

> (sxml:text '(p (em "red") " fish; " (em "blue") " fish"))

" fish;  fish"

(sxml:attr elem attr-name)  (or/c string? #f)
  elem : sxml:element?
  attr-name : symbol?
Gets the value of the attr-name attribute of elem.

(sxml:change-content elem new-content)  sxml:element?
  elem : sxml:element?
  new-content : (listof child)
Replaces the content of elem with new-content, preserving its attributes and auxiliary information.

(sxml:change-attrlist elem new-attrlist)  sxml:element?
  elem : sxml:element?
  new-attrlist : (listof attribute)
Replaces the attributes of elem with new-attrlist, preserving its contents and auxiliary information.

(sxml:change-name elem tag)  sxml:element?
  elem : sxml:element?
  tag : symbol?
Changes the tag name of elem, preserving its attributes, auxiliary information, and contents.

(sxml:set-attr elem attr)  sxml:element?
  elem : sxml:element?
  attr : (list/c symbol? any/c)
Returns an element like elem but with the attribute attr, which replaces any existing attribute with attr’s key.

(sxml:add-attr elem attr)  (or/c sxml:element? #f)
  elem : sxml:element?
  attr : (list/c symbol? any/c)
Like sxml:set-attr, but returns #f if elem already contains an attribute with attr’s key.

(sxml:change-attr elem attr)  (or/c sxml:element? #f)
  elem : sxml:element?
  attr : (list/c symbol? any/c)
Like sxml:set-attr, but returns #f unless elem already contains an attribute with attr’s key.

(sxml:squeeze elem)  sxml:element?
  elem : sxml:element?
Eliminates empty attribute lists and auxiliary lists.

(sxml:clean elem)  sxml:element?
  elem : sxml:element?
Eliminates empty attribute lists and removes all auxilary lists.