Programmation XML/XSLT
Syntaxe de base
modifierXSLT signifie 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.
Le langage XSLT se base sur la notion de règles de transformation. Pour chaque balise XML, on définit comment son contenu doit apparaître dans le document produit. Ces règles sont dans des balises <xsl: et on les enregistre dans des documents .xsl.
Attributs
modifierAttributs | Note |
---|---|
match |
désigne le nœud auquel s'applique le code. |
select |
ce qu'il faut trier dans le résultat. |
order |
classe les résultats (ascending ou descending )
|
case-order |
classe les résultats en distinguant les lettres capitales (upper-first ou
|
data-types |
convertit le type des données (ex : string , number , boolean )
|
Exemple
modifier
<?xml version="1.0" ?>
<persons>
<person username="JS1">
<name>John</name>
<family-name>Smith</family-name>
</person>
<person username="MI1">
<name>Morka</name>
<family-name>Ismincius</family-name>
</person>
</persons>
|
+ |
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/persons">
<root>
<xsl:apply-templates select="person"/>
</root>
</xsl:template>
<xsl:template match="person">
<name username="{@username}">
<xsl:value-of select="name" />
</name>
</xsl:template>
</xsl:stylesheet>
|
= | ||
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name username="JS1">John</name>
<name username="MI1">Morka</name>
</root>
|
PHP
modifierDepuis PHP5 l'extension est déjà inclue[1] dans le fichier PHP.ini :
extension=php_xsl.dll
Il n'y a donc rien a installer pour charger les deux fichiers publiés ci-dessus :
<?php
$XML = new DOMDocument();
$XML->load('note.xml');
$XSL = new DOMDocument();
$XSL->load('note.xsl', LIBXML_NOCDATA);
$xslt = new XSLTProcessor();
$xslt->importStylesheet($XSL);
print $xslt->transformToXML($XML);
?>
Syntaxe avancée
modifierÉléments
modifierÉléments XSL[2][3] | Description | Catégorie |
---|---|---|
apply-imports | Applies a template rule from an imported stylesheet | instruction |
apply-templates | Applies a template rule to the current element or to the current element's child nodes | instruction |
attribute | Adds an attribute | instruction |
attribute-set | Defines a named set of attributes | top-level-element |
call-template | Calls a named template | instruction |
choose | Used in conjunction with <when> and <otherwise> to
express multiple conditional tests |
instruction |
comment | Creates a comment node in the result tree | instruction |
copy | Creates a copy of the current node (without child nodes and attributes) |
instruction |
copy-of | Creates a copy of the current node (with child nodes and attributes) |
instruction |
decimal-format | Defines the characters and symbols to be used when converting numbers into strings, with the format-number() function | top-level-element |
element | Creates an element node in the output document | instruction |
fallback | Specifies an alternate code to run if the processor does not support an XSLT element | instruction |
for-each | Loops through each node in a specified node set | instruction |
if | Contains a template that will be applied only if a specified condition is true | instruction |
import | Imports the contents of one stylesheet into another. Note: An imported stylesheet has lower precedence than the importing stylesheet |
top-level-element |
include | Includes the contents of one stylesheet into another. Note: An included stylesheet has the same precedence as the including stylesheet |
top-level-element |
key | Declares a named key that can be used in the stylesheet with the key() function | top-level-element |
message | Writes a message to the output (used to report errors) | instruction |
namespace-alias | Replaces a namespace in the stylesheet to a different namespace in the output | top-level-element |
number | Determines the integer position of the current node and formats a number | instruction |
otherwise | Specifies a default action for the <choose> element | instruction |
output | Defines the format of the output document | top-level-element |
param | Declares a local or global parameter | top-level-element |
preserve-space | Defines the elements for which white space should be preserved | top-level-element |
processing-instruction | Writes a processing instruction to the output | instruction |
sort | Sorts the output | instruction |
strip-space | Defines the elements for which white space should be removed | top-level-element |
stylesheet | Defines the root element of a stylesheet | top-level-element |
template | Rules to apply when a specified node is matched | top-level-element |
text | Writes literal text to the output | instruction |
transform | Defines the root element of a stylesheet | top-level-element |
value-of | Extracts the value of a selected node | instruction |
variable | Declares a local or global variable | top-level-element or instruction |
when | Specifies an action for the <choose> element | instruction |
with-param | Defines the value of a parameter to be passed into a template | instruction |
Fonctions
modifierNom[4] | Description |
---|---|
current() | Returns the current node |
document() | Used to access the nodes in an external XML document |
element-available() | Tests whether the element specified is supported by the XSLT processor |
format-number() | Converts a number into a string |
function-available() | Tests whether the element specified is supported by the XSLT processor |
generate-id() | Returns a string value that uniquely identifies a specified node |
key() | Returns a node-set using the index specified by an <xsl:key> element |
system-property | Returns the value of the system properties |
unparsed-entity-uri() | Returns the URI of an unparsed entity |
Fonctions héritées de XPath
modifierNom[5] | Description | Syntaxe |
---|---|---|
count() | Returns the number of nodes in a node-set | number=count(node-set) |
id() | Selects elements by their unique ID | node-set=id(value) |
last() | Returns the position number of the last node in the processed node list | number=last() |
local-name() | Returns the local part of a node. A node usually consists of a prefix, a colon, followed by the local name | string=local-name(node) |
name() | Returns the name of a node | string=name(node) |
namespace-uri() | Returns the namespace URI of a specified node | uri=namespace-uri(node) |
position() | Returns the position in the node list of the node that is currently being processed | number=position() |
Fonctions chaines
modifierNom | Description | Exemple |
---|---|---|
Concat() | Returns the concatenation of all its arguments | string=concat(val1, val2, ..) Example: |
contains() | Returns true if the second string is contained within the first
string, otherwise it returns false |
bool=contains(val,substr) Example: |
normalize-space() | Removes leading and trailing spaces from a string | string=normalize-space(string) Example: |
starts-with() | Returns true if the first string starts with the second string,
otherwise it returns false |
bool=starts-with(string,substr) Example: |
string() | Converts the value argument to a string | string(value) Example: |
string-length() | Returns the number of characters in a string | number=string-length(string) Example: |
substring() | Returns a part of the string in the string argument | string=substring(string,start,length) Example: |
substring-after() | Returns the part of the string in the string argument that occurs after the substring in the substr argument | string=substring-after(string,substr) Example: |
substring-before() | Returns the part of the string in the string argument that occurs
before the substring in the substr argument |
string=substring-before(string,substr) Example: |
translate() | Takes the value argument and replaces all occurrences of string1
with string2 and returns the modified string |
string=translate(value,string1,string2) Example: |
Fonctions nombres
modifierNom | Description | Exemple |
---|---|---|
ceiling() | Returns the smallest integer that is not less than the number argument | number=ceiling(number)
Example: |
floor() | Returns the largest integer that is not greater than the number
argument |
number=floor(number)
Example: |
number() | Converts the value argument to a number | number=number(value)
Example: |
round() | Rounds the number argument to the nearest integer | integer=round(number)
Example: |
sum() | Returns the total value of a set of numeric values in a node-set | number=sum(nodeset)
Example: |
Fonctions booléennes
modifierNom | Description | Exemple |
---|---|---|
boolean() | Converts the value argument to Boolean and returns true or false | bool=boolean(value) |
false() | Returns false | false() Example: |
lang() | Returns true if the language argument matches the language of the xsl:lang element, otherwise it returns false | bool=lang(language) |
not() | Returns true if the condition argument is false, and false if the condition argument is true | bool=not(condition) Example: |
true() | Returns true | true() Example: |