Description
The functx:replace-element-values function takes a sequence of elements and updates their values with the values specified in $values. It keeps all the original attributes of the elements in tact. This is useful if you would like to perform an operation on a sequence of elements, for example, doubling them or taking a substring, without eliminating the elements themselves. The two argument sequences are positionally related; i.e. the first element in $elements takes on the first value in $values, the second element takes on the second value, etc.
Note: this function is intended to change the way elements appear in the results of a query, not to update them in an XML database. To update your XML database, you should use the implementation-specific update functions of your processor. (There is no standard XQuery update syntax yet.)
Arguments and Return Type| Name | Type | Description |
$elements |
element()* |
the elements whose content you wish to replace |
$values |
xs:anyAtomicType* |
the replacement values |
| return value |
element()* |
XSLT Function Declaration| See XQuery definition. | | XSLT 2.0 Syntax: |
|---|
<xsl:function name="functx:replace-element-values" as="element()*"
xmlns:functx="http://www.functx.com" >
<xsl:param name="elements" as="element()*"/>
<xsl:param name="values" as="xs:anyAtomicType*"/>
<xsl:for-each select="$elements">
<xsl:variable name="seq" select="position()"/>
<xsl:element name="{node-name(.)}">
<xsl:sequence select="@*, $values[$seq]"/>
</xsl:element>
</xsl:for-each>
</xsl:function>
|
Examples<xsl:variable name="in-xml" as="item()*"> | | <in-xml>
<price num="1">12</price>
<price num="2">20</price>
<price num="3">5</price>
</in-xml> |
| </xsl:variable> |
| XSLT Example | Results |
|---|
functx:replace-element-values(
$in-xml/price,
for $p in $in-xml/price
return $p * 2)
|
<price num="1">24</price>
<price num="2">40</price>
<price num="3">10</price>
|
for $p in $in-xml/price
return functx:replace-element-values(
$p,concat($p,'.0'))
|
<price num="1">12.0</price>
<price num="2">20.0</price>
<price num="3">5.0</price>
|
History |
|