Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COMP9322 XSLT Lab A Lab Session on XSLT Introduction In this lab, you will work through some exercises in XSLT. Saxon - XSLT Processor First, create a directory called saxon. Saxon is freely available on the Web. Here is a local copy of the version 9.5.1 (Home Edition). Unzip the file into the 'saxon' directory you have created. It has a complete documentation and samples. In this lab, you are interested in using the saxon9he.jar file. Downloading exercise files Download the source code of the examples. Unzipping the file will create a directory called xslexample. All XSLT files and related XML documents from the lecture will be there. Copy saxon9he.jar to the xslexample directory. Trying out the XSLT examples Check that JAVA_HOME is set. Test the examples and examine the output files. You should also examine the XSLT files and see if you can understand the transforming process in each example (Any doubts? Ask questions.) java -jar saxon9he.jar edmond.xml edmond.xsl > out.html java -jar saxon9he.jar books.xml BIBstyle2.xsl > out.html (Note: BIBstyle2 uses apply-templates element) java -jar saxon9he.jar books.xml BIBstyle.xsl > out.html (Note: BIBstyle uses for-each element) java -jar saxon9he.jar Phonebook.xml p2p.xsl > out.xml java -jar saxon9he.jar gem.xml MyFriends.xsl > out.html java -jar saxon9he.jar gem.xml GEMholiday.xsl > out.html Developing your own XSLT programs gem.xml Now write an XSLT program over the gem.xml file that produces a listing of all friends and the number of holidays each has taken. The output should be something like this:
Sue 3
Bill 3
Doug 2
sdb.xml Now write some XSLT programs over the sdb.xml file that produces the given outputs as shown: Generate a listing of Assessment Items with weight greater than 25. XML 30 Exam 60 Generate a listing of students, and the number of assessment result items for each student (hint: use need xslt:variable to 'join' Students and Result), 871 3 862 3 869 3 854 2 831 0 872 3 868 3 Sorting XSLT output In this section, we will examine the ability to sort the output produced as part of an XSL transformation. Open the XML data file aus.xml. You should see a list of all eight Australian states and territories. They are classified as "divisions", and each division's name, abbreviation, capital city, area and population is provided. Try aussort1.xsl via: java -jar saxon9he.jar aus.xml aussort1.xsl > out.html Examine the output file. You should see a list of all divisions in descending order of area (in square kilometres). Open the XSLT program aussort1.xsl. Note the use of the xsl:sort element to sort the table in descending order of area. Also, note the location of the sort element (ie., inside 'apply-templates') In a new browser window, open this xslt reference link. You should then scroll down to xsl:sort. Read up about the sort options in XSLT. Now change the program so that it sorts the output by ascending order of population. Providing parameters to XSLT programs We will often wish to parameterise an XSLT program. A simple example would occur when we only want to select some of the possible output that might result from scanning the document. In the next program, we will display information about just one of the states or territories, depending upon a value supplied by the user. The value will be the abbreviation, for example, VIC for Victoria. Try the following: java -jar saxon9he.jar aus.xml ausparam1.xsl short=QLD > out.html Examine the output file. You should see information specific to Queensland. Open the file ausparam1.xsl file. You should make sure you understand the following features of the program: The declaration of the parameter "short". The use of the parameter in the form $short in the

element. The use of the expression division/name[@abbr=$short] to ensure that the only divisions selected are those with a name that has an "abbr" attribute equal to the parameter provided. This expression has the effect of choosing name elements for template matching. The consequent appearance of match="name" to catch these elements. The use of select="." to display the name element. The use of select="../cap", for example, to display sibling elements of name. Indexing elements in XSLT We can tell an XSLT program that we want to build an index structure. This could be useful if the transformation requires repeated access to elements. Suppose that, our program needs to frequently access divisions by means of abbreviations such as QLD. We can define a key that may then be used to access division by means of their abbreviations. Try the following: java -jar saxon9he.jar aus.xml auskey1.xsl short=QLD > out.html Examine the output file. You should see information specific to Queensland, just as we did with the previous program. Open the file auskey1.xsl file. You should make sure you understand the following features of the program: The use of the xsl:key element to state our need for an index, and its three attributes: (1) the name="by-abbr" which allows us to name the key structure; (2) the match="name" which indicates that we want to access name elements by means of this structure; and (3) the use="@abbr", which indicates that we want to access these elements by means of their abbreviation. The use of select="key('by-abbr',$short)" to indicate that we only want to access name elements that match the abbreviation indicated in the short parameter.