XSLT CLASS FILE ORDER: one.txt = the elements and one attribute for demo source.xml = the elements with data as .xml demo1.xsl = stylesheet demo - integrates html + one template - view structure only demo2.xsl = source.xml with demo transformation [Transformations: a) in the browser b) by JSP c) by Java [or other]] %java javademo1 source.xml demo1.xsl formatted.html ------------ What stylesheets look for: attributes comments elements namespaces processing instructions root node text ------------ demo3.xsl - here uses "current node operator" = the dot . [compare demo2.xsl's "value-of select="name" to for-each ... value-of ... demo3.xls's "value-of select="."/> ] ---- MATCHING NODES BY USING "match" ROOT NODE = / NOTICE ROOT IS ALSO courses BUT HERE ID's BY SYMBOL / HANDLING ELEMENTS by name of element NOTICE THAT courses IS ALSO THE ROOT - but ID'd by name SELECTING PARENT/CHILD

SELECTING EVERYONE BY WILDCARD * SELECTING PARENT/GRANDCHILD ALTERNATIVE APPROACH ------------------ ATTRIBUTES cannot be uniquely identified so need symbol to find an attribute in the current node - only the text in that node appears, not the name of the node: e.g., 25 SPECIAL CASE: the ID attribute ---------------------- MULTIPLE MATCHES: - getting and controlling all your data --------------------- SPECIAL NOTE: demo4.xsl is an example of applying all the data in an HTML table. demo4.xml is a copy of demo2.xml ----------------- demo5.xsl (using demo5.xml) MATCHING USING "OR" (get element A OR element B) ... ----------- /* ******************************************** [THIS SECTION IS A MORE ADVANCED LEVEL] *********************************************** */ XPATH EXPRESSIONS: the above examples work fine, but they're long to create. XPath expressions are more powerful, but more complicated. To MATCH ANY ELEMENT THAT HAS AN ATTRIBUTE, e.g., book that has an authorID attribute [look for any element (*) and in each element check for an attribute with the name "authorID"> USES: "location path" made up of "location steps" "location steps" are made of "axis", "node tests", and 0+ "predicates" Example: / = location step -- find the child node // = find any dependent (child) node child::course[position() = 2] means "find the second child element of the context node (the current node being processed is called the "context node") Example: model: /descendant::MYNODE/child::MYSUBNODE (replace MYNODE with your element name) example: /descendant::course/child::name gets the element of the element ------------- USING PREDICATES - child::course[position() = 1] uses the predicate [position() = 1] to select not just the child but the FIRST child element Predicates: let you move around the axis and also to select certain types: Boolean, Node sets, numbers, and strings. AXES: if there's a node called "person" that has an element "lastName" then "child::lastName" means get the current node (person) element of "lastName" Here's a list of axes commands - SUBSTITUTE THEM IN THE BELOW EXAMPLES FOR PRACTICE: ancestor ancestor-or-self attribute child descendant descendant-or-self following following-sibling namespace parent preceding preceding-sibling self --------- ["name" is the node test; "child" is the axis] THIS IS THE SAME AS: ---------------- NODE TESTS: after you find define the axis, you can test it. * = wildcard example: child::*/child::prereq will select all elements that are grandchild of the current node. OTHER TESTS: comment() node() processing-instruction() text() ----------------------------------------------- USING PREDICATES BOOLEAN: != is not equal to < less than <= less than or equal to = equals > greater than >= is greater than or equal to EXAMPLE: get only the course nodes after the first 3: This means "match all the elements AFTER the first three. -------------------------- NODE SETS: last() position() count(node-set) id(ID) local-name(node-set) namespace-uri(node-set) name(node-set) EXAMPLE: Get courses between #3 and #10: EXAMPLE: get the very last node: SEE demo6.xsl (demo6.xml) EXAMPLE ---------------------------- [THERE WILL BE MORE ABOUT MATCHING, but first...] - this is a boolean: Yes or No, True or False many times you need to test for some condition... EXAMPLE: want to know if the node is first or last so that we can change the display of the data ... add
before the first node and after the last. see demo7.xsl and demo7.xml ---------------- CHOOSING: choose gives more options: "when" some condition is met, do this. EXAMPLE: you want to see a node in a certain color only if some special condition or need .... see demo8.xsl demo8.xml ------------------------ SORTING: e.g., inside the element EXAMPLE:

Course List

Name Course Track
------------------------ CHOOSING OUTPUT: XSLT will output to different document types (xml, html, txt, and **other MIME types**!) and also change the character encoding, kind of indentation, remove stuff, indicate the version, etc. ================================================= ================================================= ================================================= Formatting Objects --> PDF FILES! Using the same program (javademo.java) we can convert the .xml file, using an .xsl sheet, into a .fo file [formatting objects] Then the .fo file can be fed to "FOP" (a free program but they're easy enough to write) that will generate a .pdf Take a look at demo-fo.xml - same as the other versions but without the stylesheet instruction. Now look at the .xsl that will be used to create the .fo file: demo-fo.xsl Using the java demostration program (javademo1.java): %java javademo1 demo-fo.xml demo-fo.xsl demoOutput.fo [try it and see the output! If you can't run the program, at the bottom of this page is the outputted file.] Now, run FOP: %fop demoOutput.fo MyDemo.pdf ======================= END OF THE FILE ========================= = = file name: XSLT Class Examples Order = created: Feb 14, 2009 ================================================================= ------------------------------------------------------------------ demoOutput.fo contents: Name: Introduction to XML Number: LIS469 Enrollment: 25 Info Sci Instructor: Benoit LIS488 Name: Information Retrieval Number: LIS466 Enrollment: 20 Info Sci Instructor: Benoit LIS488 LIS415 Name: Art of the Americas Number: ART123 Enrollment: 30 General Education Instructor: Mahard none -------------------------------------------------------------------------- [NOTES about FOP - don't worry if you can't read all this.] FOP - download. On Mac: put the fop.jar file (from build folder) in /Library/Extension/Java Navigate to the folder where fop resides (e.g., /FOP2/fop-0.95-bin/fop-0.95/fop) In the command shell, type ./fop you'll see a help but then a serious error - ignore the error msg: Fop foo.fo foo.pdf Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line) Fop -xml foo.xml -xsl foo.xsl -pdf foo.pdf Fop -xml foo.xml -xsl foo.xsl -foout foo.fo Fop foo.fo -mif foo.mif Fop foo.fo -rtf foo.rtf Fop foo.fo -print or Fop -print foo.fo Fop foo.fo -awt See also: http://xmlgraphics.apache.org/fop/0.95/running.html#fop-script NOTE: see all the kinds of inputs and outputs? e.g., /Users/gbenoit/Desktop/FOP2/fop-0.95-bin/fop-0.95/examples/fo/basic/simple.fo ---------------------------------------------------------------- [Macintosh-169:FOP2/fop-0.95-bin/fop-0.95] gbenoit% pwd /Users/gbenoit/Desktop/FOP2/fop-0.95-bin/fop-0.95 [Macintosh-169:FOP2/fop-0.95-bin/fop-0.95] gbenoit% ---------------------------------------------------------------- EXAMPLE (on my Mac): [Macintosh-169:FOP2/fop-0.95-bin/fop-0.95] gbenoit% ./fop examples/fo/basic/simple.fo -txt helloFop.txt Dec 2, 2008 5:44:59 PM org.apache.fop.render.txt.TXTRenderer startRenderer INFO: Rendering areas to TEXT. Dec 2, 2008 5:45:00 PM org.apache.fop.render.txt.TXTRenderer stopRenderer INFO: writing out TEXT [Macintosh-169:FOP2/fop-0.95-bin/fop-0.95] gbenoit% --------------------------------