Friday, 11 July 2014

Tomorrow's lab in DM2517: XPath

Overview

XPath is W3C's language for searching and selecting specific parts of XML structures, based on different criteria. You have used this in all XSLT related labs, where all attribute values in "select"-attributes and "match"-attributes are (simple) XPath expressions.
After completing this lab you will be able to write complex XPath expressions, which will be useful in the project and in your professional career (at least if your career involves XML).

Exercise

You want to analyze the results from the Swedish election 2006 for "Bengtsfors", and have received an XML-file containing the data (see XML data file in links below).
To do that you will use XPath and XSLT to select and display the data you are interested in.
Start by downloading the zip file in the link section below, unzip it and place the files in the same directory on your computer (in the end they must be placed in a directory you name "lab-xpath" within your DM2517 folder in your public_html folder. If you have problems opening the zip files try moving it to the "Temporary file area"-folder on the desktop before extracting.
Open the first XML file (Bengtsfors-ex1.xml) in your browser to see the result of the transformations (tested in Firefox and Chrome). Then start modifying the corresponding XSLT file to solve the first exercise. When you are done, open the file Bengtsfors-ex2.xml and start working with task 2 in the file xpath-ex2.xsl, and so on.
Note that you are NOT allowed to use xsl:for-each or xsl:if in this lab. This is to put more focus on XPath.
Help can be found in the links below.

Links

XPath exercise 1

Useful links/look at

Task

Make a template which writes the NAMEs of each ELECTORAL ("valdistrikt"). The resulting NAMEs should each be enclosed within P-tags.
_____________________________________________________________________________________

XPath exercise 2

Useful links/look at

Task

Make a template which, within P-tags, writes the NAMEs of each ELECTORAL ("valdistrikt") with the side condition (predicate) that the number of votes ("VOTES") in the district must be >300 .
_____________________________________________________________________________________

XPath exercise 3

Useful links/look at

Task

Make a template which matches all ELECTORALs ("valdistrikt") where the PARTY "M" has received more than 18% of the VALID votes. Then write the name ("NAME") of the corresponding ELECTORALs within P-tags.
_____________________________________________________________________________________

XPath exercise 4

Useful links/look at

Task

Make a template which matches "/" and, within a P-tag, writes the number of ELECTORALs where the party "M" has received more than 18% of the valid votes.
_____________________________________________________________________________________

XPath exercise 5

Useful links/look at

  • XPaths selecting attributes with side conditions for other attributes.

Task

Make a template which matches "/" and, within a P-tag, writes the REALNAME of the PARTY with the SHORTNAME "ENH".
_____________________________________________________________________________________

XPath exercise 6

Useful links/look at

Task

Make a template which matches "/" and, within a P-tag, writes the sum of the number of "PERSONALVOTES" which Fredrik Reinfeldt has received in the PERSONALELECTION.
_____________________________________________________________________________________

XPath exercise 7

Useful links/look at

Task

Make a template which, in a suitable way, finds all ocurrances in "PERSONELECTION" where "NAME" contains the letter "R" (names will appear several times in the results since they participate in several election districs, handle it at your own discretion). Output each name within P-tags.

No comments:

Post a Comment