1. Value, and loop current element 1.1. value-of: display of values 1.1.1. Statement The statement, crucial as it is the only one that allows the display of a value xslt, can only be done in a template. As the templates are a chapter following the examples below place will be held all in the first template <xsl:template match="/"> <xsl:value-of select="'Cela displays a string!'"/> </ xsl: template> The select attribute of xsl: value-of contains the data to display. This data can be in the form of a chain (as above), a number or a path xpath. 1.1.2. Behavior following XPath For this section we will take a xml "directory" as an example. XML file <? xml version = "1.0" encoding = "UTF-8"?> <? xml-stylesheet type = "text / xsl" href = "monfichier.xslt"?> <annuaire> <personne> <name> Dupond </ name> <firstname> Marc </ first name> <num_telephone> 02 96 45 87 34 </ num_telephone> </ Person> <personne> <name> Smith </ name> <firstname> Matthias </ first name> <num_telephone> 03 45 67 25 99 </ num_telephone> </ Person> <personne> <name> Durand </ name> <firstname> Lucien </ first name> <num_telephone> 01 45 29 62 77 </ num_telephone> </ Person> </ directory> The xsl: value-of when it has a xpath in its select attribute displays data text () (data only text which lie between the tags) corresponding. If we want for example view data of the second person "Dupont Matthias" on a line of HTML table, use the following code: XSLT file: <? 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> </ Title> </ Head> <body> <table> <tr> <td> <xsl:value-of select="/annuaire/personne[2]/nom"/> </ Td> <td> <xsl:value-of select="/annuaire/personne[2]/prenom"/> </ Td> <td> <xsl:value-of select="/annuaire/personne[2]/num_telephone"/> </ Td> </ Tr> </ Table> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> result <html> <head> <META Http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> </ Title> </ Head> <Body > <table> <tr> <td> Smith </ td> <td> Matthias </ td> <td> 03 45 67 25 99 </ td> </ Tr> </ Table> </ Body> </ html> </ html> This corresponds well each text () of the three beacons name, surname and num_telephone. What is the behaviour on a xpath which selects several knots? We will take all the names of file: / / name XSLT file <? 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> </ Title> </ Head> <body> <table> <tr> <td> <xsl:value-of Select="//nom"/> </ Td> </ Tr> </ Table> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> result <html> <head> <META Http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> </ Title> </ Head> <body> <table> <tr> <td> Dupond </ td> </ Tr> </ Table> </ Body> </ html> </ html> The answer is simple, if a xpath selects several knots only the first is displayed. What would happen if the cons by xpath was selected as a node that contains others, such as tags? And what would be the text content () of all tags which contained displayed: XSLT file <? 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> </ Title> </ Head> <body> <table> <tr> <td> <xsl:value-of select="/annuaire/personne[2]"/> </ Td> </ Tr> </ Table> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> result <html> <head> <META Http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> </ Title> </ Head> <body> <table> <tr> <td> DupontMatthias03 45 67 25 99 </ td> </ Tr> </ Table> </ Body> </ html> Attention, I would remind you that the contents of an attribute is not a text (). If the file xml was amended in this way: XML file amended: <? xml version = "1.0" encoding = "UTF-8"?> <? xml-stylesheet type = "text / xsl" href = "monfichier.xslt"?> <annuaire> <personne> <name Id="1"> Dupond </ name> <firstname> Marc </ first name> <num_telephone> 02 96 45 87 34 </ num_telephone> </ Person> <personne> <name Id="2"> Smith </ name> <firstname> Matthias </ first name> <num_telephone> 03 45 67 25 99 </ num_telephone> </ Person> <personne> <name Id="3"> Durand </ name> <firstname> Lucien </ first name> <num_telephone> 01 45 29 62 77 </ num_telephone> </ Person> </ directory> In him applying file xslt before, the result would not be changed. The only way to view the contents of an attribute is to use the corresponding xpath: @ nom_de_l_attribut. variation on the roster 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> </ Title> </ Head> <body> <table> <tr> <td> <xsl:value-of select="/annuaire/personne[2]/nom/@id"/> </ Td> <td> <xsl:value-of select="/annuaire/personne[2]/nom"/> </ Td> <td> <xsl:value-of select="/annuaire/personne[2]/prenom"/> </ Td> <td> <xsl:value-of select="/annuaire/personne[2]/num_telephone"/> </ Td> </ Tr> </ Table> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> result <html> <head> <META Http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title> </ Title> </ Head> <body> <table> <tr> <td> 2 </ td> <td> Smith </ td> <td> Matthias </ td> <td> 03 45 67 25 99 </ td> </ Tr> </ Table> </ Body> </ html>
1.2. Definition of the current element The current element is the node selected by the processor, it is not necessarily one who is playing or display, it can not be created / modified by the instructions xsl: for-each or xsl: apply-templates. In these loops, it is the crux short treatment can be achieved by the civil xslt: current () <xsl:value-of select="current()"/> The result of this course will detail a party, and chapter tests and Conditions complement its behavioral peculiarities. 1.3. for-each: the loop 1.3.1. Declaration and functioning The xsl: for-each loop is treating all xml knots before it, because XSLT there is no loop indexed (for i =.. to ..), the loop is always on completeness of the information in the select of xsl: for-each As for the xsl: value-of the road xpath it is provided in its select attribute. The code xslt framed by the tags xsl: for-each will be applied to each element "visited" the current element current () example declaration <xsl:for-each select="//node()"> <xsl:value-of Select="current()"/> </ xsl: for-each> 1.3.2. Modification and use of the current element We will again select all the names, but this time, instead putting the xpath in the xsl: value-of, we will submit to xsl: for-each. my 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> </ Title> </ Head> <body> <table> <tr> <xsl:for-each Select="//nom"> <td> <xsl:value-of Select="current()"/> </ Td> </ Xsl: for-each> </ Tr> </ Table> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> Result: <html> <head> <META Http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title /> </ Head> <body> <table> <tr> <td> Dupond </ td> <td> Smith </ td> <td> Durand </ td> </ Tr> </ Table> </ Body> </ html> This time we can observe that all nodes have been treated well and that the current element is the node that is currently playing in the xpath of select. 1.3.3. Behavior rangelands Some previous tests we can already deduct a portion of the trail of xpath. In the previous example, the first name displayed is the first encountered from the top of the roster. By serving xsl: for-each and xpath / / node () which selects ALL nodes xml. We will be able to see the order of the trail using the xml tags numbered: my XML file <? xml version = "1.0" encoding = "UTF-8"?> <? xml-stylesheet type = "text / xsl" href = "monfichier.xslt"?> <racine num="0"> <noeud Num="1"> <noeud Num="3"> <noeud Num="5"/> <noeud Num="6"/> </ Node> <noeud Num="4"> <noeud Num="7"/> </ Node> </ Node> <noeud Num="2"/> </ root> XSLT file <? xml version = "1.0" encoding = "UTF-8"?> <xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/Transform" xmlns: for = "http://www.w3.org/1999/XSL/Format "> <xsl:template Match="/"> <html> <body> <xsl:for-each Select="//node()"> <xsl:value-of Select="concat(@num,' ::')"/> </ Xsl: for-each> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> result <html xmlns:fo="http://www.w3.org/1999/XSL/Format"> <body>: 0:: 1:: 3:: 5: 6:: 4:: 7:: 2:: </ body> </ html> </ html> The order is: 1. a) Sens vertical top down take the first move in b) 2. b) treat the node to go c) 3. c) If the node has a son: -- Take the first in the vertical direction -- Back to b) -- Otherwise go to d) 4. d) If in the vertical direction there is a "brother" to succeed him go b) if e) 5. e) I took the father of node if it exists and to go d) otherwise go to f) 6. f) end the first attention: the result is not a mistake. It is simply that the first node selection is: xml-stylesheet type = "text / xsl" href = "my fichier.xslt"? as he did not attribute num, nothing is displayed. 1.4. Supplement on the current element 1.4.1. XPath on the current element So far we used relative paths preceded by / (where you have to indicate all the way from the root) or / / parser for the whole of the tree. With the advent of the current element, we will can take as a reference instead of the root. The XPath using it as such are preceded neither / nor / /. In this example, with the file directory, it runs the various people and it shows their name surname, num_telephone via the current element set by the xsl: for-each XSLT file <? 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/> </ Head> <body> <table> <xsl:for-each Select="/annuaire/personne"> <tr> <td> <xsl:value-of Select="nom"/> </ Td> <td> <xsl:value-of Select="prenom"/> </ Td> <td> <xsl:value-of Select="num_telephone"/> </ Td> </ Tr> </ Xsl: for-each> </ Table> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> result <html> <head> <META Http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title /> </ Head> <body> <table> <tr> <td> Dupond </ td> <td> Marc </ td> <td> 02 96 45 87 34 </ td> </ Tr> <tr> <td> Smith </ td> <td> Matthias </ td> <td> 03 45 67 25 99 </ td> </ Tr> <tr> <td> Durand </ td> <td> Lucien </ td> <td> 01 45 29 62 77 </ td> </ Tr> </ Table> </ Body> </ html> For sake of performance, it is noteworthy that use the current element as a basis for xpath (as here), where possible, is always more efficient than using roads prefixed / / /. 1.4.2. Difference between. and current () The "." Is a shortcut for xpath self:: * and not for the function xslt current (). As we have seen previously current () returns the item being traitement.Self: * reference to the item being played. By against it is true that in some cases, as in the xsl: value-of these elements point on the same node. XSLT file: <? 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/> </ Head> <body> <table> <xsl:for-each select="/annuaire/personne/nom"> <tr> <td> <xsl:value-of Select="current()"/> </ Td> <td> <xsl:value-of Select="."/> </ Td> </ Tr> </ Xsl: for-each> </ Table> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> Result: <html> <head> <META Http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title /> </ Head> <body> <table> <tr> <td> Dupond </ td> <td> Dupond </ td> </ Tr> <tr> <td> Smith </ td> <td> Smith </ td> </ Tr> <tr> <td> Durand </ td> <td> Durand </ td> </ Tr> </ Table> </ Body> </ html> The difference is mainly in scores of xpath, when using tests. We are moving in a first xsl: for-each put us on behalf of the first person. / / person [1] / name, following three xsl: for-each nested which have as a common basis to go back two parents parser then again all the name tags by conducting a test: In the first select = "parent:: * / parent:: * / person [name = current ()] / name" is not that selects persons whose son name is equal to the current node. In the second select = "parent:: * / parent:: * / person / name [text ()=.]", could not select that name tags whose content is equal to the item being played (it same). In the last select = "parent:: * / parent:: * / person / name [.= current ()]", not select tags that read the content of which is equal to the current element. XSLT file: <? 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/> </ Head> <body> <table> <xsl:for-each Select="//personne[1]/nom"> <tr> <xsl:for-each select="parent::*/parent::*/personne[nom=current()]/nom"> <td> <xsl:value-of Select="."/> </ Td> </ Xsl: for-each> </ Tr> <tr> <xsl:for-each select="parent::*/parent::*/personne/nom[text()=.]"> <td> <xsl:value-of Select="."/> </ Td> </ Xsl: for-each> </ Tr> <tr> <xsl:for-each select="parent::*/parent::*/personne/nom[.=current()]"> <td> <xsl:value-of Select="."/> </ Td> </ Xsl: for-each> </ Tr> </ Xsl: for-each> </ Table> </ Body> </ Html> </ Xsl: template> </ xsl: stylesheet> result <? xml-stylesheet type = "text / xsl" href = "C: \ Documents and Settings \ lovers erwan \ My Documents \ xml \ tutorial \ loop \ test.xslt"?> <html> <head> <META Http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title /> </ Head> <body> <table> <tr> <td> Dupond </ td> </ Tr> <tr> <td> Dupond </ td> <td> Smith </ td> <td> Durand </ td> </ Tr> <tr> <td> Dupond </ td> </ Tr> </ Table> </ Body> </ html>
|