This post will explain some of the unique conversion features of OmniMark, which is uptranslate and context translate (See the conversion scheme to have an overview of the different types of conversion).

An uptranslate makes it possible to create XML (or SGML) starting from structured data. And this structured data doesn’t have to be XML. While creating this XML it is sent to the parser.

A context translate goes even a step further, it’s a combination of an uptranslate followed by a downtranslate.

So before going into the details, summarized the OmniMark conversion types:

  • uptranslate (structured data to XML/SGML): uses find rules, the parser is involved
  • downtranslate (XML/SGML to structured data): uses element rules, the parser is involved
  • context translate: uptranslate followed by downtranslate
  • crosstranslate:  uses find rules, no parser

A context translate is sometimes used instead of a crosstranslate if you have a complex structured data conversion. XML is then used as an intermediate state.

Uptranslate construction

OmniMark source code, uptranslate
  1. define string source function CreateXML value string source theInput
  2. as
  3.   using output as #current-output & #main-output
  4.     using group your-findrules
  5.       submit theInput
  7. process
  8.   do xml-parse
  9.    scan CreateXML '<html>[head][/head][BODY][/BODY]</html>'
  10.      suppress
  11.   done
  13. element #implied
  14.   suppress
  16. group your-findrules
  17.   find  '[' '/'? => slash  any++ => nameOfElement ']'
  18.     output '<' || slash || 'lg' % nameOfElement || '>'
The output result
  1.   <html><head></head><body></body></html>

So what are we doing here, we create a string source function CreateXML. This is a special kind of function because it streams data input and output without buffering. A normal string function only returns all data at once at the end of the scope of the function.

So the input parameter of the function (theInput) consumes the input string “<html>[head][/head][BODY][/BODY]</html>”. The output of the function goes to two predefined streams #current-output and #main-output. In this case #current-output is the parser because the function is called within the do xml-parse construction. #main-output is the standard output which is in this case the console.

So the function sends the data to the parser and the output destination at the same time. The parser itself supresses the data while parsing it. That is also the reason why we have to send the data to a second stream, in this case #main-output. Otherwise we would not have output at all.

While the function sends data it executes the find rules. The find rule here as a simple example transforms “[” to “<” and the elementnames from uppercase to lowercase. The result is XML which is sent to the parser (to check wellformdness or validate) and an other separate output stream.

Context translate

Suppose we want to change the created XML we send to the parser. Let’s take again a simple example, we take the same input string “<html>[head][/head][BODY][/BODY]</html>” but we want to change the <html>-element to <doc> and the <head>-element to <header>.

OmniMark source code, context translate
  1. define string source function CreateXML value string source theInput
  2. as
  3.   using group your-findrules
  4.     submit theInput
  6. process
  7.   do xml-parse
  8.    scan CreateXML '<html>[head][/head][BODY][/BODY]</html>'
  9.      output '%c'
  10.   done
  12. element #implied
  13.   output '<%q>%c</%q>'
  15. element html
  16.   output '<doc>%c</doc>'
  18. element head
  19.   output '<header>%c</header>'
  21. group your-findrules
  22.   find  '[' '/'? => slash  any++ => nameOfElement ']'
  23.     output '<' || slash || 'lg' % nameOfElement || '>'
The output result
  1. <doc><header></header><body></body></doc>

We now send again the output data of the function to the parser but we don’t suppress it any longer. We create some extra element rules to change the name of the elements.

I intentionally kept the examples very simple so the principles of uptranslate and context translate should now be clear.

Rating 3.00 out of 5