49. How it Works? <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class.xsl"?> <class> <student>Jack</student> <student>Harry</student> <student>Rebecca</student> <teacher>Mr. Bean</teacher> </class> <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="student"> Found a learner! </xsl:template> </xsl:stylesheet> Step 1: For all matching student tags, display output Found a learner! Step 2: Come here and run the template recursively for all the student tags, displaying Found a learner! Step 3: For all tags other than student , there is no template in our XSL. Therefore, blindly output them, as they are!
117. XPath View Root Comment This is a comment Element BOOK Attribute author Andrew Tanenbaum Attribute edition 3 Element REVIEW Text This is a wonderful book! Text Computer Networks
118.
119.
120. XPath Examples – 1 If context node is the <book> element, an empty node-set would be returned. If the context node is <paragraph> number 1 in <section> 2 of <chapter> 2, the returned node-set would be the text node with value First paragraph . Select all text node children of the context node. child::text() If context node is the <book> element, the node-set containing <title>, <chapters> and <appendices> nodes would be returned. Select all element node children of the context node. Children other than the element node are not selected. child::* If context is book, empty node-set would be returned. If context node is <section> element in chapter 2 with attribute number =“1”, a node-set containing four paragraph nodes would be returned. Select the <paragraph> child element nodes of the context node. child::paragraph Example Meaning Syntax
121. XPath Examples – 2 If context node is the <chapter> 1 element, a node-set containing two attribute nodes would be returned, representing the number and title attributes. Select all attribute nodes of the context node. attribute::* NA Select all attribute nodes with name number under the context node. attribute::number NA Select all the child nodes of the context node. Note that this will return non-elements also. child::node() Example Meaning Syntax
122. XPath Examples – 3 NA Select the context node if it is the <paragraph> element. self:paragraph If the context node is <chapters>, an empty node-set would be returned. If the context node is a <section>, the result would be a node-set containing one <chapter> element which contains this <section> element. Select the <chapter> element nodes that are ancestors of the context node. If the context node is a <chapter> element, return that also. ancestor-or-self::chapter If the context node is <book> or <chapters> element, we would get a node-set consisting of six <paragraph> nodes. If the context node is <section> 1 element for <chapter> 2, we would get a node-set consisting of four <paragraph> nodes. Select all <paragraph> elements that are descendants of the context node. descendant::paragraph Example Meaning Syntax
123. Unabbreviated relative location path Abbreviated relative location path Selection criteria child::BOOK BOOK Element nodes that are children of the context node. child::* * All element nodes that are children of the context node. child::text () text () All text node children of the context node. self::node () . Context node itself. parent::node () .. Parent of the context node. attribute::reviewer @reviewer Attribute reviewer of the context node. attribute::* @* All attributes of the context node. parent::node ()/attribute::reviewer [email_address] Attribute reviewer of the parent of the context node. child::AUTHOR [position () = 1] AUTHOR [1] First AUTHOR child node of the context node. child::AUTHOR [position () = last ()] AUTHOR [last ()] Last AUTHOR child node of the context node. child::REVIEW [attribute::reviewer = “Jui”] REVIEW [@reviewer = “Jui”] All REVIEW element children of the context node having a reviewer attribute with a value of Jui . child::REVIEW [attribute::reviewer = “Jui’] [2] REVIEW [@reviewer = “Jui”] [2] Second REVIEW child of the context node having a reviewer attribute with a value of Jui . child::REVIEW [position () = 2] [attribute::reviewer = “Jui’] REVIEW [2] [@reviewer = “Jui”] Second REVIEW child of the context node if it has a reviewer attribute with a value of Jui . child::*/child::AUTHOR */AUTHOR All grand-children element nodes of the context node that are also AUTHOR elements. child::BOOK[child::TITLE= “TCP/IP”] BOOK[TITLE = “TCP/IP”] BOOK element children of the context node having one or more TITLE element children with string-value as TCP/IP . child::BOOK/child::REVIEW/child::COMMENT [position () = 2] BOOK/REVIEW/COMMENT [2] Second child COMMENT element node of the REVIEW element node, which itself is the child of the BOOK element node. self::node()/descendant-or-self::node()/child:COMMENT .//COMMENT All COMMENT element nodes that are descendants of the context node. Note that the // notation is a short-hand for the descendant:: syntax.