7.1 Creating Elements and Attributes
Creating Elements and Attributes
Literal Result Elements[top]
Literal Result Elements
In a template, an element in the stylesheet that does not belong to
the XSLT namespace and that is not an extension element (see [Extension Elements]) is instantiated to create an element node
with the same expanded-name. The content
of the element is a template, which is instantiated to give the
content of the created element node. The created element node will
have the attribute nodes that were present on the element node in the
stylesheet tree, other than attributes with names in the XSLT
namespace.
The created element node will also have a copy of the namespace
nodes that were present on the element node in the stylesheet tree
with the exception of any namespace node whose string-value is the
XSLT namespace URI (http://www.w3.org/1999/XSL/Transform), a
namespace URI declared as an extension namespace (see [Extension Elements]), or a namespace URI designated as an
excluded namespace. A namespace URI is designated as an excluded
namespace by using an exclude-result-prefixes attribute
on an xsl:stylesheet element or an
xsl:exclude-result-prefixes attribute on a literal result
element. The value of both these attributes is a whitespace-separated
list of namespace prefixes. The namespace bound to each of the
prefixes is designated as an excluded namespace. It is an error if
there is no namespace bound to the prefix on the element bearing the
exclude-result-prefixes or
xsl:exclude-result-prefixes attribute. The default
namespace (as declared by xmlns) may be designated as an
excluded namespace by including #default in the list of
namespace prefixes. The designation of a namespace as an excluded
namespace is effective within the subtree of the stylesheet rooted at
the element bearing the exclude-result-prefixes or
xsl:exclude-result-prefixes attribute;
a subtree rooted at an xsl:stylesheet element
does not include any stylesheets imported or included by children
of that xsl:stylesheet element.
NOTE:
When a stylesheet uses a namespace declaration only for the
purposes of addressing the source tree, specifying the prefix in the
exclude-result-prefixes attribute will avoid superfluous
namespace declarations in the result tree.
The value of an attribute of a literal result element is
interpreted as an Attribute Value Template: it can contain expressions contained
in curly braces ({}).
A namespace URI in the stylesheet tree that is being used to
specify a namespace URI in the result tree is called a literal
namespace URI. This applies to:
-
the namespace URI in the expanded-name of a literal
result element in the stylesheet
-
the namespace URI in the expanded-name of an attribute
specified on a literal result element in the stylesheet
-
the string-value of a namespace node on a literal result
element in the stylesheet
<
namespace-alias
stylesheet-prefix=
prefix
result-prefix=
prefix
>
<-- Content: -->
<
/namespace-alias>
A stylesheet can use the
xsl:namespace-alias element to declare that one namespace
URI is an alias for another namespace URI. When
a Literal Namespace URI has been declared to be an alias for another namespace
URI, then the namespace URI in the result tree will be the namespace
URI that the literal namespace URI is an alias for, instead of the
literal namespace URI itself. The xsl:namespace-alias
element declares that the namespace URI bound to the prefix specified
by the stylesheet-prefix attribute is an alias for the
namespace URI bound to the prefix specified by the
result-prefix attribute. Thus, the
stylesheet-prefix attribute specifies the namespace URI
that will appear in the stylesheet, and the
result-prefix attribute specifies the corresponding
namespace URI that will appear in the result tree. The default
namespace (as declared by xmlns) may be specified by
using #default instead of a prefix. If a namespace URI
is declared to be an alias for multiple different namespace URIs, then
the declaration with the highest Import Precedence is used. It is
an error if there is more than one such declaration. An XSLT
processor may signal the error; if it does not signal the error, it
must recover by choosing, from amongst the declarations with the
highest import precedence, the one that occurs last in the
stylesheet.
When literal result elements are being used to create element,
attribute, or namespace nodes that use the XSLT namespace URI, the
stylesheet must use an alias. For example, the stylesheet
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias">
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:template match="/">
<axsl:stylesheet>
<xsl:apply-templates/>
</axsl:stylesheet>
</xsl:template>
<xsl:template match="block">
<axsl:template match="{.}">
<fo:block><axsl:apply-templates/></fo:block>
</axsl:template>
</xsl:template>
</xsl:stylesheet>
will generate an XSLT stylesheet from a document of the form:
<elements>
<block>p</block>
<block>h1</block>
<block>h2</block>
<block>h3</block>
<block>h4</block>
</elements>
NOTE:
It may be necessary also to use aliases for namespaces other
than the XSLT namespace URI. For example, literal result elements
belonging to a namespace dealing with digital signatures might cause
XSLT stylesheets to be mishandled by general-purpose security
software; using an alias for the namespace would avoid the possibility
of such mishandling.
Creating Elements with xsl:element[top]
Creating Elements with xsl:element
<
element
name=
{
qname
}
namespace=
{
uri-reference
}
use-attribute-sets=
qnames
>
<-- Content: -->
<
/element>
The xsl:element element allows an element to be
created with a computed name. The expanded-name of the
element to be created is specified by a required name
attribute and an optional namespace attribute. The
content of the xsl:element element is a template for the
attributes and children of the created element.
The name attribute is interpreted as an Attribute Value Template.
It is an error if the string that results from instantiating the
attribute value template is not a QName. An XSLT processor may signal
the error; if it does not signal the error, then it must recover
by making the the result of instantiating the xsl:element
element be the sequence of nodes created by instantiating
the content of the xsl:element element, excluding
any initial attribute nodes. If the namespace attribute is
not present then the QName is
expanded into an expanded-name using the namespace declarations in
effect for the xsl:element element, including any default
namespace declaration.
If the namespace attribute is present, then it also is
interpreted as an Attribute Value Template. The string that results from instantiating
the attribute value template should be a URI reference. It is not an
error if the string is not a syntactically legal URI reference. If
the string is empty, then the expanded-name of the element has a null
namespace URI. Otherwise, the string is used as the namespace URI of
the expanded-name of the element to be created. The local part of the
QName specified by the
name attribute is used as the local part of the
expanded-name of the element to be created.
XSLT processors may make use of the prefix of the QName specified in the
name attribute when selecting the prefix used for
outputting the created element as XML; however, they are not required
to do so.
Creating Attributes with xsl:attribute[top]
Creating Attributes with xsl:attribute
<
attribute
name=
{
qname
}
namespace=
{
uri-reference
}
>
<-- Content: -->
<
/attribute>
The xsl:attribute element can be used to add
attributes to result elements whether created by literal result
elements in the stylesheet or by instructions such as
xsl:element. The expanded-name of the
attribute to be created is specified by a required name
attribute and an optional namespace attribute.
Instantiating an xsl:attribute element adds an attribute
node to the containing result element node. The content of the
xsl:attribute element is a template for the value of the
created attribute.
The name attribute is interpreted as an Attribute Value Template.
It is an error if the string that results from instantiating the
attribute value template is not a QName or is the string
xmlns. An XSLT processor may signal the error; if it
does not signal the error, it must recover by not adding the attribute
to the result tree. If the namespace attribute is not
present, then the QName is
expanded into an expanded-name using the namespace declarations in
effect for the xsl:attribute element, not
including any default namespace declaration.
If the namespace attribute is present, then it also is
interpreted as an Attribute Value Template. The string that results from instantiating
it should be a URI reference. It is not an error if the string is not
a syntactically legal URI reference. If the string is empty, then the
expanded-name of the attribute has a null namespace URI. Otherwise,
the string is used as the namespace URI of the expanded-name of the
attribute to be created. The local part of the QName specified by the
name attribute is used as the local part of the
expanded-name of the attribute to be created.
XSLT processors may make use of the prefix of the QName specified in the
name attribute when selecting the prefix used for
outputting the created attribute as XML; however, they are not
required to do so and, if the prefix is xmlns, they must
not do so. Thus, although it is not an error to do:
<xsl:attribute name="xmlns:xsl" namespace="whatever">http://www.w3.org/1999/XSL/Transform</xsl:attribute>
it will not result in a namespace declaration being output.
Adding an attribute to an element replaces any existing attribute
of that element with the same expanded-name.
The following are all errors:
-
Adding an attribute to an element after children have been
added to it; implementations may either signal the error or ignore the
attribute.
-
Adding an attribute to a node that is not an element;
implementations may either signal the error or ignore the
attribute.
-
Creating nodes other than text nodes during the
instantiation of the content of the xsl:attribute
element; implementations may either signal the error or ignore the
offending nodes.
NOTE:
When an xsl:attribute contains a text node with
a newline, then the XML output must contain a character reference.
For example,
<xsl:attribute name="a">x
y</xsl:attribute>
will result in the output
a="x
y"
(or with any equivalent character reference). The XML output cannot
be
a="x
y"
This is because XML 1.0 requires newline characters in attribute
values to be normalized into spaces but requires character references
to newline characters not to be normalized. The attribute values in
the data model represent the attribute value after normalization. If
a newline occurring in an attribute value in the tree were output as a
newline character rather than as character reference, then the
attribute value in the tree created by reparsing the XML would contain
a space not a newline, which would mean that the tree had not been
output correctly.
Named Attribute Sets[top]
Named Attribute Sets
<
attribute-set
name=
qname
use-attribute-sets=
qnames
>
<-- Content: attribute*-->
<
/attribute-set>
The xsl:attribute-set element defines a named set of
attributes. The name attribute specifies the name of the
attribute set. The value of the name attribute is a QName, which is expanded as described
in [Qualified Names]. The content of the xsl:attribute-set
element consists of zero or more xsl:attribute elements
that specify the attributes in the set.
Attribute sets are used by specifying a
use-attribute-sets attribute on xsl:element,
xsl:copy (see [Copying]) or
xsl:attribute-set elements. The value of the
use-attribute-sets attribute is a whitespace-separated
list of names of attribute sets. Each name is specified as a QName, which is expanded as described
in [Qualified Names]. Specifying a
use-attribute-sets attribute is equivalent to adding
xsl:attribute elements for each of the attributes in each
of the named attribute sets to the beginning of the content of the
element with the use-attribute-sets attribute, in the
same order in which the names of the attribute sets are specified in
the use-attribute-sets attribute. It is an error if use
of use-attribute-sets attributes on
xsl:attribute-set elements causes an attribute set to
directly or indirectly use itself.
Attribute sets can also be used by specifying an
xsl:use-attribute-sets attribute on a literal result
element. The value of the xsl:use-attribute-sets
attribute is a whitespace-separated list of names of attribute sets.
The xsl:use-attribute-sets attribute has the same effect
as the use-attribute-sets attribute on
xsl:element with the additional rule that attributes
specified on the literal result element itself are treated as if they
were specified by xsl:attribute elements before any
actual xsl:attribute elements but after any
xsl:attribute elements implied by the
xsl:use-attribute-sets attribute. Thus, for a literal
result element, attributes from attribute sets named in an
xsl:use-attribute-sets attribute will be added first, in
the order listed in the attribute; next, attributes specified on the
literal result element will be added; finally, any attributes
specified by xsl:attribute elements will be added. Since
adding an attribute to an element replaces any existing attribute of
that element with the same name, this means that attributes specified
in attribute sets can be overridden by attributes specified on the
literal result element itself.
The template within each xsl:attribute element in an
xsl:attribute-set element is instantiated each time the
attribute set is used; it is instantiated using the same current node
and current node list as is used for instantiating the element bearing
the use-attribute-sets or
xsl:use-attribute-sets attribute. However, it is the
position in the stylesheet of the xsl:attribute element
rather than of the element bearing the use-attribute-sets
or xsl:use-attribute-sets attribute that determines which
variable bindings are visible (see [Variables and Parameters]); thus,
only variables and parameters declared by Top-level xsl:variable and
xsl:param elements are visible.
The following example creates a named attribute set
title-style and uses it in a template rule.
<xsl:template match="chapter/heading">
<fo:block quadding="start" xsl:use-attribute-sets="title-style">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:attribute-set name="title-style">
<xsl:attribute name="font-size">12pt</xsl:attribute>
<xsl:attribute name="font-weight">bold</xsl:attribute>
</xsl:attribute-set>
Multiple definitions of an attribute set with the same
expanded-name are merged. An attribute from a definition that has
higher Import Precedence
takes precedence over an attribute from a definition that has lower
Import Precedence. It
is an error if there are two attribute sets that have the same
expanded-name and equal import precedence and that both contain
the same attribute, unless there is a definition of the attribute set
with higher Import Precedence that also contains the attribute. An XSLT
processor may signal the error; if it does not signal the error, it
must recover by choosing from amongst the definitions that specify the
attribute that have the highest import precedence the one that was
specified last in the stylesheet. Where the attributes in an
attribute set were specified is relevant only in merging the
attributes into the attribute set; it makes no difference when the
attribute set is used.
|