Conditions and tests XSLT 1.0 PDF Print E-mail
User Rating: / 0
PoorBest 
Thursday, 10 July 2008

A. Conditions and tests XSLT
Below the XML file that will be used during demonstrations following:
1. Reminder on operators Xpath
     * Equality: =
     * Education:>;
     * Below: <;
     * Added: +
     * Subtraction: - (attention this symbol is accepted in tag names, it is therefore imperative be preceded and succeeded by a space if it is to be interpreted)
     * Division: div
     * Modulo: mod

2. xsl: if
In the XSLT if? So what? is code by using the tag xsl: if. His test attribute contains the xpath need to test if the code is valid content within the tag xsl: if is executed.

<xsl:if Test="le xpath"> ......</ xsl: if>

The types of tests can be separated into two categories:
-- To test the value of a node or a variable
-- To test the existence of a node or content of a variable.
We want to show in a table all individuals sex M (test value)
the XSLT
<? xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template Match="/">
<html>
<head>
<title> Name </ title>
</ Head>
<body>
<table>
<xsl:for-each Select="//personne">
<xsl:if Test="sexe='M' ">
<tr>
<td>
<xsl:value-of Select="nom"/>
</ Td>
<td>
<xsl:value-of Select="prenom"/>
</ Td>
</ Tr>
</ Xsl: if>
</ Xsl: for-each>
</ Table>
</ Body>
</ Html>
</ Xsl: template>
</ xsl: stylesheet>
the result
<html>
<head>
<META Http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> Name </ title>
</ Head>
<body>
<table>
<tr>
<td> Dupuis </ td>
<td> Paul </ td>
</ Tr>
<tr>
<td> Kaplan </ td>
<td> Pierre </ td>
</ Tr>
</ Table>
</ Body>
</ html>
We want to show in a table all individuals with a hobby (test on the existence)
the XSLT
<? xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template Match="/">
<html>
<head>
<title> Leisure </ title>
</ Head>
<body>
<table>
<xsl:for-each Select="//personne">
<xsl:if Test="loisir ">
<tr>
<td>
<xsl:value-of Select="nom"/>
</ Td>
<td>
<xsl:value-of Select="prenom"/>
</ Td>
<td>
<xsl:value-of Select="loisir"/>
</ Td>
</ Tr>
</ Xsl: if>
</ Xsl: for-each>
</ Table>
</ Body>
</ Html>
</ Xsl: template>
</ xsl: stylesheet>
the result
<html>
<head>
<META Http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> Leisure </ title>
</ Head>
<body>
<table>
<tr>
<td> Dupuis </ td>
<td> Paul </ td>
<td> Music </ td>
</ Tr>
<tr>
<td> Durand </ td>
<td> Celine </ td>
<td> Sports </ td>
</ Tr>
<tr>
<td> Kaplan </ td>
<td> Pierre </ td>
<td> Cinema </ td>
</ Tr>
</ Table>
</ Body>
</ html>
Behavior multiple xsl: if: It is of course possible to save many xsl: if behind each other or to encapsulate them. In the first case they run one after the other. This tag does not allow the expression of one else.

As an example we consolidate the two previous requests.
The xslt
<? xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template Match="/">
<html>
<head>
<title> 2 applications </ title>
</ Head>
<body>
<table>
<xsl:for-each Select="//personne">
<xsl:if Test="sexe='M' ">
<tr>
<td>
<xsl:value-of Select="nom"/>
</ Td>
<td>
<xsl:value-of Select="prenom"/>
</ Td>
<td> Man </ td>
</ Tr>
</ Xsl: if>
<xsl:if Test="loisir ">
<tr>
<td>
<xsl:value-of Select="nom"/>
</ Td>
<td>
<xsl:value-of Select="prenom"/>
</ Td>
<td>
<xsl:value-of Select="loisir"/>
</ Td>
</ Tr>
</ Xsl: if>
</ Xsl: for-each>
</ Table>
</ Body>
</ Html>
</ Xsl: template>
</ xsl: stylesheet>

The result

<html>
<head>
<META Http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> 2 applications </ title>
</ Head>
<body>
<table>
<tr>
<td> Dupuis </ td>
<td> Paul </ td>
<td> Man </ td>
</ Tr>
<tr>
<td> Dupuis </ td>
<td> Paul </ td>
<td> Music </ td>
</ Tr>
<tr>
<td> Durand </ td>
<td> Celine </ td>
<td> Sports </ td>
</ Tr>
<tr>
<td> Kaplan </ td>
<td> Pierre </ td>
<td> Man </ td>
</ Tr>
<tr>
<td> Kaplan </ td>
<td> Pierre </ td>
<td> Cinema </ td>
</ Tr>
</ Table>
</ Body>
</ html>

3. The xsl: choose
The structure looks a bit like that of a CASE / SWITCH. Different cases (whose tests are not obliged to refer to a single variable or node) are listed one after another. If the test is valid, the code content between the two tags (xsl: when), which is executed. The tag xsl: otherwise corresponds to one else.
<xsl:choose>
<xsl:when Test="xpath1">
...
</ Xsl: when>
<xsl:when Test="xpath1">
....
</ Xsl: when>
<xsl:otherwise>
....
</ Xsl: otherwise>
</ Xsl: choose>
 If tests overlap, only one is made: the first said.
the XSLT

<? xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template Match="/">
<html>
<head>
<title> Choice </ title>
</ Head>
<body>
<table>
<xsl:for-each Select="//personne">
<tr>
<td>
<xsl:value-of Select="nom"/>
</ Td>
<td>
<xsl:value-of Select="prenom"/>
</ Td>
<xsl:choose>
<xsl:when Test="sexe='M'">
<td> Man </ td>
</ Xsl: when>
<xsl:when Test="loisir">
<td>
<xsl:value-of Select="loisir"/>
</ Td>
</ Xsl: when>
<xsl:otherwise>
<td> Other </ td>
</ Xsl: otherwise>
</ Xsl: choose>
</ Tr>
</ Xsl: for-each>
</ Table>
</ Body>
</ Html>
</ Xsl: template>
</ xsl: stylesheet>

the result
<html>
<head>
<META Http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> Choice </ title>
</ Head>
<body>
<table>
<tr>
<td> Dupuis </ td>
<td> Paul </ td>
<td> Man </ td>
</ Tr>
<tr>
<td> Durand </ td>
<td> Celine </ td>
<td> Sports </ td>
</ Tr>
<tr>
<td> Kaplan </ td>
<td> Pierre </ td>
<td> Man </ td>
</ Tr>
<tr>
<td> Gautier </ td>
<td> Lucille </ td>
<td> Other </ td>
</ Tr>
</ Table>
</ Body>
</ html>
  Can be seen here that the two men are resumed once under test xsl: when test = "sex = 'M'" It is of course quite possible to encapsulate xsl: if or xsl: choose with each other.

4. Tests individuals on the current element
When a test is conducted on the current element, notions Xpath as name (), position (), text ()? that can not normally be tested between [], can be tested directly. An example: we are going to build a table per person, knowing that, depending on their position () in the tree (or odd pair) the table will have the "class" correspondent. The title lines will be the name () tag.
the XSLT
<? xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template Match="/">
<html>
<head>
<title> Name </ title>
</ Head>
<body>
<table>
<xsl:for-each Select="//personne">
<xsl:choose>
<xsl:when Test="position() mod 2=0">
<table Class="pair">
<xsl:for-each Select="child::*">
<tr>
<td>
<xsl:value-of Select="name()"/>
</ Td>
<td>
<xsl:value-of Select="."/>
</ Td>
</ Tr>
</ Xsl: for-each>
</ Table>
</ Xsl: when>
<xsl:otherwise>
<table Class="impair">
<xsl:for-each Select="child::*">
<tr>
<td>
<xsl:value-of Select="name()"/>
</ Td>
<td>
<xsl:value-of Select="."/>
</ Td>
</ Tr>
</ Xsl: for-each>
</ Table>
</ Xsl: otherwise>
</ Xsl: choose>
</ Xsl: for-each>
</ Table>
</ Body>
</ Html>
</ Xsl: template>
</ xsl: stylesheet>

the result
<html>
<head>
<META Http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> Name </ title>
</ Head>
<body>
<table>
<table Class="impair">
<tr>
<td> Name </ td>
<td> Dupuis </ td>
</ Tr>
<tr>
<td> First_name </ td>
<td> Paul </ td>
</ Tr>
<tr>
<td> Age </ td>
<td> 45 </ td>
</ Tr>
<tr>
<td> Sex </ td>
<td> M </ td>
</ Tr>
<tr>
<td> Leisure </ td>
<td> Music </ td>
</ Tr>
</ Table>
<table Class="pair">
<tr>
<td> Name </ td>
<td> Durand </ td>
</ Tr>
<tr>
<td> First_name </ td>
<td> Celine </ td>
</ Tr>
<tr>
<td> Age </ td>
<td> 54 </ td>
</ Tr>
<tr>
<td> Sex </ td>
<td> F </ td>
</ Tr>
<tr>
<td> Leisure </ td>
<td> Sports </ td>
</ Tr>
</ Table>
<table Class="impair">
<tr>
<td> Name </ td>
<td> Kaplan </ td>
</ Tr>
<tr>
<td> First_name </ td>
<td> Pierre </ td>
</ Tr>
<tr>
<td> Age </ td>
<td> 23 </ td>
</ Tr>
<tr>
<td> Sex </ td>
<td> M </ td>
</ Tr>
<tr>
<td> Leisure </ td>
<td> Cinema </ td>
</ Tr>
</ Table>
<table Class="pair">
<tr>
<td> Name </ td>
<td> Gautier </ td>
</ Tr>
<tr>
<td> First_name </ td>
<td> Lucille </ td>
</ Tr>
<tr>
<td> Age </ td>
<td> 27 </ td>
</ Tr>
<tr>
<td> Sex </ td>
<td> F </ td>
</ Tr>
</ Table>
</ Table>
</ Body>
</ html>
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials