Home

Intermediate XSLT and XPath

image

Contents

1. gt lt 1i gt lt ul gt Here are all participant s firstnames that have a food preference lt ul gt lt xsl apply templates select participant FoodPref gt lt ul gt Here are all items that have a value of high lt ul gt lt xsl apply templates select item val high gt lt ul gt lt body gt lt html gt lt xsl template gt lt xsl template match participant gt lt li gt lt xsl value of select FirstName gt lt li gt lt xsl template gt lt xsl template match item gt lt li gt lt xsl value of select gt lt li gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 22 HTML result lt html gt lt body bgcolor FFFFFF gt lt h1 gt Retrieve selected elements lt h1 gt Here is the name of participant two lt ul gt lt li gt Jonathan lt 1li gt lt ul gt Here are all participant s firstnames that have a food preference lt ul gt lt li gt Daniel lt li gt lt li gt Jonathan lt li gt lt ul gt Here are all items that have a value of high lt ul gt lt li gt Register for a XSLT course and do exercices lt li gt lt li gt Register for a XPath course and do exercices lt li gt lt ul gt lt body gt lt html gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 2
2. 2 3 The document model of XPath e XPath sees an XML document as a tree structure e Each information XML elements attributes text etc is called a node e this is fairly similar to the W3C DOM model an XML or XSLT processor would use Nodes that XPath can see e root node e ATTENTION The root is not necessarily the XML root element E g processing instructions like a stylesheet declaration are also nodes e Elements and attributes e Special nodes like comments processing instructions namespace declarations Nodes XPath can t see e XPath looks at the final document therefore can t see entities and document type declarations The XML context e What a given XPath expression means is always defined by a given XML context i e the current node in the XML tree Advance warning The rest of this chapter will be quite boring and it only covers XPath essentials XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xmi xpath 1 10 2 4 Element Location Paths e We present a few expressions for locating nodes e This page is not complete and uses abbreviated syntax Document root node returns the document root which is not necessarily the XML root Syntax Direct child element Syntax XML _element_name Direct child of the root node Syntax XML_element_name Child of a child Syntax XML_element_name
3. and a valid XML example file e Translate this XML document with XSLT to a somewhat valid HTML or XHTML e Write a report about the purpose and the architecture of the XSLT file e Make use of some more advanced XPath and XSLT constructs e g filtering constructs or conditionals Make sure to create something that is different from project 4 e Bonus Use CSS to style the HTML output e Bonus Produce really valid HTML or XHTML e Take into account critique and self critique for homework 4 e You may reuse materials from previous homework e strongly suggest to base this project on homework 4 XSLT XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 9 Homework mini project 5 xml xpath 1 48 9 2 Approximate evaluation grid Minimal requirement Features Expect a Valid XML DTD and well formed XSLT style sheet that does a translation D An XML contents that displays as X HTML in a web browser of your choice C You will get extra points for Extra points Extra features quality Inserted comments lt gt in the various files XML XSLT or CSS XML data organization that is appropriate for your domain Complexity of style sheet kinds of transformations Ergonomic and nice presentation style and function Your result document is valid HTML or XHTML A 1 2 page user manual that exp
4. be the tutor lt description gt lt FoodPref picture dolores_001 jpg gt Sea Food lt FoodPref gt lt participant gt lt participant gt lt FirstName gt Jonathan lt FirstName gt lt qualification gt 5 lt qualification gt lt FoodPref picture dolores_002 jpg gt Asian lt FoodPref gt lt participant gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 6 Looping xml xpath 1 43 Part of XSLT file http tecfa unige ch lt xsl template match participants gt lt table border 2 cellspacing 1 cellpadding 6 gt lt tr gt lt th gt Qualification lt th gt lt th gt First Name lt th gt lt th gt Description lt th gt lt th gt Food Picture lt th gt lt tr gt lt xsl for each select participant gt lt xsl sort select qualification gt lt tr gt lt td gt lt xsl value of select qualification gt lt td gt lt td gt lt xsl value of select FirstName gt lt td gt lt td gt lt xsl value of select description gt lt td gt lt td gt lt xsl if test FoodPref picture gt lt img src FoodPref picture gt lt xsl2if gt lt td gt lt tr gt lt xsl for each gt lt table gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 7 Movingon XX path 1 44 7 Moving on e XSLT is a full programming language but further XSLT
5. gt lt qualification gt 8 lt qualification gt lt description gt Bernadette is an arts major lt description gt lt participant gt Task e Display a list of First Names plus their food preferences XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 16 XSLT File xpath jungle 2 xsl e The first rule will just select all participants and create the list container ul lt xsl template match gt lt html gt lt body bgcolor FFFFFE gt lt hl gt What do we know about our participants lt h1 gt Here are some food preferences lt ul gt lt xSl apply templates select participant gt lt ul gt lt body gt lt html gt lt xsl template gt e The second rule will display names of participants and launch a template for FoodPref e Note Not all participants have a FoodPref element If it is absent it will just be ignored lt xsl template match participant gt lt li gt lt xsl value of select FirstName gt lt xsl apply templates select FoodPref gt lt li gt lt xsl template gt e This rule displays the text contents of FoodPref and then makes an HTML img tag lt xsl template match FoodPref gt prefers lt xsl value of select gt lt img src picture gt lt br clear all gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TEC
6. just copied see lt xsl template match text gt lt xsl value of select gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xmi xpath 1 19 2 7 XPaths with predicates e A predicate is an expression that can be true or false e A predicate is appended within to a given location path and it will refine results e More than one predicate can be appended to and within a location path e Expressions can contain mathematical or boolean operators Find element number N in a list Syntax XML_element_name N project participants participant 2 project participants participant 2 FirstName Find elements that have a given attribute Syntax XML_element_name attribute_name Find elements that have a given element as child Syntax XML _element_name XML_element_name participant FoodPref Mathematical expressions e Use the standard operators except div instead of Syntax div mod e mod is interesting if you want to display a long list in table format 5 mod 2 returns 1 7 mod 2 and 3 mod 2 Loo XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 20 Boolean operators comparison and or e List of operators according to preceden
7. participants lt h1 gt Average is SATAS lt body gt lt html gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 27 Example 2 5 Find first names containing nat url http tecfa unige ch guides xml examples xpath xpath jungle 5 xsl url http tecfa unige ch guides xml examples xpath xpath jungle 5 xml The XSLT stylesheet file xpath jungle 5 xsl lt xsl template match gt lt html gt lt body bgcolor FFFFFF gt lt hl gt Do we have a nat lt h1 gt First Names that contain nat lt ul gt lt xsl apply templates select participant contains FirstName nat gt lt ul gt First Names that contain nat and Nat lt ul gt lt xSl apply templates select participant contains translate FirstName N n nat gt lt ul gt lt body gt lt html gt lt xsl template gt lt xsl template match participant gt lt li gt lt xsl value of select FirstName gt lt li gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 28 2 9 Union of XPaths e Union Xpaths combine more than one XPath and all the resulting nodes are returned e A typical example is the default rule which means that the templ
8. title gt lt head gt lt body bocolor iLTLrri gt Here is a list of recipe ingrediants lt p gt lt xsl apply templates select item gt lt p gt Here is a list of recipe ingrediants this time numbered lt p gt lt xsl apply templates select item mode numb gt lt p gt lt body gt lt html gt lt xsl template gt lt xsl template match item gt lt xsl value of select gt lt xsl if test position last gt lt xsl text gt lt xsl text gt lt xsl if gt lt xsl template gt lt xsl template match item mode numb gt lt xsl value of select position gt lt xsl value of select gt lt xsl if test position last gt lt xsl text gt lt xsl text gt lt xsl if gt lt xsl template gt lt xsl stylesheet gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 5 Conditional XSLT and whitespaces xml xpath 1 37 HTML results lt html xmins http www w3 org 1999 xhtml gt lt head gt lt title gt Ingrediant list lt title gt lt head gt lt body bocoLlor iliriri gt Here is a list of recipe ingrediants lt p gt 3 taco shells 1 pkg hamburger 100g Taco mix Lettuce lt p gt Here is a list of recipe ingrediants this time numbered lt p gt 1 3 taco shells 2 1 pkg hamburger 100g 3 Taco mix 4 Lettuce lt p gt lt body gt lt html gt Same logic woul
9. 1 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 23 2 8 XPath functions e XPath defines a certain number of functions e You can recognize a function because it has e Functions are programming constructs that will return various kinds of informations e g e true false e a number e a string e alist of nodes e It is not obvious to understand these e There are restrictions on how you can use functions stick to examples or the reference last last gives the number or nodes within a context position position returns the position of an element with respect to other children for a parent count node set count gives the number of nodes in a node set usually found with an XPath starts with string string returns TRUE if the second string is part of the first and starts off the first Participant starts with Firstname Berna contains string string returns TRUE if the second string is part of the first Participant contains FirstName nat XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 24 string length string returns the length of a string number string transforms a string into a number sum node set computes the sum of a given set of nodes If necessary does string conversion with number round number round a n
10. FA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xmi xpath 1 17 Parts of the result lt hl gt What do we know about our participants lt h1 gt Here are some food preferences lt ul gt lt li gt Daniel prefers Sea Food lt img sro dolores 001 jpg gt lt be clear all gt lt li gt lt 1li gt Jonathan prefers Asian lt img sre dolores 002 jpg gt lt br clear al1l gt lt li gt lt li gt Bernadette lt li gt lt li gt Nathalie lt li gt lt ul gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 18 2 6 Location wildcards e Sometimes but not often it is useful to work with wildcards e You have to understand that only one rule will be applied element Rules with wildcards have less priority and this is why your rules are applied before the system default rules Find all child nodes of type XML element Syntax Find all child nodes including comments etc Syntax node Find all element attributes Syntax Find all text nodes Syntax text FYI The system built in default rules rely on wildcards This rule applies to the document root and all other elements see 2 9 Union of XPaths 28 lt xsl template match gt lt xsl apply templates gt lt xsl template gt Text and attribute values are
11. Intermediate XSLT and XPath xml xpath Intermediate XSLT and XPath Code xml xpath Author and version e Daniel K Schneider e Email Daniel Schneider tecfa unige ch e Version 1 0 modified 21 4 09 by DKS Prerequisites e Editing XML being able to use a simple DTD e Introductory XSLT xsl template xsl apply templates and xsl value of e Know about the role of XPath with respect to XSLT Availability http tecfa unige ch quides te files xml xpath pdf cies SOME RIGHTS RESERVED XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath xml xpath 1 2 Objectives e Understand XPath expressions e Learn some XSLT programming constructions conditions and loops e Being able to cope with most XML to HTML transformations Warning e XSLT is a rather complex language e believe that one could distinguish four levels of difficulty e Introductory level 1 e These slides concerns Level 2 XSLT i e XPath and XSLT conditional expressions loops etc e Level 3 is advanced XPath expressions and more exotic XSLT instructions e Level 4 is functional programming with templates Disclaimer e There may be typos sorry and mistakes sorry again e Please also consult a textbook XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath xmil xpath 1 3 Contents 1 Introduction to XML Path Languag
12. K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 8 2 2 The formal specification of an XML Path e is very complex i e has about 39 clauses and is very difficult to understand e Some expressions shown here are beyound the scope of this class don t panic Here are the first few clauses expressed in EBNF syntax XML 1 LocationPath RelativeLocationPath AbsoluteLocationPath 2 AbsoluteLocationPath RelativeLocationPath AbbreviatedAbsoluteLocationPath 3 RelativeLocationPath Step RelativeLocationPath Step AbbreviatedRelativeLocationPath 4 Step AxisSpecifier NodeTestPredicate AbbreviatedStep 5 AxisSpecifier AxisName AbbreviatedAxisSpecifier 6 AxisName ancestor ancestor or self attribute child descendant descendant or self following following sibling namespace parent preceding preceding sibling self 7 NodeTest NameTest NodeType processing instruction Literal 8 Predicate PredicateExpr 9 PredicateExpr Expr oy Bee Ignore this page Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 9
13. S lt recipe_name gt lt meal gt Dinner lt meal gt lt ingredients gt lt item gt 3 taco shells lt item gt lt item gt 1 pkg hamburger 100g lt item gt lt ingredients gt lt directions gt lt bullet gt Oven on at 180 degrees lt bullet gt lt bullet gt Cut up lettuce lt bullet gt lt directions gt lt recipe gt lt recipe id r2 gt lt list gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 4 Multiple templates for one element xml xpath 1 33 XSLT segment file douglas recipes xsl lt xsl template match list gt lt html xmlns http www w3 0org 1999 xhtm1 gt lt head gt lt title gt Recipes lt title gt lt head gt lt body bgcolor ffffff gt lt hl1 gt Recipes lt h1 gt lt h2 gt Contents lt h2 gt lt p gt lt xsl apply templates select recipe mode toc gt lt p gt lt xsl apply templates select recipe gt lt body gt lt html gt lt xsl template gt e We define 2 rules for recipe One will have a mode toc e The next rule will create the table of contents e It will use the recipe_name as link e t will use the id attribute to create an internal href link lt xsl template match recipe mode toc gt lt p gt lt a href id gt lt xsl value of select recipe_name gt lt a gt lt p gt lt xsl template gt e This rule is almost normal except that we insert a name anchor lt
14. Task 47 9 2 Approximate evaluation grid 48 XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 1 Introduction to XML Path Language xml xpath 1 5 1 Introduction to XML Path Language 1 1 Definition and history e XPath is a language for addressing parts of an XML document e In support of this primary purpose it also provides basic facilities for manipulation of strings numbers and booleans e XPath uses a compact non XML syntax to facilitate use of XPath within URIs and XML attribute values e XPath gets its name from its use of a path notation for navigating through the hierarchical structure of an XML document similar to the Unix Wi Internet model of path names History e XPath was defined at the same time as XSLT nov 1999 e Initally it was developped to support XSLT and XPointer XML Pointer Language used for XLink XInclude etc Specification url XPath 1 0 http www w3 org TR xpath nov 1999 e Used by XSLT 1 0 url XPath 2 0 http www w3 org TR xpath20 Jan 2007 url XPath 2 0 Functions and Operators hitp www w3 org TR xquery operators e XPath 2 0 is a superset of XPath 1 0 e Used by XSLT 2 0 and XQuery and other specifications XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 1 Introduction to XML Path Language xml xpath 1 6 1 2 XSLT and XPath e Each time a g
15. XML_element_name Descendant of the root Syntax XML_element_name Descendant of a node Syntax XML_element_name XML_element_name Parent of a node Syntax Un far cousin of a node Syntax XML_element_name XML_element_name XML_element_name XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 11 Example 2 1 Extracting titles from an XML file An XML document xpath jungle xml is used throughout the XPath chapter e We nly show part al the document XML fragment gt lt project gt lt title gt The Xpath project lt title gt lt problems gt lt problem gt lt title gt Initial problem lt title gt lt description gt We have to learn something about Location Path lt description gt lt difficulty level 5 gt This problem should not be too hard lt difficulty gt lt problem gt lt problem gt lt title gt Next problem lt title gt lt description gt We have to learn something about predicates lt description gt lt difficulty level 6 gt This problem is a bit more difficult lt difficulty gt lt problem gt lt problems gt lt project gt Task e We would like to get a simple list of problem titles XSLT Templates on next slide XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath S
16. ate matches either the root element i e or just any element lt xsl template match gt lt xsl apply templates gt lt xsl template gt e Often this is used to simplify apply templates or even templates themselves E g the following rules applies to both description and para elements lt xsl template match para description gt lt p gt lt xsl apply templates gt lt p gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 29 2 10List of commonly used XPath expressions pane Type of path Example path Example matches name child element name project lt project gt lt project gt child child project title lt project gt lt title gt lt title gt root element descendant project title lt project gt lt problem gt lt title gt lt title gt title lt root gt lt title gt lt title gt any place wildcard title lt bla gt lt title gt lt title gt and lt bli gt lt title gt lt title gt or operator title head lt title gt lt title gt or lt head gt lt nead gt All elements root children and attributes current element al parent element problem lt project gt attr attribute name id lt xyz Id test gt lt xyz gt element attr at
17. ce lt lt gt gt or Examples e Return all exercise titles with a note bigger than 5 exercise note gt 5 title e Find elements that have a given attribute with a given value Syntax XML_element_name attribute_name value solutions item val low syntax e Example XSLT template that will match all item elements with val low lt xsl template match item val low gt lt xsl value of select gt lt xsl template gt Note Usually expression also contain functions see 2 8 XPath functions 23 e Return last five elements of a list author last 4 lt position and position lt last e Return all Participant nodes with contents of FirstName bigger than 7 characters Participant string length FirstName gt 8 XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 21 Example 2 3 Retrieve selected elements The XSLT stylesheet file xpath jungle 3 xsl url http tecfa unige ch guides xml examples xpath xpath jungle 3 xsl url http tecfa unige ch guides xml examples xpath xpath jungle 3 xml lt xsl template match gt lt html gt lt body bgcolor FFFFFF gt lt hl gt Retrieve selected elements lt h1 gt Here is the name of participant two lt ul gt lt li gt lt xsl value of select participant 2 FirstName
18. constructs are out of scope for this e Just for your information present a small example that demonstrates function calls Example 7 1 Display ingredients with increasing fonts XML file http tecfa unige ch guides xml examples xpath ingredient list2 xml lt ingredients gt lt item gt 3 taco shells lt item gt lt item gt 1 pkg hamburger 100g lt item gt lt item gt Taco mix lt item gt lt item gt 50 g of cheese lt item gt lt item gt 1 tomato lt item gt lt item gt 1 carot lt item gt lt item gt Lettuce lt item gt lt ingredients gt XSLT file http tecfa unige ch quides xml examples xpath ingredient list2 xsl lt xsl template match ingredients gt lt html gt lt head gt lt title gt Ingrediant list lt title gt lt head gt lt body bgcolor ffffff gt Here is a list of recipe ingrediants lt xsl for each select item gt lt xsl call template name display_item gt lt xsl with param name position select position gt lt xsl call template gt lt xsl for each gt lt body gt lt html gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 7 Moving on xmi xpath 1 45 lt you can change the value of these 2 parameters gt lt xsl param name initial_size select 5 gt lt xsl param name multiplier select 3 gt lt xsl template name display_item gt lt xsl param name po
19. d apply e g to building references lt xsl template match reference gt lt xsl apply templates select author ref title edition publisher pubPlace publicationYear gt lt xsl template gt lt xsl template match author edition publisher pubPlace publicationYear gt lt xsl apply templates gt lt xsl if test position last gt lt xsl if gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 5 Conditional XSLT and whitespaces xml xpath 1 38 5 2 if then else Syntax lt xsl choose gt lt Content xsl when xsl otherwise gt lt xsl choose gt lt xsl when test boolean expression gt lt Content template gt lt xsl when gt lt xsl otherwise gt lt Content template gt lt xsl otherwise gt Example 5 2 Animal colors with xsl choose XML file http tecfa unige ch lt example gt lt title gt Animals with colors lt title gt lt list gt lt animal color black gt Panther lt animal gt uides xml examples xpath animals xml lt animal color complicated with spots gt Panther lt animal gt lt animal color white gt Polar bear lt animal gt lt animal color green gt Frog lt animal gt lt animal gt Cow lt animal gt lt list gt lt example gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Inte
20. e 5 1 1 Definition and history 5 1 2 XSLT and XPath 6 2 The XPath Syntax and the document model 7 2 1 Xpath Syntax 7 2 2 The formal specification of an XML Path 8 2 3 The document model of XPath 9 2 4 Element Location Paths 10 Example 2 1 Extracting titles from an XML file 11 2 5 Attribute Location Paths 14 Example 2 2 Extract a list and also insert an html img link from an attribute 15 2 6 Location wildcards 18 2 7 XPaths with predicates 19 Example 2 3 Retrieve selected elements 21 2 8 XPath functions 23 Example 2 4 Computation of an average 25 Example 2 5 Find first names containing nat 27 2 9 Union of XPaths 28 2 10List of commonly used XPath expressions 29 3 XSLT reminder 30 Example 3 1 Dealing with pictures 30 4 Multiple templates for one element 32 5 Conditional XSLT and whitespaces 35 5 1 Simple if 35 Example 5 1 Display lists the last element differently with xsl if 35 5 2 lIf then else 38 Example 5 2 Animal colors with xsl choose 38 6 Looping 40 Example 6 1 Translating database query output into an HTML table 40 XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath xml xpath 1 4 6 1 Looping with a sort 42 Example 6 2 Sort a participants list according to qualification 42 7 Moving on 44 Example 7 1 Display ingredients with increasing fonts 44 8 Next steps 46 8 1 Reading 46 8 2 Next modules 46 9 Homework mini project 5 47 9 1
21. e lt th gt lt th gt food lt th gt lt th gt work lt th gt lt th gt love lt th gt lt th gt leisure lt th gt lt tr gt lt xsl for each select ROW gt lt tr gt lt td gt lt xsl value of select id gt lt td gt lt td gt lt xsl value of select login gt lt td gt lt td gt lt xsl value of select fullname gt lt td gt lt td gt lt xsl value of select food gt lt td gt lt td gt lt xsl value of select work gt lt td gt lt td gt lt xsl value of select love gt lt td gt lt td gt lt xsl value of select leisure gt lt td gt lt tr gt lt xs1l for each gt lt table gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 6 Looping xml xpath 1 42 6 1 Looping with a sort e xsl sort can sort a list according to several criteria can be used more than once lt xsl sort select Xpath data type text number order ascending descending case order upper first lower first gt Example 6 2 Sort a participants list according to qualification XML fragment file http tecfa unige ch lt xml version 1 0 gt lt xml stylesheet href participants xsl type text xs1l gt lt participants gt lt participant gt lt FirstName gt Daniel lt FirstName gt lt qualification gt 8 lt qualification gt lt description gt Daniel will
22. eva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 14 2 5 Attribute Location Paths Find an attribute of a child element of the current context Syntax attribute_name Example Q val Find attributes of an element in a longer location path starting from root Syntax element_name element_name attribute_name Example project problems solutions item val Find attributes in the whole document Syntax attribute_name XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 15 Example 2 2 Extract a list and also insert an html img link from an attribute url http tecfa unige ch qguides xml examples xpath xpath jungle 2 xsl url http tecfa unige ch guides xml examples xpath xpath jungle 2 xml XML fragment lt participants gt lt participant gt lt FirstName gt Daniel lt FirstName gt lt qualification gt 8 lt qualification gt lt description gt Daniel will be the tutor lt description gt lt FoodPref picture dolores_001 jpg gt Sea Food lt FoodPref gt lt participant gt lt participant gt lt FirstName gt Jonathan lt FirstName gt lt qualification gt 5 lt qualification gt lt FoodPref picture dolores_002 jpg gt Asian lt FoodPref gt lt participant gt lt participant gt lt FirstName gt Bernadette lt FirstName
23. iven XSLT instruction needs to address refer to parts of an XML document we use XPath expressions e XPath expressions also can contain functions simple math and boolean expressions e Typically XPath expressions are used in match select and test attributes Simple XPath expressions ser ef rd I lt xsl template match page gt J r lt xsl apply templates select title gt lt xsl template gt I lt xsl value of select comment author gt lt xsl if test position sc oe a ay Complex XPath expression XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 7 2 The XPath Syntax and the document model 2 1 Xpath Syntax Primary relatively simple XPath expressions Location Path tests Axis Node Test Zero or more predicates where to look for tag attribute name further tests child solutions child item attribute val low solutions item val low Result of an Xpath e Can be various data structures e g sets of nodes a single node a number etc There are two notations for location paths 1 abbreviated less available options e e g para is identical to child para 2 unabbreviated not presented in these slides e e g child para would define the para element children of the current context node XML Daniel
24. l programming constructs to handle if and if then else situations e When you use XPath functions like position you have to be very careful about whitespaces 5 1 Simple if Syntax for xsl if lt xsl if test boolean_expression gt lt l Content or xsl instructions here gt lt xsl if gt Example 5 1 Display lists the last element differently with xsl if e Warning This only works in standards compliant browsers if you either eliminate whitespaces from your XML file or if you add the following XSL instruction at the beginning Syntax lt xsl strip space elements element_name element_name gt lt xsl strip space elements ingredients gt XML file http tecfa unige ch quides xml examples xpath ingredient list xml lt xml version 1 0 encoding ISO 8859 1 gt lt xml stylesheet href ingredient list xsl type text xs1 gt lt ingredients gt lt item gt 3 taco shells lt item gt lt item gt 1 pkg hamburger 100g lt item gt lt item gt Taco mix lt item gt lt item gt Lettuce lt item gt lt ingredients gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 5 Conditional XSLT and whitespaces xml xpath 1 36 XSL file http tecfa unige ch quides xml examples xpath ingredient list xsl lt xsl template match ingredients gt lt html xmlns http www w3 org 1999 xhtml gt lt head gt lt title gt Ingrediant list lt
25. lains a user how to use your DTD and stylesheet A 1 page report that discusses your implementation e To get a B Produce a useful X HTML document from valid XML e To get an A e Create a useful DTD an ergonomic XSLT a useful manual and a report e Alternatively Create a really difficult XSLT and a good report XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 9 Homework mini project 5 xml xpath 1 49 Submission format e Electronic copies to be uploaded to the worlclassroom Please make sure to submit all elements In addition it is always good to use names like ex5 your_name xml XML your_name your_name your_name your_name xml XSl dtd doc pdf html Chose the format you like optional Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 9 Homework mini project 5 xml xpath 1 50 XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09
26. rmediate XSLT and XPath 5 Conditional XSLT and whitespaces xml xpath 1 39 XSLT template for animal file http tecfa unige ch lt xsl template match animal gt lt li gt lt xsl choose gt lt xsl when test color black gt lt p style color black font weight bold gt lt xsl value of select gt lt p gt lt xsl when gt uides xml examples xpath animals xsl lt xsl when test color green gt lt p style color green font weight bold gt lt xsl value of select gt lt p gt lt xsl when gt lt xsl otherwise gt lt p style color cyan gt lt xsl value of select gt lt p gt lt xsl otherwise gt lt xsl choose gt lt li gt lt xsl template gt HTML some lt li gt lt p style color black font weight bold gt Panther lt p gt lt li gt lt li gt lt p style color cyan gt Panther lt p gt lt li gt lt li gt lt p style color cyan gt Polar bear lt p gt lt li gt lt li gt lt p style color green font weight bold gt Frog lt p gt lt li gt lt li gt lt p style color cyan gt Cow lt p gt lt 1li gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 6 Looping xmi xpath 1 40 6 Looping e Most of the time you do looping with templates as in all previous examples e There is a xsl for each looping construct It is useful to write more compact code to use it with sorting and f
27. sition gt lt xsl variable name font_size select Sinitial_size Sposition Smultiplier gt lt xsl variable name color gt lt xsl choose gt lt xsl when test Sposition mod 3 0 gt lt xsl text gt yellow lt xsl text gt lt xsl when gt lt xsl when test Sposition mod 3 1 gt lt xsl text gt red lt xsl text gt lt xsl when gt lt xsl when test Sposition mod 3 2 gt lt xsl text gt blue lt xsl text gt lt xsl when gt lt xsl otherwise gt lt xsl text gt green lt xsl text gt lt xsl otherwise gt lt xsl choose gt lt xsl variable gt lt p style color color font size font_size pt gt lt xsl value of select gt lt br gt Pos lt xsl value of select Sposition gt Multip lt xsl value of select Smultiplier gt Font size lt xsl value of select S font_size gt lt p gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 8 Next steps xml xpath 1 46 8 Next steps 8 1 Reading Carey pp 297 317 317 335 Optional 1 or 2 case problems 8 2 Next modules e XML Schema XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 9 Homework mini project 5 xml xpath 1 47 9 Homework mini project 5 Due next Monday 9 1 Task Create an XSLT transformation to X HTML e Create or reuse a DTD
28. tribute of child project id lt project id test gt lt project gt attr value value of attribute list type ol lt list type ol gt lt list gt position positon prEN position in parent last number of elements 7 last within a context position last Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 XML Intermediate XSLT and XPath 3 XSLT reminder xml xpath 1 30 3 XSLT reminder e In most situations writing simple XSLT rules will do e Do not attempt to use looping constructs etc when you don t have to e There is no special magic for dealing with images links stylesheets etc Simply e look at your XML e figure out how to translate into equivalent HTML or whatever you are translating into Example 3 1 Dealing with pictures XML file with picture file names file images xml lt xml version 1 0 gt lt xml stylesheet href images xsl type text xs1 gt lt page gt lt title gt Hello Here are my images lt title gt lt list gt lt image gt dolores_001 jpg lt image gt lt image gt dolores_002 jpg lt image gt lt image gt dolores_002 jpg lt image gt lt image2 gt scrolls jpg lt image2 gt lt image2 gt scrolls jpg lt image2 gt lt image3 source dolores_002 jpg gt Recipe image lt image3 gt lt list gt lt comment gt Written by DKS lt comment gt lt page gt XML Daniel K Schneider Webster Uni
29. umber e g 1 4 becomes 1 and 1 7 becomes 2 translate string1 string2 string3 translates string1 by substituting string2 elements with string3 elements See next slides for examples XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 25 Example 2 4 Computation of an average url http tecfa unige ch guides xml examples xpath xpath jungle 4 xsl url http tecfa unige ch guides xml examples xpath xpath jungle 4 xml e We would like to compute the average of participant s qualifications lt participant gt lt FirstName gt Daniel lt FirstName gt lt qualification gt 8 lt qualification gt lt participant gt The XSLT stylesheet file xpath jungle 4 xsl e We compute the sum of a node set and then divide by the number of nodes lt xsl template match gt lt html gt lt body bgcolor FFFFFE gt lt hl gt Qualiticataion level of participants lt hi gt Average is lt xsl value of select sum participant qualification div count participant qualification gt lt body gt lt html gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 26 HTML result lt html gt lt body bgcolor FFFFFF gt lt h1 gt Qualification level of
30. unctional programming constructs not explained here Syntax xsl for each select XPath Example 6 1 Translating database query output into an HTML table XML file http tecfa unige ch guides xml examples xpath rowset xml e Typical database query output may look like this e E g survey data will present as a series of rows of the same length e We would like to translate each ROW into a table row and each child as a table cell lt xml version 1 0 encoding ISO 8859 1 gt lt xml stylesheet href rowset xsl type text xsl gt lt page gt lt ROWSET gt lt ROW gt lt id gt 1 lt id gt lt login gt test lt login gt lt fullname gt Joe Test lt fullname gt lt food gt 3 lt food gt lt work gt 4 lt work gt lt love gt 5 lt love gt lt leisure gt 2 lt leisure gt lt ROW gt lt ROW gt lt id gt 2 lt id gt lt login gt test2 lt login gt lt fullname gt Janine Test lt fullname gt lt food gt 3 lt food gt lt work gt 4 lt work gt lt love gt 6 lt love gt lt leisure gt 2 lt leisure gt lt ROW gt lt ROWSET gt lt page gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 6 Looping xml xpath 1 41 XSLT file http tecfa unige ch quides xml examples xpath rowset xsl lt xsl template match ROWSET gt lt table border 2 cellspacing 1 cellpadding 6 gt lt tr gt lt th gt id lt th gt lt th gt Login lt th gt lt th gt Full Nam
31. versity Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 3 XSLT reminder xml xpath 1 31 XSLT stylesheet file images xsl lt xsl template match list gt Apply templates for image elements lt xsl apply templates select image gt This will only insert the first image2 element contents it finds lt p gt lt img src image2 gt lt p gt And another template for a tag image3 element with an attribute lt xsl apply templates select image3 gt lt xsl template gt e This rule will insert the content of the image element into the value of src lt xsl template match image gt lt p gt lt img src gt lt p gt lt xsl template gt e This rule will insert the value of the source attribute into the value of src and also insert the contents of the the image3 element lt xsl template match image3 gt lt p gt lt img src source gt lt xsl value of select gt lt p gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 4 Multiple templates for one element xml xpath 1 32 4 Multiple templates for one element e XSLT allows to define multiple rules for the same XPath by using the mode attribute e This construct is frequently used to produce table of contents Parts of the XML file file douglas recipes xml lt list gt lt recipe id r1 gt lt recipe_name gt TACO
32. xsl template match recipe gt lt hl gt lt a name id gt lt xsl value of select recipe_name gt lt a gt lt hl1 gt lt xsl apply templates select meal gt lt xsl apply templates select ingredients gt lt xsl apply templates select directions gt lt xsl template gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 4 Multiple templates for one element xml xpath 1 34 HTML result lt xml version 1 0 encoding iso 8859 1 gt lt DOCTYPE html PUBLIC W3C DTD XHTML 1 0 Transitional EN http www w3 org TR xhtml1l DTD xhtmll transitional dtd gt lt html xmilns http www w3 org 1999 xhtml gt lt head gt lt title gt Recipes lt title gt lt head gt lt body bocolor iLiLiri gt lt hl gt Recipes lt h1 gt lt h2 gt Contents lt h2 gt lt p gt lt p gt lt a href r1 gt TACOS lt a gt lt p gt lt p gt lt a href r2 gt VOL AU VENT one person lt a gt lt p gt lt p gt lt hr gt lt h1 gt lt a name r1 gt TACOS lt a gt lt h1 gt lt p gt Type Dinner lt p gt XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 5 Conditional XSLT and whitespaces xml xpath 1 35 5 Conditional XSLT and whitespaces e Although many conditionals can be expressed with XPath and according selection rules XSLT provides 2 typica
33. yntax and the document model xml xpath 1 12 1 XSLT template for project XML root element file xpath jungle 1 xsl lt xsl template match project gt lt html gt lt body bgcolor FFFFFE gt lt hl gt lt xsl value of select title gt lt h1 gt Here are the titles of our problems lt ul gt lt xsl apply templates select problems problem gt lt ul gt lt body gt lt html gt lt xsl template gt e The XPath of the match means applies to project element node descendant of root node e Execution context of this template is therefore the element project e xsl apply templates will select a rule for descendant problem 2 XSLT template for the problem element lt xsl template match problems problem gt lt li gt lt xsl value of select title gt lt 1li gt lt xsl template gt e This second rule will be triggered by the first rule because problems problem is indeed a descendant of the project element XML Daniel K Schneider Webster University Geneva TECFA University of Geneva 21 4 09 Intermediate XSLT and XPath 2 The XPath Syntax and the document model xml xpath 1 13 3 Result HTML lt html gt lt body bgcolor FFFFFF gt lt hl gt The Xpath project lt hl1 gt Here are the titles of our problems lt ul gt lt li gt Initial problem lt li gt lt li gt Next problem lt li gt lt ul gt lt body gt lt html gt XML Daniel K Schneider Webster University Gen

Download Pdf Manuals

image

Related Search

Related Contents

Dise˜no de un seccionador con cánula de aspiraci´on para una  User's Manual G31 Series  AMRESCO BlueStep 取扱説明書  この取扱説明書は、脳~ず最終ューザ様までお届けください。 胴  Ouvre - Sadimato.com  カタログダウンロード - 象印ラコルト株式会社  iHome iDM3  Vantec FireWire 400, 4 ports, PCI  

Copyright © All rights reserved.
Failed to retrieve file