home contribute faq download

FunctX XSLT Functions

functx:add-or-update-attributes

Adds attributes to XML elements

Google
Webdatypic.com

Description

The functx:add-or-update-attributes function takes one or more XML element nodes, along with a sequence of attribute names and a sequence of attribute values, and returns a newly constructed element with those attributes added. The attribute names and values are positionally related, i.e. the first attribute name corresponds to the first attribute value, the second name to the second value, etc. It leaves all the original attributes and content of the element, except that if it already has an attribute with the same name of one of $attrNames, its value is updated to match the corresponding value in $attrValues. This is in contrast with the functx:add-attributes function, which will not update the value.

Each name must be specified as an xs:QName value. QNames can be constructed with calls to the xs:QName type constructor or the fn:QName function, as shown in the examples.

Arguments and Return Type

NameTypeDescription
$elements element()* the element(s) to which you wish to add the attribute
$attrNames xs:QName* the name(s) of the attribute(s) to add
$attrValues xs:anyAtomicType* the value(s) of the attribute(s) to add
return value element()*

XSLT Function Declaration

See XQuery definition.
<xsl:function name="functx:add-or-update-attributes" as="element()?"
              xmlns:functx="http://www.functx.com">
  <xsl:param name="elements" as="element()*"/>
  <xsl:param name="attrNames" as="xs:QName*"/>
  <xsl:param name="attrValues" as="xs:anyAtomicType*"/>

  <xsl:for-each select="$elements">
    <xsl:copy>
      <xsl:for-each select="$attrNames">
        <xsl:variable name="seq" select="position()"/>
        <xsl:attribute name="{.}"
                       namespace="{namespace-uri-from-QName(.)}"
                       select="$attrValues[$seq]"/>
      </xsl:for-each>
      <xsl:copy-of select="@*[not(node-name(.) = $attrNames)]|node()"/>
    </xsl:copy>
  </xsl:for-each>

</xsl:function>

Examples

<xsl:stylesheet xmlns:new="http://new">...
<xsl:variable name="in-xml" as="item()*">
<in-xml>
   <a>x</a>
   <b att1="x">x</b>
</in-xml>
</xsl:variable>
XPath ExampleResults
functx:add-or-update-attributes(
   $in-xml/a, xs:QName('att1'), 1)
<a att1="1">x</a>
functx:add-or-update-attributes(
   $in-xml/a,
   (xs:QName('att1'),xs:QName('att2')),
   (1,2))
<a att1="1" att2="2">x</a>
functx:add-or-update-attributes(
   $in-xml/b,
   (xs:QName('att1'),xs:QName('att2')),
   (1,2))
<b att1="1" att2="2">x</b>
functx:add-or-update-attributes(
   $in-xml/a,
   xs:QName('new:att1'),
   1)
<a xmlns:new="http://new" new:att1="1">x</a>
functx:add-or-update-attributes(
   $in-xml/a,
   QName('http://new','new:att1'),
   1)
<a xmlns:new="http://new" new:att1="1">x</a>

See Also

functx:add-attributesAdds attributes to XML elements
functx:update-attributesUpdates the attribute value of an XML element
functx:copy-attributesCopies attributes from one element to another
functx:remove-attributesRemoves attributes from an XML element, based on name

History

Published OnLast UpdatedContributor(s)
2006-06-272007-08-08Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Datypic XSLT Services

Recommended Reading:

XQuery