The replace command
Syntax:
replace expression with queryThe nodes (elements or attributes) selected by the given expression are replaced by the result of evaluating the query.
Note that it is the entire node that is replaced, not just its content. To replace the content of an element or attribute node, rather than replacing the entire node, use the command update.
The result of the operation can be inspected using the command show.
Examples
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:
replace //city[@country="DE"] with <stadt @Name="{@name}"/>produces the document:
<cities> <stadt Name="Berlin"/> <stadt Name="Munich"/> <city name="Paris" country="FR"/> <city name="Lyon" country="FR"/> <city name="Rome" country="IT"/> </cities>Given a source document:
<cities> <city><name>Berlin</name><country>DE</country></city> <city><name>Munich</name><country>DE</country></city> <city><name>Paris</name><country>FR</country></city> <city><name>Lyon</name><country>FR</country></city> <city><name>Rome</name><country>IT</country></city> </cities>The command:
replace //city/name[.="Munich"]/text() with "München"produces the document:
<cities> <city><name>Berlin</name><country>DE</country></city> <city><name>München</name><country>DE</country></city> <city><name>Paris</name><country>FR</country></city> <city><name>Lyon</name><country>FR</country></city> <city><name>Rome</name><country>IT</country></city> </cities>With the same source document, the command:
replace //name withproduces the document:
<cities> <city><cityName><name>Berlin</name></cityName><country>DE</country></city> <city><cityName><name>Munich</name></cityName><country>DE</country></city> <city><cityName><name>Paris</name></cityName><country>FR</country></city> <city><cityName><name>Lyon</name></cityName><country>FR</country></city> <city><cityName><name>Rome</name></cityName><country>IT</country></city> </cities>Note that in this expression, {.}
follows the XQuery rules rather than
the XSLT rules: it copies the whole element, rather than extract its string value.