Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

Downloading exercise files

Download the source code of the .

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 thetransforming process in each example (Any doubts? Ask questions!)

  1. java -jar saxon9he.jaredmond.xml edmond.xsl > out.html
  2. java -jar saxon9he.jar books.xml BIBstyle.xsl> out.html (Note: BIBstyle uses for-each element)
  3. java -jar saxon9he.jar books.xml BIBstyle2.xsl> out.html (Note: BIBstyle2 uses apply-templates element)
  4. java -jar saxon9he.jar gem.xml MyFriends.xsl> out.html
  5. java -jar saxon9he.jar gem.xml GEMholiday.xsl> out.html
  6. java -jar saxon9he.jar scorers.xml gtimes.xsl> out.html
  7. java -jar saxon9he.jar scorers.xml gcount.xsl> out.html
  8. java -jar saxon9he.jar Phonebook.xml p2p.xsl> out.xml
  9. java -jar saxon9he.jar sdb.xml q1.xsl> out.xml
  10. java -jar saxon9he.jar sdb.xml q1a.xsl> out.xml (Note: the one with weight more than 25)
  11. java -jar saxon9he.jar sdb.xml q2.xsl> out.xml
  12. java -jar saxon9he.jar sdb.xml q3.xsl> out.xml
  13. java -jar saxon9he.jar sdb.xml q4.xsl> out.xml
  14. java -jar saxon9he.jar sdb.xml q5.xsl> out.xml
  15. java -jar saxon9he.jar sdb.xml q6.xsl> out.xml

Developing your own XSLT program

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:

  1. The output should be something like:
      Sue 3
      Bill 3
      Doug 2

Sorting XSLT output

In this section, we will examine the ability to sort the output produced aspart of an XSL transformation.

  1. 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.
  2. Try aussort1.xsl via:
    java -jar saxon9he.jar aus.xml aussort1.xsl > out.html
  3. Examine the output file. You should see a list of all divisions in descending order of area (in square kilometres).
  4. 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')
  5. In a new browser window, open this . You should then scroll down to xsl:sort.Read up about the sort options in XSLT.
  6. 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 resultfrom 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.

  1. Try the following:
    java -jar saxon9he.jar aus.xml ausparam1.xsl short=QLD >out.html
  2. Examine the output file. You should see information specific to Queensland.
  3. Open the file ausparam1.xsl file. You should make sure you understand the following features of the program:
    1. The declaration of the parameter "short".
    2. The use of the parameter in the form $short in the <H2> element.
    3. The use of the expression division/name[@abbr=$short] to ensure thatthe 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.
    4. The consequent appearance of match="name" to catch these elements.
    5. The use of select="." to display the name element.
    6. 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.

  1. Try the following:
    java -jar saxon9he.jar aus.xml auskey1.xsl short=QLD >out.html
  2. Examine the output file. You should see information specific to Queensland, just as we didwith the previous program.
  3. Open the file auskey1.xsl file. You should make sure you understand the following features of the program:
    1. 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 nameelements by means of this structure; and (3) the use="@abbr", which indicatesthat we want to access these elements by means of their abbreviation.
    2. 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.

Saxon - XQuery Processor

  1. Create a directory called saxon-xq.
  2. Here is a local copy of the of the SAXON XQuery processor with some XQuery sample queries and XML documents.
  3. Unzip the file into the 'saxon-xq' directory you have created.
  4. Test the examples and examine the output files using the following command

    java -cp saxon9he.jar net.sf.saxon.Query -q:$1 -o:$2

    where variable 1 represents the query file name ("sample files with extension .xq") and variable 2 represent the output file name.