« Programmation XML/XSLT » : différence entre les versions

Contenu supprimé Contenu ajouté
Aucun résumé des modifications
Ligne 1 :
{{Programmation XML}}
 
== Syntaxe de base ==
{{w|XSLT}} signifie ''{{lang|en|Extended Stylesheet Language Transformations}}''. Il s'agit d'un langage qui permet de transformer un document XML en un autre format en s'appuyant sur [[../XPath/]]. On peut par exemple créer une page [[HTML]], un fichier PDF ou autre à partir d'un fichier XML.
 
Ligne 8 :
{{remarque|1=Les exemples ci-dessous sont vérifiables avec l'outil http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog}}
 
=== Exemple ===
{|class="wikitable"
! Attributs !! Note
|-
| <code>match</code> || désigne le nœud auquel s'applique le code.
|-
| <code>select</code> || ce qu'il faut présenter dans le résultat.
|-
| <code>copy</code> || copie le nœud sélectionné
|-
| <code>order</code> || classe les résultats ("ascending" ou "descending")
|-
| <code>case-order </code> || classe les résultats en distinguant les lettres capitales ("upper-first" ou
"lower-first")
|}
 
== Exemples ==
{|class="wikitable"
|
Ligne 93 ⟶ 78 :
?>
</source>
 
 
== Syntaxe avancée ==
=== Attributs ===
{|class="wikitable"
! Attributs !! Note
|-
| <code>match</code> || désigne le nœud auquel s'applique le code.
|-
| <code>select</code> || ce qu'il faut présenter dans le résultat.
|-
| <code>order</code> || classe les résultats ("ascending" ou "descending")
|-
| <code>case-order</code> || classe les résultats en distinguant les lettres capitales ("upper-first" ou
"lower-first")
|}
 
=== Éléments ===
{| TableGrid border="1" cellspacing="0" cellpadding="0" width="583"
|-
| valign="top" | <b>Éléments XSL</b><ref>http://www.w3schools.com/xsl/xsl_w3celementref.asp</ref><ref>http://www.w3.org/TR/xslt#element-syntax-summary)</ref> || valign="top" | <b>Description</b>
| valign="top" | <b>Catégorie</b>
|-
| valign="top" | apply-imports
| valign="top" | Applies a template rule from an imported stylesheet
| valign="top" | instruction
|-
| valign="top" | apply-templates
| valign="top" | Applies a template rule to the current element or to the current element's child nodes
| valign="top" | instruction
|-
| valign="top" | attribute || valign="top" | Adds an attribute
| valign="top" | instruction
|-
| valign="top" | attribute-set
| valign="top" | Defines a named set of attributes
| valign="top" | top-level-element
|-
| valign="top" | call-template
| valign="top" | Calls a named template
| valign="top" | instruction
|-
| valign="top" | choose
| valign="top" | Used in conjunction with &lt;when&gt; and &lt;otherwise&gt; to
express multiple conditional tests
| valign="top" | instruction
|-
| valign="top" | comment
| valign="top" | Creates a comment node in the result tree
| valign="top" | instruction
|-
| valign="top" | copy
| valign="top" | Creates a copy of the current node<br>(without child nodes and attributes)
| valign="top" | instruction
|-
| valign="top" | copy-of
| valign="top" | Creates a copy of the current node<br>(with child nodes and attributes)
| valign="top" | instruction
|-
| valign="top" | decimal-format
| valign="top" | Defines the characters and symbols to be used when converting numbers into strings, with the format-number() function
| valign="top" | top-level-element
|-
| valign="top" | element
| valign="top" | Creates an element node in the output document
| valign="top" | instruction
|-
| valign="top" | fallback
| valign="top" | Specifies an alternate code to run if&nbsp; the processor does not support an XSLT element
| valign="top" | instruction
|-
| valign="top" | for-each
| valign="top" | Loops through each node in a specified node set
| valign="top" | instruction
|-
| valign="top" | if
| valign="top" | Contains a template that will be applied only if a specified condition is true
| valign="top" | instruction
|-
| valign="top" | import
| valign="top" | Imports the contents of one stylesheet into another.<br><b>Note:</b> An imported stylesheet has lower precedence than the importing stylesheet
| valign="top" | top-level-element
|-
| valign="top" | include
| valign="top" | Includes the contents of one stylesheet into another.<br><b>Note:</b> An included stylesheet has the same precedence as the including stylesheet
| valign="top" | top-level-element
|-
| valign="top" | key
| valign="top" | Declares a named key that can be used in the stylesheet with the key() function
| valign="top" | top-level-element
|-
| valign="top" | message
| valign="top" | Writes a message to the output (used to report errors)
| valign="top" | instruction
|-
| valign="top" | namespace-alias
| valign="top" | Replaces a namespace in the stylesheet to a different namespace in the output
| valign="top" | top-level-element
|-
| valign="top" | number
| valign="top" | Determines the integer position of the current node and formats a number
| valign="top" | instruction
|-
| valign="top" | otherwise
| valign="top" | Specifies a default action for the &lt;choose&gt; element
| valign="top" | instruction
|-
| valign="top" | output
| valign="top" | Defines the format of the output document
| valign="top" | top-level-element
|-
| valign="top" | param
| valign="top" | Declares a local or global parameter
| valign="top" | top-level-element
|-
| valign="top" | preserve-space
| valign="top" | Defines the elements for which white space should be preserved
| valign="top" | top-level-element
|-
| valign="top" | processing-instruction
| valign="top" | Writes a processing instruction to the output
| valign="top" | instruction
|-
| valign="top" | sort || valign="top" | Sorts the output
| valign="top" | instruction
|-
| valign="top" | strip-space
| valign="top" | Defines the elements for which white space should be removed
| valign="top" | top-level-element
|-
| valign="top" | stylesheet
| valign="top" | Defines the root element of a stylesheet
| valign="top" | top-level-element
|-
| valign="top" | template
| valign="top" | Rules to apply when a specified node is matched
| valign="top" | top-level-element
|-
| valign="top" | text
| valign="top" | Writes literal text to the output
| valign="top" | instruction
|-
| valign="top" | transform
| valign="top" | Defines the root element of a stylesheet
| valign="top" | top-level-element
|-
| valign="top" | value-of
| valign="top" | Extracts the value of a selected node
| valign="top" | instruction
|-
| valign="top" | variable
| valign="top" | Declares a local or global variable
| valign="top" | top-level-element or instruction
|-
| valign="top" | when
| valign="top" | Specifies an action for the &lt;choose&gt; element
| valign="top" | instruction
|-
| valign="top" | with-param
| valign="top" | Defines the value of a parameter to be passed into a template
| valign="top" | instruction
|}
 
=== Fonctions ===
 
{| TableGrid border="1" cellspacing="0" cellpadding="0" width="583"
|-
| valign="top" | <b>Nom</b><ref>http://www.w3schools.com/xsl/xsl_functions.asp</ref> || valign="top" | <b>Description</b>
|-
| valign="top" | current()
| valign="top" | Returns the current node
|-
| valign="top" | document()
| valign="top" | Used to access the nodes in an external XML document
|-
| valign="top" | element-available()
| valign="top" | Tests whether the element specified is supported by the XSLT processor
|-
| widtvalign="top" | format-number()
| valign="top" | Converts a number into a string
|-
| valign="top" | function-available()
| valign="top" | Tests whether the element specified is supported by the XSLT processor
|-
| valign="top" | generate-id()
| valign="top" | Returns a string value that uniquely identifies a specified node
|-
| valign="top" | key()
| valign="top" | Returns a node-set using the index specified by an &lt;xsl:key&gt; element
|-
| valign="top" | system-property
| valign="top" | Returns the value of the system properties
|-
| valign="top" | unparsed-entity-uri()
| valign="top" | Returns the URI of an unparsed entity
|}
 
'''Exhibit 38: Inherited XPath Functions'''
<SMALL>(from http://www.w3schools.com/xsl/xsl_functions.asp)</SMALL>
<br/>''Node Set Functions''
 
 
{| TableGrid border="1" cellspacing="0" cellpadding="0" width="583"
|-
| width="100" valign="top" | <b>Name</b>
| valign="top" | <b>Description</b>
| width="150" valign="top" | <b>Syntax</b>
|-
| valign="top" | count()
| valign="top" | Returns the number of nodes in a node-set
| valign="top" | number=count(node-set)
|-
| valign="top" | id()
| valign="top" | Selects elements by their unique ID
| valign="top" | node-set=id(value)
|-
| valign="top" | last()
| valign="top" | Returns the position number of the last node in the processed node list
| valign="top" | number=last()
|-
| valign="top" | local-name()
| valign="top" | Returns the local part of a node. A node usually consists of a prefix, a colon, followed by the local name
| valign="top" | string=local-name(node)
|-
| valign="top" | name()
| valign="top" | Returns the name of a node
| valign="top" | string=name(node)
|-
| valign="top" | namespace-uri()
| valign="top" | Returns the namespace URI of a specified node
| valign="top" | uri=namespace-uri(node)
|-
| valign="top" | position()
| valign="top" | Returns the position in the node list of the node that is currently being processed
| valign="top" | number=position()
|}
 
''String Functions''
 
{| TableGrid border="1" cellspacing="0" cellpadding="0" width="583"
|-
| width="127" valign="top" | <b> Name</b>
| width="276" valign="top" | <b> Description</b>
| width="180" valign="top" | <b> Syntax &amp; Example</b>
|-
| width="127" valign="top" | Concat()
| width="276" valign="top" | Returns the concatenation of all its arguments
| width="180" valign="top" | string=concat(val1, val2, ..)<br>
<b>Example:</b><br>
concat('The',' ','XML')<br>
Result: 'The XML'
|-
| width="127" valign="top" | contains()
| width="276" valign="top" | Returns true if the second string is contained within the first
string, otherwise it returns false
| width="180" valign="top" | bool=contains(val,substr)<br>
<b>Example</b>:<br>
contains('XML','X')<br>
Result: true
|-
| width="127" valign="top" | normalize-space()
| width="276" valign="top" | Removes leading and trailing spaces from a string
| width="180" valign="top" | string=normalize-space(string)<br>
<b>Example</b>:<br>
normalize-space(' The&nbsp;&nbsp; XML ')<br>
Result: 'The XML'
|-
| width="127" valign="top" | starts-with()
| width="276" valign="top" | Returns true if the first string starts with the second string,
otherwise it returns false
| width="180" valign="top" | bool=starts-with(string,substr)<br>
<b>Example:</b><br>
starts-with('XML','X')<br>
Result: true
|-
| width="127" valign="top" | string()
| width="276" valign="top" | Converts the value argument to a string
| width="180" valign="top" | string(value)<br>
<b>Example:</b><br>
string(314)<br>
Result: '314'
|-
| width="127" valign="top" | string-length()
| width="276" valign="top" | Returns the number of characters in a string
| width="180" valign="top" | number=string-length(string)<br>
<b>Example:</b><br>
string-length('Beatles')<br>
Result: 7
|-
| width="127" valign="top" | substring()
| width="276" valign="top" | Returns a part of the string in the string argument
| width="180" valign="top" | string=substring(string,start,length)<br>
<b>Example:</b><br>
substring('Beatles',1,4)<br>
Result: 'Beat'
|-
| width="127" valign="top" | substring-after()
| width="276" valign="top" | Returns the part of the string in the string argument that occurs after the substring in the substr argument
| width="180" valign="top" | string=substring-after(string,substr)<br>
<b>Example:</b><br>
substring-after('12/10','/')<br>
Result: '10'
|-
| width="127" valign="top" | substring-before()
| width="276" valign="top" | Returns the part of the string in the string argument that occurs
before the substring in the substr argument
| width="180" valign="top" | string=substring-before(string,substr)<br>
<b>Example:</b><br>
substring-before('12/10','/')<br>
Result: '12'
|-
| width="127" valign="top" | translate()
| width="276" valign="top" | Takes the value argument and replaces all occurrences of string1
with string2 and returns the modified string
| width="180" valign="top" | string=translate(value,string1,string2)<br>
<b>Example:</b><br>
translate('12:30',':','!')<br>
Result: '12!30'
|}
''Number Functions''
 
{| TableGrid border="1" cellspacing="0" cellpadding="0" width="590"
|-
| width="197" valign="top" | <b> Name</b>
| width="197" valign="top" | <b> Description</b>
| width="197" valign="top" | <b> Syntax &amp; Example</b>
|-
| width="197" valign="top" | ceiling()
| width="197" valign="top" | Returns the smallest integer that is not less than the number argument
| width="197" valign="top" | number=ceiling(number)
<b>Example:</b><br>
ceiling(3.14)<br>
Result: 4
|-
| width="197" valign="top" | floor()
| width="197" valign="top" | Returns the largest integer that is not greater than the number
argument
| width="197" valign="top" | number=floor(number)
<b>Example:</b><br>
floor(3.14)<br>
Result: 3
|-
| width="197" valign="top" | number()
| width="197" valign="top" | Converts the value argument to a number
| width="197" valign="top" | number=number(value)
<b>Example:</b><br>
number('100')<br>
Result: 100
|-
| width="197" valign="top" | round()
| width="197" valign="top" | Rounds the number argument to the nearest integer
| width="197" valign="top" | integer=round(number)
<b>Example:</b><br>
round(3.14)<br>
Result: 3
|-
| width="197" valign="top" | sum()
| width="197" valign="top" | Returns the total value of a set of numeric values in a node-set
| width="197" valign="top" | number=sum(nodeset)
<b>Example:</b><br>
sum(/cd/price)
|}
'' Boolean Functions''
 
{| TableGrid border="1" cellspacing="0" cellpadding="0" width="590"
|-
| width="197" valign="top" | <b> Name</b>
| width="197" valign="top" | <b> Description</b>
| width="197" valign="top" | <b> Syntax &amp; Example</b>
|-
| width="197" valign="top" | boolean()
| width="197" valign="top" | Converts the value argument to Boolean and returns true or false
| width="197" valign="top" | bool=boolean(value)
|-
| width="197" valign="top" | false()
| width="197" valign="top" | Returns false
| width="197" valign="top" | false()<br>
<b>Example:</b><br>
number(false())<br>
Result: 0
|-
| width="197" valign="top" | lang()
| width="197" valign="top" | Returns true if the language argument matches the language of the xsl:lang element, otherwise it returns false
| width="197" valign="top" | bool=lang(language)
|-
| width="197" valign="top" | not()
| width="197" valign="top" | Returns true if the condition argument is false, and false if the condition argument is true
| width="197" valign="top" | bool=not(condition)<br>
<b>Example:</b><br>
not(false())
|-
| width="197" valign="top" | true()
| width="197" valign="top" | Returns true
| width="197" valign="top" | true()<br>
<b>Example:</b><br>
number(true())<br>
Result: 1
|}
 
== Références ==