In one of my previous posts about the OmniMark conversion scheme I explained the different translate types at high level.

I will now demonstrate a simple OmniMark downtranslate example. In this example an XML document is converted to (X)HTML.

OmniMark source code embedded with XML source
  1. process
  2.   do xml-parse
  3.     scan '<doc><title>The exiting world of OmniMark</title>' ||
  4.          '<section><title>First title</title>' ||
  5.              '<section><title>Subsection of first title</title>' ||
  6.              '<par>A paragraph with some text…</par>' ||
  7.              '<par>A second paragraph with some text…</par>' ||
  8.              '</section>' ||
  9.          '</section>' ||
  10.          '<section><title>Second title</title>' ||
  11.          '<par>Text in the second section.</par>' ||
  12.          '</section>' ||
  13.          '</doc>'
  14.     output '%c'
  15.   done
  16.  
  17. element #implied
  18.   output '%c'
  19.  
  20. element doc
  21.   output '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' ||
  22.          '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"' ||    
  23.          '>%n'
  24.   output '<html xmlns="http://www.w3.org/1999/xhtml">%n'
  25.   output '%c'
  26.   output '</body>%n'
  27.   output '</html>'
  28.  
  29. element title when parent is doc
  30.   output '<head><%q>%c</%q></head>%n'
  31.   output '<body>'
  32.  
  33. element title when parent is section
  34.   do when parent of parent is doc
  35.    output '<h1>%c</h1>%n'
  36.   else when parent of parent is section
  37.    output '<h2>%c</h2>%n'
  38.   done
  39.  
  40. element par
  41.   output '<p>%c</p>%n'
The resulting output
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head><title>The exiting world of OmniMark</title></head>
  5. <body><h1>First title</h1>
  6. <h2>Subsection of first title</h2>
  7. <p>A paragraph with some text…</p>
  8. <p>A second paragraph with some text…</p>
  9. <h1>Second title</h1>
  10. <p>Text in the second section.</p>
  11. </body>
  12. </html>

Explaining the source code

You can see that the syntax of the language is very declarative for itself.

The do xml-parse scan ... done block is the part where the XML input source is the input data for the parser. The XML source is not loaded as a whole into memory but it streams like a water flow through the parser. While it streams, events occur and the corresponding element rules are fired.

The order in which these element rules are written is not important, they are only fired when the corresponding event occurs.

%c explained

The % character is an escape character, %c is the parse continuation operator so the XML source continues to flow and element rules are fired, less or more comparable with the XSLT <xsl:apply-templates/> element.

An other escape sequence is %n which represents a newline. %q is for the name of the current element.

No nested rules, but nested execution of the rules

The first rule which is fired when the input data flows and the <doc> start tag is encountered is the element doc rule on line 20. Because %c is used on line 25, the XML stream continues to flow and other element rules will be fired. At the end of the stream when the end tag </doc> is encountered, line 26 and 27 will be the last lines that will be executed.

The default element rule element #implied

Every element must have a corresponding element rule, if not an error will raise.

The section element has no specific rule, so the element #implied is fired. In this specific case the rule says “go on with the stream, no specific output”.

Rating 3.00 out of 5
[?]