Specifying the position to duplicate to

Mark Birbeck's picture

It is also possible to indicate a position to copy the last node in the list to. The at attribute indicates which node in the nodeset to place the copied node after:

<xf:insert nodeset="list/y" at="1" />

The result would be:

<list>
  <y>y1</y>
  <y>y3</y>
  <y>y2</y>
  <y>y3</y>
</list>

To place the copied node before the node referred to by @at, use the position attribute with a value of before:

<xf:insert nodeset="list/y" at="1" position="before" />

The result would be:

<list>
  <y>y3</y>
  <y>y1</y>
  <y>y2</y>
  <y>y3</y>
</list>

Note that the default value for @position is after, so the following are equivalent:

<xf:insert nodeset="list/y" at="1" />
<xf:insert nodeset="list/y" at="1" position="after" />

The @at attribute is actually an XPath expression, so it is possible to calculate an insert position at run-time. The expression is evaluated in the context of the first node in the @nodeset, as illustrated by the following example:

<xf:insert nodeset="list/y" at="count(../y) - 1" position="before" />

The result would be:

<list>
  <y>y1</y>
  <y>y3</y>
  <y>y2</y>
  <y>y3</y>
</list>