Earthly Powers

Tuesday Nov 14, 2006

URI templates

The URI template IETF draft specifies a very simple mechanism for representing embedded template variables in a URI. A URI template is transformed into a URI by subsituting the embedded template variables.

For example the following URI template:

http://employees/{id}

can be transformed to the following URI:

http://employees/paul

where id=paul

That is essentially it. Simple and very useful for machine processing! No regular expressions, no complex matching choices.

Initially i tending to think of just URI templates being transformed to URIs. Flipping things the other around, a URI can also be matched against one or more URI templates. 

Given the simplicity of URI templates it is really easy ascertain whether a URI matches a URI template by doing some pre-processing of the URI template using regular expressions.

1) search for patterns in the URI template that match the expression{([\w-._~]+?)}

2) replace all matches in 1) with the expression (.*?)

3) escape as appropriate all characters in 2) that are not regular expression characters

and what you end up with at 3) is a string that is regular expression that can be used to match against URIs. Essentially the URI template is transformed into a regular expression. In addition to matching against a URI what you get for free is the capturing of template variables in the URI template and corresponding strings in a matching URI (because group matching is used)!

A nice elegant solution that is only possible because URI templates are simple.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed