Node Creation XPath Functions

From W3C XForms Group Wiki (Public)
Jump to: navigation, search

Node Creation XPath Functions

The xf:element() function

element() xf:element($qname as xs:anyAtomicType, $content as node()*)

This function returns a new XML element with the qualified name provided. If the qualified name is not of type xs:QName the namespace of the element is resolved using the in-scope namespaces (the in-scope default namespace is used for qualified names that are unprefixed). The second, optional argument can take a node-set specifying attributes and content for the new element.


<!-- Insert an element called "value" as a child of element "section" -->
<xforms:insert context="section" origin="xf:element('value')"/>


<!-- Insert an element called "value" as a child of element "section", with an attribute and text from an instance -->
<xforms:insert context="section" origin="xf:element('value', xf:attribute('id', 'my-value') | ../name/text())"/>
<!-- Insert an element called "value" as a child of element "section", with an attribute and text content (requires an XPath 2.0 enabled XForms implementation)-->
<xforms:insert context="section" origin="xf:element('value', (xf:attribute('id', 'my-value'), 'John'))"/>


Note: The element will behave as if they have read-only MIP attached which can't be removed

Note: When an XPath 1.0 processor is used the first argument is always a string

Note: When support for XPath 2.0 is added the type of $content should become a sequence of items

Note:This function is already implemented in Orbeon

Note: When an XPath 1.0 processor is used second argument isn't as useful as in XPath 2.0 bcz. the nodes are always in document order

The xf:attribute() function

attribute() xf:attribute($qname as xs:anyAtomicType, $value as xs:anyAtomicType?)

This function returns a new XML attribute with the qualified name provided as first argument. If the qualified name is not of type xs:QName the namespace of the element is resolved using the in-scope namespaces (the in-scope default namespace is used for qualified names that are unprefixed). The second argument is an optional value for the attribute with the empty string as default


<!-- Add an attribute called "id" with a value of "first-name" to element "section" -->
<xforms:insert context="section" origin="xf:attribute('id', 'first-name')"/>

Note: The attribute will behave as if they have read-only MIP attached which can't be removed

Note: When an XPath 1.0 processor is used the arguments are of type string

Note:This function is already implemented in Orbeon



Related XPath functions: Parse_Serialize_functions