I’m trying to use XSLT to append context-param as last sibling. There’s no common parent element so the task is a little bit harder.
I’d like to append following element:
<context-param>
<param-name>miku</param-name>
<param-value>kawaii</param-value>
</context-param>
as the last context-param element (eg. all context-param elements must be next to each other, they can’t be scattered anywhere in xml) in following xml:
<web-app>
<not_interesting_element1/>
<not_interesting_element2/>
<context-param>
<param-name>not_interesting_param_key1</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>not_interesting_param_key2</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>parameterThatsGuaranteedToBeHere</param-name>
<param-value>someValue</param-value>
</context-param>
<not_interesting_element3/>
<not_interesting_element4/>
<!-- ... servlets, ... -->
</web-app>
The result should look like this:
<web-app>
<not_interesting_element1/>
<not_interesting_element2/>
<context-param>
<param-name>not_interesting_param_key1</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>not_interesting_param_key2</param-name>
<param-value>kawaii</param-value>
</context-param>
<context-param>
<param-name>parameterThatsGuaranteedToBeHere</param-name>
<param-value>someValue</param-value>
</context-param>
<context-param>
<param-name>miku</param-name>
<param-value>kawaii</param-value>
</context-param>
<not_interesting_element3/>
<not_interesting_element4/>
<!-- ... servlets, ... -->
</web-app>
How could I do it please?
This transformation:
when applied to the provided XML document:
produces the wanted, correct result:
Explanation:
The identity rule copies every node “as-is”.
There is a single template, overriding the identity template. This template matches the last
context-paramelement of allcontext-paramelements that are children of their parent.In the overriding template two actions are performed; the current node is copied by a call to the identity rule; then the element to be appended is copied to the output. For convenience and flexibility, we assume that the element to be appended is passed as a parameter to the transformation.