On this page:
xml-path?
attributes-ref
read-xexpr/  standardized
write-xexpr/  standardized
8.1 Raw X-Expressions
raw-xexpr/  c
raw-xexpr-atom/  c
raw-xexpr-element/  c
entity-symbol/  c
raw-xexpr?
raw-xexpr-element?
check-raw-xexpr-element
8.1.1 Plain-Text Conversion
non-element-xexpr->plain-text
non-element-body->plain-text
8.2 Normalized X-Expressions
normalized-xexpr/  c
normalized-xexpr-atom/  c
normalized-xexpr-element/  c
normalize-xexpr
normalized-xexpr?
normalized-xexpr-element?
normalized-comment/  c
normalized-comment?
normalized-p-i/  c
normalized-p-i?
check-normalized-xexpr-element
8.3 xmllint-based Operations
xmllint-available?
valid-xml-file?
directory-validate-xml
call/  prettyprint-xml-out
0.5.91

8 X-Expression and XML Operations🔗

procedure

(xml-path? pth)  any/c

  pth : path-string?
Tests whether pth is syntactically a path to an XML file, without checking the validity of the file or even its existance.

procedure

(attributes-ref attrs key)  (or/c #f string?)

  attrs : (listof (list/c symbol? string?))
  key : symbol?
Returns the string value of the key attribute in attrs (the attributes list of an x-expression) or #f if none exists.

procedure

(read-xexpr/standardized [in])  xexpr/c

  in : input-port? = (current-input-port)
Somewhat like syntax:read-xml or string->xexpr, but handles various details to ensure that the input from in is converted to an x-expression consistently and without data loss.

Some specific guarantees:
  • A redundant UTF-8 byte-order mark at the beginning of the input will be ignored, which is not true with the functions from xml.

  • XML comments will be preserved in the resulting x-expression as comment data structures.

procedure

(write-xexpr/standardized xs [out])  any

  xs : xexpr/c
  out : output-port? = (current-output-port)
Like read-xexpr/standardized, but for writing x-expressions. Use write-xexpr/standardized as a drop-in replacement for write-xexpr.

8.1 Raw X-Expressions🔗

Many functions in this library that operate on x-expressions require that they conform to the additional requirement that all symbols representing symbolic entities be known to this library, which is determined by the contract entity-symbol/c. X-expressions meeting this requirement qualify as raw x-expressions (or raw xexprs) and are recognized by the contract raw-xexpr/c.

The contract raw-xexpr-element/c recognizes specifically those raw x-expressions which represent XML elements (all of which would also satisfy list?).

Note that normalized x-expressions are a subset of raw x-expressions.

Similar to symbol?, but only recognizes those symbols which correspond to symbolic entities known to this library, which can be converted to strings with functions like non-element-xexpr->plain-text. Most commonly-used symbolic entities are supported.

A value satisfying entity-symbol/c is allways a raw x-expression, but never a normalized x-expression.

procedure

(raw-xexpr? v)  any/c

  v : any/c

procedure

(raw-xexpr-element? v)  any/c

  v : any/c
Equivalent to raw-xexpr/c and raw-xexpr-element/c, respectively, except that these functions avoid some overhead assosciated with the contract system, but give bad error messages when used as contracts.

procedure

(check-raw-xexpr-element blame 
  val 
  neg-party) 
  (and/c α raw-xexpr-element/c)
  blame : blame?
  val : α
  neg-party : any/c
A low-level function that can be used to implement contracts derived from raw-xexpr-element/c.

Returns val directly if it satisfies raw-xexpr-element/c; otherwise, calls raise-blame-error to report the details of the violation, supplying neg-party as the missing party of the blame object.

8.1.1 Plain-Text Conversion🔗

Converts a raw x-expression (including normalized xexprs) that does not represent an XML element to a string.

Because of the restriction that xs must not represent an XML element, (non-element-xexpr->plain-text xs) is very similar to (normalize-xexpr xs), except that comment and p-i (processing instruction) structures are replaced by "".

Like non-element-xexpr->plain-text, but concatenates the string forms of a list of non-element raw xexprs. As the name of non-element-body->plain-text suggests, body is usually the body of a raw xexpr element that may not contain any child elements.

8.2 Normalized X-Expressions🔗

Some higher-level operations in this library produce and/or consume normalized x-expressions (or normalized xexprs), which are a restricted subset of raw x-expressions.

These are non-destructively normalized in a few ways, which simplifies case analysis when working with normalized xexprs:

procedure

(normalize-xexpr raw)  normalized-xexpr/c

  raw : raw-xexpr/c
Returns a normalized xexpr equivalent to the given raw xexpr.

procedure

(normalized-xexpr? v)  any/c

  v : any/c

procedure

(normalized-xexpr-element? v)  any/c

  v : any/c
Equivalent to normalized-xexpr/c and normalized-xexpr-element/c, respectively, but with the same relative advantages and disadvantages as raw-xexpr? and raw-xexpr-element?.

A contract and predicate recognizing XML comment data structures (as in comment?) that contain only immutable strings.

value

normalized-p-i/c : flat-contract?

procedure

(normalized-p-i? v)  any/c

  v : any/c
Like normalized-comment/c and normalized-comment?, respectively, but for XML processing-instruction data structures (as in p-i?) that contain only immutable strings.

procedure

(check-normalized-xexpr-element blame 
  val 
  neg-party) 
  (and/c α normalized-xexpr-element/c)
  blame : blame?
  val : α
  neg-party : any/c
Like check-raw-xexpr-element, but for implementing contracts derived from normalized-xexpr-element/c.

8.3 xmllint-based Operations🔗

The functions documented in this section depend on the external command-line utility xmllint (which is part of libxml2) to work as their names indicate. If xmllint can not be found (see xmllint-available?), a warning is logged to (current-logger) at startup and these functions fall back to the alternate behavior specified in each function’s documentation, which is typically a noop.

procedure

(xmllint-available?)  boolean?

Reports whether xmllint is available at runtime.

procedure

(valid-xml-file? [#:quiet? quiet?] pth ...+)  boolean?

  quiet? : any/c = #t
  pth : path-string?
Checks that every pth is a valid TEI XML file according to Digital Ricœur’s customized DTD (but not the additional project-specific requirements enforced by contracts like any-tei-xexpr/c).

When quiet? is #false, writes any validation error messages (from xmllint) to current-error-port.

If xmllint is not available, always returns #true.

Changed in version 0.5.1 of package ricoeur-tei-utils: Changed to always use Digital Ricœur’s DTD. Previously, valid-xml-file? was not specific to Digital Ricœur. It had the same meaning as passing --valid to xmllint, and it depended on a system DOCTYPE declaration to find the DTD file via a relative path reference. Instead, valid-xml-file? now uses a DTD file distributed with this library, which improves reliability at the cost of rejecting XML files that might be valid according to other arbitrary DTDs.

procedure

(directory-validate-xml dir    
  [#:quiet? quiet?])  boolean?
  dir : (and/c path-string? directory-exists?)
  quiet? : any/c = #f
Checks that every path satisfying xml-path? in dir and its recursive subdirectories is a valid TEI XML file according to Digital Ricœur’s customized DTD, as with valid-xml-file?.

When quiet? is #false, writes any validation error messages (from xmllint) to current-error-port.

If xmllint is not available, always returns #true.

Changed in version 0.5.1 of package ricoeur-tei-utils: Changed to be specific to Digital Ricœur’s DTD, consistent with the change to valid-xml-file?.

procedure

(call/prettyprint-xml-out thunk)  any/c

  thunk : (-> any/c)
If xmllint is not available, equivalent to (thunk).

When xmllint is available, thunk is called in a context where everything written to the current-output-port is piped through xmllint’s prettyprint function before being written to the original current-output-port. When prettyprinting succeeds, the result of call/prettyprint-xml-out is the result of thunk.

If prettyprinting fails (perhaps because the output of thunk was not well-formed XML), xmllint may still write to the original current-output-port, but call/prettyprint-xml-out raises an exception rather than returning a value. See with-output-to-file/unless-exn for an alternative when this behavior is undesirable.

If thunk raises an exception and xmllint is available, xmllint is never invoked and nothing is written to the original current-output-port.

Note: While xmllint’s prettyprint function natively uses platform-specific line endings, call/prettyprint-xml-out arranges to replace these with "\n" (Racket’s internal line ending) on all platforms for the purposes of writing to the current-output-port.