The prefix command


prefix expression with query

For every node N selected by the expression, the query is evaluated (with N as the context item), and its result is inserted as the first child of N. The expression must return nodes that are capable of having children (that is document or element nodes). 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.

As a special case, if the query returns an attribute node, it is added as an attribute of the selected element, replacing any existing attribute with the same name.

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


The command:

prefix //section with <head>{data(@title)}</head>

copies the value of the title attribute of every section element into a new head element appearing as the first child of the section.

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:

prefix //city with <country name="{@country}"/>

produces the document:

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

With the same document, the command:

prefix //city with attribute{'id'}{count(preceding-sibling::*)+1}


<cities> <city name="Berlin" country="DE" id="1"/> <city name="Munich" country="DE" id="2"/> <city name="Paris" country="FR" id="3"/> <city name="Lyon" country="FR" id="4"/> <city name="Rome" country="IT" id="5"/> </cities>