The precede command


precede expression with query

Before each node N selected by the expression, the query is evaluated (with N as the context item), and its result is inserted as an immediate preceding sibling of N. Both the expression and the query must return nodes that are capable of having siblings: that is, element, text, comment, or processing instruction nodes. But if the query returns an atomic value, it is treated as a text node with the same string value.

The result of the operation can be inspected using the command show.


The command:

precede //img with <caption/>

inserts an empty <caption/> element before every img element in the document.

Given a source document:

<cities> <city name="Berlin" country="DE"/> <city name="Munich" country="DE"/> <city name="Paris" country="FR"/> <city name="Lyon" country="FR"/> <city name="Rome" country="IT"/> </cities>

The command:

precede //city[not(@country=preceding-sibling::*[1]/@country)] with <country name="{@country}"/>

produces the document:

<cities> <country name="DE"/> <city name="Berlin" country="DE"/> <city name="Munich" country="DE"/> <country name="FR"/> <city name="Paris" country="FR"/> <city name="Lyon" country="FR"/> <country name="IT"/> <city name="Rome" country="IT"/> </cities>