When I have to create some textual representation of data, I can use a template language, so that my code does not have to worry about the structure of the output file - I can sometimes even write code that's independent of whether the output is XML, LaTeX or any other plain text. A simple example:

Template (in separate file):




Result (written to output file):


I want to do the same, but in the opposite direction. I have XML or plain text or whatever files to input. I want to describe the input structure in a separate file that looks like the input but has these variable declarations in it, and I want to handle it with code that's independent of the structure.

Has this been done before? What approach was used and how did it work out?

(We usually handle XML input by using an XML parser library to describe the input structure in program code, handle plaintext input by writing regexes in code, and don't handle LaTeX input because LaTeX can't really be parsed.)

  • Are you looking for Velocity? May 30, 2014 at 7:31
  • 8
    He is not looking for a recommendation, he is asking if a library like this even exists, or if the concept has a name. Please, please, do not be so rush in closing questions. This site is becoming unusable
    – Andrea
    May 30, 2014 at 7:53
  • @SilviuBurcea: Are you sure Velocity can take a template and it's output and extract the substituted values? All I've seen is the normal generation stuff.
    – Jan Hudec
    May 30, 2014 at 8:22

5 Answers 5


A variety of tools like this exist. The choice of tool depends ont the type of input and the kind of extraction or transformation required.

The simplest tool is Perl like regexps that now exist in most modern languages. The main limitation of these is that they do not naturally handle recursive structure in the input. Also languages/formats with reasonably complex escaping/unescaping mechanisms can be tricky to capture correctly with regexps.

If you are specifically interested in XML input pattern matching then CDuce and XDuce are probably closest to what you are after. Scala also has a similar but weaker form of XML pattern matching built into the language. This kind of thing used to be a fairly active area a few years ago, many of the tools are listed on this Wikipedia page.

General input with recursive structure requires some form of parser generator like yacc, bison or Antlr.

  • Very interesting references (+1)
    – Giorgio
    Jun 1, 2014 at 8:41

Simple templating doesn't really work in the reverse direction. The reverse direction is (effectively) parsing. The problem with parsing based on just templates is that a typical template or set of templates can be applied to a typical input string in lots of ways ... resulting in ambiguous parses.

This is not to say that reverse templating is impossible. But I think that you can only make it practical in the context of a grammar and/or schema. For example, to handle XML templates your reverse templating engine would need at least an understanding of core XML syntax, and possible also an XML schema or DTD as well.

Has it been done? Well I used a system called Refine in the 1990s that did something rather like this. (It was designed as a framework for program language engineering / reverse engineering, and it allowed you to do funky things like defining a custom grammar and define parse-tree patterns in the context of that grammar.) I recall it being quite powerful ... but the system was really expensive, and never took off.


The closest I think you're going to find is web scraping software. Most of those use xpath or similar rather than a template, but I'm not familiar with the entire field.

It wouldn't be terribly difficult to write your own, though, especially if you limit your templates to simple substitution, no loops or if statements, and you make all your variables strings. You're basically looking for a long string of fixed text, followed by a unknown-length string to put into the variable, followed by another fixed string. I would estimate less than a day to do a bare bones proof of concept.


In C-family of languages, scanf() family of functions takes a format string that looks almost the same as the format string used for printf() family of functions.

int num = 5;
printf("%d", num);
scanf("%d", &num);

For XML, XForm does two-way binding, though you would need to declare the binds separately rather than inside the template itself, XForm can even do complex stuffs like binding into repeated/looped data. What makes this possible is due to the use of XPath for describing data binds.

You could also argue that regular expression with multiple capturing groups and findall() are a kind of input template languages as well.

how did it work out?

In general, parsing in XML, JSON, or other formatted languages cannot be done with general purpose string-based input template language (e.g. scanf, regex). XML and JSON has their own idea about what tokens are significant and what tokens can be collated/ignored (e.g. whitespace), so a general string-based input formatting language will generally be too cumbersome/too limited be able to parse XML/JSON completely. If you need to parse in XML, you need an input template language that understands XML syntax, and so on.


Yes, it has been done before. For example you can use the same URL templates by routing a web application and by constructing links:

/users/{id} + {id:123} -> /users/123 -- constructing a link
/users/123 + /users/{id} -> {id:123} -- routing requests

Be aware that input and output representations of the same data are usually asymmetric. For example by REST you respond with hypermedia and you usually request with just a simple JSON, which is a huge difference...

Input representations are usually much simpler, and they can contain validation data, while output representations are more complex, and they does not contain validation data unless they describe input fields. So your approach has its limits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.