Révision 261587 sur Paramètres des instructions de traitement

  • Raccourci de la révision : XSLT/Paramètres_des_instructions_de_traitement
  • Titre de la révision : Paramètres des instructions de traitement
  • ID de la révision : 261587
  • Créé :
  • Créateur : Fredchat
  • Version actuelle ? Non
  • Commentaire /* Préparation de la traduction */
Étiquettes : 

Contenu de la révision

{{template.Traduction_en_cours("XSLT:PI Parameters")}}

Overview

XSLT supports the concept of passing parameters to a stylesheet when executing it. This has been possible for a while when using the XSLTProcessor in JavaScript. However when using an <?xml-stylesheet?> processing instruction (PI) there used to be no way to provide parameters.

To solve this two new PIs are implemented in Firefox 2 (see {{template.Anch("Supported versions")}} below for details), <?xslt-param?> and <?xslt-param-namespace?>. Both PIs can contain "pseudo attributes" the same way that the xml-stylesheet PI does.

The following document passes the two parameters "color" and "size" to the stylesheet "style.xsl".

<?xslt-param name="color" value="blue"?>
<?xslt-param name="size" select="2"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>

Note that these PIs have no effect when transformation is done using the XSLTProcessor object in JavaScript.

Processing instructions

The attributes in the xslt-param and xslt-param-namespace PIs are parsed using the rules defined in xml-stylesheet. Any unrecognized attributes must be ignored. Parsing of any attribute must not fail due to the presence of an unrecognized attribute as long as that attribute follows the syntax in xml-stylesheet.

Both the xslt-param and the xslt-param-namespace PIs must appear in the prolog of the document, i.e. before the first element tag. All PIs in the prolog must be honored, both ones occurring before and onces occurring after any xml-stylesheet PIs.

If there are multiple xml-stylesheet PIs the parameters apply to all stylesheets as a consequence of that all stylesheets are imported into a single stylesheet per the XSLT spec.reference? Note that multiple xml-stylesheet XSLT PIs are not supported in Firefox currently.

xslt-param

The xslt-param PI supports 4 attributes:

name
The local-name part of the parameter name. No syntax checking is done on the attribute, however if it is not a valid NCName it will never match any parameter in the stylesheet.
namespace
The namespace of the parameter name. No syntax checking is done on the attribute.
value
Contains the string value for the parameter. The value of the attribute is used as value for the parameter. The datatype will always be string.
select
An XPath expression for the parameter. The value of the attribute is parsed as an XPath expression. The result of evaluating the expression is used as value for the parameter.

If the name attribute is missing or empty the PI is ignored.

If the namespace attribute is missing or empty the null namespace is used.

It is not an error to specify a parameter name that does not exist in the stylesheet (or that is a variable in the stylesheet). The PI is simply ignored.

If both value and select are present or if neither value nor select are present the PI is ignored.

Note that value="..." is not strictly equal to select="'...'" since the value can contain both apostrophe and quote characters.

Examples

Set the parameter 'color' to the string 'red':

<?xslt-param name="color" value="red"?>

Set the parameter 'columns' to the number 2:

<?xslt-param name="columns" select="2"?>

Set the parameter 'books' to a nodeset containing all <book> elements in the null namespace:

<?xslt-param name="books" select="//book"?>

Set the parameter 'show-toc' to boolean true:

 <?xslt-param name="show-toc" select="true()"?>
The select attribute context

The following context is used to parse and evaluate the expression in the select attribute.

  • The context node is the node used as initial current node used when executing the stylesheet.
  • The context position is the position of the context node in the initial current node list used when executing the stylesheet.
  • The context size is the size of the initial current node list used when executing the stylesheet.
  • No variables are available.
  • The function library is the standard XPath function library.
  • The namespace declarations are determined by the xslt-param-namespace PIs, see below.

If the select attribute fails to parse or execute, the PI is ignored (in particular, it does not fall back to the value attribute).

xslt-param-namespace

The xslt-param-namespace uses two attributes:

prefix 
The prefix that is mapped.
namespace 
The namespace the prefix maps to.

An xslt-param-namespace PI affects the expression in the select attribute for all xslt-params following the PI. This applies even if there are other nodes such as comments or other PIs between the xslt-param-namespace and xslt-param PIs.

It is not an error for multiple PIs to use the same prefix, every new PI just changes what namespace the prefix maps to.

If prefix is missing, empty, or equals an invalid NCName, the PI is ignored.

If namespace is missing, the PI is ignored. If namespace is empty, the prefix mapping is removed.

Examples

Set the parameter 'books' to a nodeset containing all <book> elements in the 'http://www.example.org/myNamespace' namespace:

<?xslt-param-namespace prefix="my" namespace="http://www.example.org/myNamespace"?>
<?xslt-param name="books" select="//my:book"?>

Supported versions

Supported as of Firefox 2.0.0.1. The value attribute is supported in Firefox 2, but the select attribute crashes for some expressions in the 2.0 release.

Possible future developments

Should we allow any XSLT functions in the expression? document() seems useful, but it seems tricky to maintain the invariant that generate-id() should produce the same string for the same document.

Interwiki Language Links

{{ wiki.languages( { "en": "en/XSLT/PI_Parameters" } ) }}

Source de la révision

<p>
{{template.Traduction_en_cours("XSLT:PI Parameters")}}
</p>
<h3 name="Overview"> Overview </h3>
<p>XSLT supports the concept of passing parameters to a stylesheet when executing it. This has been possible for a while when using the <a href="fr/XSLTProcessor">XSLTProcessor</a> in JavaScript. However when using an <code>&lt;?xml-stylesheet?&gt;</code> processing instruction (PI) there used to be no way to provide parameters.
</p><p>To solve this two new PIs are implemented in <a href="fr/Firefox_2">Firefox 2</a> (see {{template.Anch("Supported versions")}} below for details), <code>&lt;?xslt-param?&gt;</code> and <code>&lt;?xslt-param-namespace?&gt;</code>. Both PIs can contain "pseudo attributes" the same way that the <code>xml-stylesheet</code> PI does.
</p><p>The following document passes the two parameters "color" and "size" to the stylesheet "style.xsl".
</p>
<pre class="eval">&lt;?xslt-param name="color" value="blue"?&gt;
&lt;?xslt-param name="size" select="2"?&gt;
&lt;?xml-stylesheet type="text/xsl" href="style.xsl"?&gt;
</pre>
<p>Note that these PIs have no effect when transformation is done using the <code>XSLTProcessor</code> object in JavaScript.
</p>
<h3 name="Processing_instructions"> Processing instructions </h3>
<p>The attributes in the <code>xslt-param</code> and <code>xslt-param-namespace</code> PIs are parsed using the rules defined in <a class="external" href="http://www.w3.org/TR/xml-stylesheet/">xml-stylesheet</a>. Any unrecognized attributes must be ignored. Parsing of any attribute must not fail due to the presence of an unrecognized attribute as long as that attribute follows the syntax in <code>xml-stylesheet</code>.
</p><p>Both the <code>xslt-param</code> and the <code>xslt-param-namespace</code> PIs must appear in the prolog of the document, i.e. before the first element tag. All PIs in the prolog must be honored, both ones occurring before and onces occurring after any <code>xml-stylesheet</code> PIs.
</p><p>If there are multiple <code>xml-stylesheet</code> PIs the parameters apply to all stylesheets as a consequence of that all stylesheets are imported into a single stylesheet per the XSLT spec.<span class="comment">reference?</span> Note that multiple <code>xml-stylesheet</code> XSLT PIs are not supported in Firefox currently.
</p>
<h4 name="xslt-param"> xslt-param </h4>
<p>The <code>xslt-param</code> PI supports 4 attributes:
</p>
<dl><dt> name
</dt><dd> The local-name part of the parameter name. No syntax checking is done on the attribute, however if it is not a valid <a class="external" href="http://www.w3.org/TR/REC-xml-names/#NT-NCName">NCName</a> it will never match any parameter in the stylesheet.
</dd><dt> namespace
</dt><dd> The namespace of the parameter name. No syntax checking is done on the attribute.
</dd><dt> value
</dt><dd> Contains the string value for the parameter. The value of the attribute is used as value for the parameter. The datatype will always be <i>string</i>.
</dd><dt> select
</dt><dd> An <a href="fr/XPath">XPath</a> expression for the parameter. The value of the attribute is parsed as an XPath expression. The result of evaluating the expression is used as value for the parameter.
</dd></dl>
<p>If the <b>name</b> attribute is missing or empty the PI is ignored.
</p><p>If the <b>namespace</b> attribute is missing or empty the null namespace is used.
</p><p>It is not an error to specify a parameter name that does not exist in the stylesheet (or that is a variable in the stylesheet). The PI is simply ignored.
</p><p>If both <b>value</b> and <b>select</b> are present or if neither <b>value</b> nor <b>select</b> are present the PI is ignored.
</p><p>Note that <code>value="..."</code> is not strictly equal to <code>select="'...'"</code> since the value can contain both apostrophe and quote characters.
</p>
<h5 name="Examples"> Examples </h5>
<p>Set the parameter 'color' to the string 'red':
</p>
<pre class="eval">&lt;?xslt-param name="color" value="red"?&gt;
</pre>
<p>Set the parameter 'columns' to the number 2:
</p>
<pre class="eval">&lt;?xslt-param name="columns" select="2"?&gt;
</pre>
<p>Set the parameter 'books' to a nodeset containing all <code>&lt;book&gt;</code> elements in the null namespace:
</p>
<pre class="eval">&lt;?xslt-param name="books" select="//book"?&gt;
</pre>
<p>Set the parameter 'show-toc' to boolean <code>true</code>:
</p>
<pre class="eval"> &lt;?xslt-param name="show-toc" select="true()"?&gt;
</pre>
<h5 name="The_select_attribute_context"> The select attribute context </h5>
<p>The following context is used to parse and evaluate the expression in the <b>select</b> attribute.
</p>
<ul><li> The context node is the node used as initial current node used when executing the stylesheet.
</li><li> The context position is the position of the context node in the initial current node list used when executing the stylesheet.
</li><li> The context size is the size of the  initial current node list used when executing the stylesheet.
</li><li> No variables are available.
</li><li> The function library is the standard XPath function library.
</li><li> The namespace declarations are determined by the <code>xslt-param-namespace</code> PIs, see below.
</li></ul>
<p>If the <b>select</b> attribute fails to parse or execute, the PI is ignored (in particular, it does not fall back to the <b>value</b> attribute).
</p>
<h4 name="xslt-param-namespace"> xslt-param-namespace </h4>
<p>The <code>xslt-param-namespace</code> uses two attributes:
</p>
<dl><dt> prefix </dt><dd> The prefix that is mapped.
</dd><dt> namespace </dt><dd> The namespace the prefix maps to.
</dd></dl>
<p>An <code>xslt-param-namespace</code> PI affects the expression in the <b>select</b> attribute for all <code>xslt-param</code>s following the PI. This applies even if there are other nodes such as comments or other PIs between the <code>xslt-param-namespace</code> and <code>xslt-param</code> PIs.
</p><p>It is not an error for multiple PIs to use the same prefix, every new PI just changes what namespace the prefix maps to.
</p><p>If <b>prefix</b> is missing, empty, or equals an invalid NCName, the PI is ignored.
</p><p>If <b>namespace</b> is missing, the PI is ignored. If <b>namespace</b> is empty, the prefix mapping is removed.
</p>
<h5 name="Examples_2"> Examples </h5>
<p>Set the parameter 'books' to a nodeset containing all <code>&lt;book&gt;</code> elements in the '<span class="plain">http://www.example.org/myNamespace</span>' namespace:
</p>
<pre class="eval">&lt;?xslt-param-namespace prefix="my" namespace="<span class="plain">http://www.example.org/myNamespace</span>"?&gt;
&lt;?xslt-param name="books" select="//my:book"?&gt;
</pre>
<h3 name="Supported_versions"> Supported versions </h3>
<p>Supported as of Firefox 2.0.0.1. The <b>value</b> attribute is supported in Firefox 2, but the <b>select</b> attribute crashes for some expressions in the 2.0 release.
</p>
<h3 name="Possible_future_developments"> Possible future developments </h3>
<p>Should we allow any XSLT functions in the expression? <code>document()</code> seems useful, but it seems tricky to maintain the invariant that <code>generate-id()</code> should produce the same string for the same document.
</p><p><span class="comment">Interwiki Language Links</span>
</p>{{ wiki.languages( { "en": "en/XSLT/PI_Parameters" } ) }}
Revenir à cette révision