blob: 931a1815607335add13d934d0420f2c3924f8184 [file] [log] [blame]
<html x:found="true" x:href="http://www.w3.org/TR/xslt#element-attribute" xmlns:x="urn:xslt-documentation" xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css" id="w3c">
a.external {
text-decoration: none;
border-bottom-color: blue;
border-bottom-style: dashed;
border-bottom-width: thin;
}
p.element-syntax {
border-color: black;
border-style: solid;
border-width: thin;
}
/* 2005-10-03: Modified to work with Java's HTML displaying capabilities */
/* Style for a "Recommendation" (http://www.w3.org/StyleSheets/TR/W3C-REC) */
/*
Copyright 1997-2003 W3C (MIT, ERCIM, Keio). All Rights Reserved.
The following software licensing rules apply:
http://www.w3.org/Consortium/Legal/copyright-software */
body {
margin: 0;
font-family: sans-serif;
color: black;
background-position: top left;
}
:link {
color: #00C;
background: transparent
}
:visited {
color: #609;
background: transparent
}
a:active {
color: #C00;
background: transparent
}
a:link img, a:visited img {
border-style: none
}
/* no border on img links */
a img {
color: white;
}
th, td {
/* ns 4 */
font-family: sans-serif;
}
h1, h2, h3, h4, h5, h6 {
text-align: left
}
/* background should be transparent, but WebTV has a bug */
h1, h2, h3 {
color: #005A9C;
}
h1 {
font: 170% sans-serif
}
h2 {
font: 140% sans-serif
}
h3 {
font: 100% sans-serif
}
h4 {
font: bold 100% sans-serif
}
h5 {
font: italic 100% sans-serif
}
h6 {
font: small-caps 100% sans-serif
}
.hide {
display: none
}
div.head {
margin-bottom: 1em
}
div.head h1 {
margin-top: 2em;
clear: both
}
div.head table {
margin-left: 2em;
margin-top: 2em
}
p.copyright {
margin-top: 15px;
font-size: 90%
}
p.copyright small {
font-size: small
}
/* hide from IE3 */
a[href]:hover {
background: #ffa
}
pre {
margin-left: 2em
}
/*
p {
margin-top: 0.6em;
margin-bottom: 0.6em;
}
*/
dt, dd {
margin-top: 0;
margin-bottom: 0
}
/* opera 3.50 */
dt {
font-weight: bold
}
pre, code {
font-family: monospace
}
/* navigator 4 requires this */
ul.toc {
list-style: disc; /* Mac NS has problem with 'none' */
list-style: none;
}
</style>
</head>
<body>
<h3>
<a name="creating-attributes"></a>Creating Attributes with
<code>xsl:attribute</code>
</h3>
<p class="element-syntax">
<a name="element-attribute"></a><code>&lt;!--
Category: instruction --&gt;<br>
&lt;xsl:attribute<br>
&nbsp;&nbsp;<b>name</b> = { <var>qname</var> }<br>
&nbsp;&nbsp;namespace = { <var>uri-reference</var> }&gt;<br>
&nbsp;&nbsp;&lt;!-- Content: <var>template</var> --&gt;<br>
&lt;/xsl:attribute&gt;</code>
</p>
<p>The <code>xsl:attribute</code> element can be used to add attributes to
result elements whether created by literal result elements in the stylesheet
or by instructions such as <code>xsl:element</code>. The <a class="external" href="http://www.w3.org/TR/xpath#dt-expanded-name">expanded-name</a> of the
attribute to be created is specified by a required <code>name</code>
attribute and an optional <code>namespace</code> attribute. Instantiating an
<code>xsl:attribute</code> element adds an attribute node to the containing
result element node. The content of the <code>xsl:attribute</code> element is
a template for the value of the created attribute.</p>
<p>The <code>name</code> attribute is interpreted as an <a class="external" href="http://www.w3.org/TR/xslt#dt-attribute-value-template">attribute value template</a>. It is an
error if the string that results from instantiating the attribute value
template is not a <a class="external" href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> or is the string
<code>xmlns</code>. 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 <code>namespace</code> attribute is not present, then the <a class="external" href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> is expanded into
an expanded-name using the namespace declarations in effect for the
<code>xsl:attribute</code> element, <i>not</i> including any default
namespace declaration.</p>
<p>If the <code>namespace</code> attribute is present, then it also is
interpreted as an <a class="external" href="http://www.w3.org/TR/xslt#dt-attribute-value-template">attribute value
template</a>. 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 <a class="external" href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> specified by the
<code>name</code> attribute is used as the local part of the expanded-name of
the attribute to be created.</p>
<p>XSLT processors may make use of the prefix of the <a class="external" href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> specified in the
<code>name</code> 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 <code>xmlns</code>, they must not do so. Thus, although it is not
an error to do:</p>
<pre>&lt;xsl:attribute name="xmlns:xsl" namespace="whatever"&gt;http://www.w3.org/1999/XSL/Transform&lt;/xsl:attribute&gt;</pre>
<p>it will not result in a namespace declaration being output.</p>
<p>Adding an attribute to an element replaces any existing attribute of that
element with the same expanded-name.</p>
<p>The following are all errors:</p>
<ul>
<li>
<p>Adding an attribute to an element after children have been added to
it; implementations may either signal the error or ignore the
attribute.</p>
</li>
<li>
<p>Adding an attribute to a node that is not an element;
implementations may either signal the error or ignore the attribute.</p>
</li>
<li>
<p>Creating nodes other than text nodes during the instantiation of the
content of the <code>xsl:attribute</code> element; implementations may
either signal the error or ignore the offending nodes.</p>
</li>
</ul>
<blockquote>
<b>NOTE:</b>When an <code>xsl:attribute</code> contains a text node with a
newline, then the XML output must contain a character reference. For
example,
<pre>&lt;xsl:attribute name="a"&gt;x
y&lt;/xsl:attribute&gt;</pre>
will result in the output
<pre>a="x&amp;#xA;y"</pre>
(or with any equivalent character reference). The XML output cannot be
<pre>a="x
y"</pre>
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.</blockquote>
<p id="w3c-footer" class="copyright">
<a href="http://www.w3.org/Consortium/Legal/ipr-notice.html#Copyright">Copyright</a>
&nbsp;&copy;&nbsp; 1999 <a href="http://www.w3.org">W3C</a><sup>&reg;</sup>
(<a href="http://www.lcs.mit.edu">MIT</a>, <a href="http://www.inria.fr/">INRIA</a>,
<a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved.
</p>
</body>
</html>