1 Motivation

A common problem in XML transformations is that the transformation of an element is determined by the contents of surrounding elements. Often this is handled by handwriting a traversal function which passes the required context, outside of the framework of the micros and macros. Within the framework of micros and macros, one "trick" is to create an intermediate element, which wraps the required context around the interior node--and then to write an xml-rules pattern which processes the intermediate node and the interior node together. This technique was used in [5].

In the example in this article, we are given markup for an HTML document, extended with "sections":

(define ex
  (h4:html
    (h4:body
      (section
        num:
        "1"
        title:
        "First Section"
        (p "This is the intro section."))
      (section
        num:
        "2"
        title:
        "Second Section"
        (section num: "1" title: "A sub-section" (p "This is section 2.1."))
        (section
          num:
          "2"
          title:
          "Another sub-section"
          (p "This is section 2.2.")))
      (section
        num:
        "3"
        title:
        "Last major section"
        (p "This is the third section")))))

The goal is to achieve a formatting where a nested section such as above is formatted as

2.2. Another sub-section
...section contents...

Doing this requires propagating inwards the parent section numbers, to allow formatting something such as "2.1.5.3." to reflect the nesting of numbered sections. As noted above, one way to do this is to have the macro that processes section wrap its children with an intermediary element which wraps the required context:

(section-context
  prefix:
  "2.1.5."
  (section num: "3" title: "A four-level deep nested section" ...contents...))

And then we can have a macro for section-context, which, using xml-rules creates the require section heading, combining the known prefix with with current section's number. In addition, it must again wrap the new section's contents with a section-context element, giving the new section-heading prefix:

(xml-macro
  section-context
  (xml-rules
    ((_ prefix: p (section num: n title: t c ...))
     (xml-template
       (h4:div
         p
         n
         ". "
         t
         (section-context prefix: (string-append p n ".") c)
         ...)))
    ...other
    clauses...))

This works, but isn't always the right solution. Imagine adding the requirement that the title for each nest section must be formatted using the HTML header elements: h1, h2, h3..., with each nesting using one-smaller sized heading format.

Last modified: Sunday, February 27th, 2005 4:37:51pm
HTML generated using WebIt!.