saxon:new-attribute
Returns a new attribute node, with given name and content.
new-attribute($name as union(xs:QName, xs:string), $content as xs:string?) ➔ attribute()
Arguments | |||
| $name | union(xs:QName, xs:string) | The name of the new attribute |
| $content | xs:string? | The content of the new attribute |
Result | attribute() |
Namespace
http://saxon.sf.net/
Notes on the Saxon implementation
Available since Saxon 10.
Details
This function constructs a new parentless attribute node.
The name of the attribute may be supplied in a number of ways:
- The first argument may be an
xs:QName
, in which case the attribute's name will have the prefix, namespace URI, and local name supplied in this QName. - The first argument may be an
xs:string
, that conforms to the rules for a validxs:NCName
. The attribute's name will have this local part, with no namespace URI or prefix. - The first argument may be an
xs:string
in the formatQ{uri}local
. The attribute's local name and namespace URI will be taken from this value, and will have a system-allocated prefix. - The first argument may be an
xs:string
in the formatprefix:local
. In this case the the prefix must be declared in the static context of the function call, and the element's name will use this prefix and local name, together with the namespace URI associated with this prefix in the static context.
If the attribute name has a URI but no prefix, then the system will allocate an arbitrary prefix. If the attribute name has a prefix but no URI, then the prefix will be ignored.
The content of the attribute node (that is, the string value of the node) is formed by evaluating the second argument. If this is an empty sequence, the string value will be a zero-length string.
The type annotation of the new element node will be xs:untypedAtomic
.
The function imposes rules preventing the misuse of reserved names such as
"xml" and "xmlns", in the same way as the xsl:attribute
instruction
in XSLT, or the attribute constructor expression in XQuery. The error codes
used are those defined in XSLT 3.0.
If the function is called twice with the same arguments, it is unpredictable whether it returns the same attribute node or different attribute nodes from the two invocations.
Notes:
-
These rules are similar to the XQuery rules for the
element {...}
expression. However, there are some differences. Most notably, the XSLT rules allow multiple attribute nodes with the same name to appear in the content sequence (the last one wins). Furthermore, the error codes used for invalid conditions (such as the presence of maps or functions or conflicting namespace nodes in the content) are those given in the XSLT 3.0 specification. -
The XSLT/XQuery rules for constructing simple content do not apply. The value must be supplied as a string, or as a value that is converted to a string by virtue of the function conversion rules.
-
Since the declared type of the first argument is namespace sensitive, error
XPTY0117
will be raised if an untyped atomic value (or an untyped node) is supplied as the actual argument. Conversion to a string should therefore be done explicitly. For example, to convert the element<prop name="x" value="y"/>
to the attribute nodex="y"
, usesaxon:new-attribute(string(@name), string(@value))