attribute

attributes are associated with elements and attribute lists. Note that the first construction here is translated to a block in the output XSLT. This is not a direct reflection of XSLT which only allows a block or a text content.

Syntax
<attribute> ::= "attribute" <quote> <qname> <quote> ( "namespace" <quote> <uri-reference> <quote> )? ( <quote> <alphanumeric string> <quote> | "{" ( <attribute block> )+ "}" )
Options
name a QName defining the name of this attribute.
namespace an (optional) namespace associated with this attribute.
alphanumeric string a string defining a simple text value for the named attribute.
Elements

One or more of the block elements. If any elements are defined then there should not be a string value defined and vice versa.

Examples

A simple element definition with two defined attributes.

element "script" { attribute "src" { value "concat( $inScriptsDir, $inScript )" } attribute "type" "text/javascript" }

would compile to

<element name="script"> <attribute name="src"> <value-of select="concat( $inScriptsDir, $inScript )"/> </attribute> <attribute name="type"> <text>text/javascript</text> </attribute> </element>

This shows the two forms of the "attribute" statement

In the above, note that if it is required to have an XPath expression (for example with a variable) then the "value" command must be used. For example

attribute "attrib-1" { value "$var-1" }

would translate to

<xsl:attribute name="attrib-1"> <xsl:value-of select="$var-1"/> </xsl:attribute>

Adding an explicit namespace is done with the namespace option.

element "div" { attribute "attrib-1" namespace "http://www.w3.org/1999/xhtml" "value-1" }

would translate to

<element name="div"> <attribute name="attrib-1" namespace="http://www.w3.org/1999/xhtml"> <text>value-1</text> </attribute> </element>
Errors

Having an attribute value and a block.

stylesheet { version "1.0" proc tryout { element "div" { attribute "attrib-1" "'value-1'" { value "'value-2'" } } } }

gives the error

**** (128) "attribute" cannot have simple/default value and enclosed templates in line 6 Remove either default/simple text or enclosed templates.

Having a misspelled keyword within the block

stylesheet { version "1.0" proc tryout { element "div" { attribute "attrib-1" { variable var_1 "'value-2'" valu "$var_1" } } } }

gives the errors

**** (106) Unexpected symbol "valu" in line 8 Check spelling, did you mean "value" ?
Copyright 2024 Hugh Field-Richards. All Rights Reserved.