PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/contribs/saxon6_5_5/doc/samples.html

https://bitbucket.org/haris_peco/debrief
HTML | 367 lines | 207 code | 77 blank | 83 comment | 0 complexity | 5cebc7cb1103ca53eeedb523a5427c58 MD5 | raw file
  1. <html>
  2. <head>
  3. <title>Sample SAXON Applications</title>
  4. </head>
  5. <body leftmargin="150" bgcolor="#ddeeff"><font face="Arial, Helvetica, sans-serif">
  6. <div align=right><a href="index.html">SAXON home page</a></div>
  7. <h1><font color="#FF0080"><big>Sample SAXON Applications</big></font></h1>
  8. <p>Several sample applications are included in the SAXON distribution.</p>
  9. <div><font face="Arial, Helvetica, sans-serif">
  10. <table width="100%">
  11. <tr>
  12. <td bgcolor="#0000FF"><font color="#FFFFFF"><big><b>Contents</b></big></font></td>
  13. </tr>
  14. <tr>
  15. <td VALIGN="top" bgcolor="#00FFFF">
  16. <a href="#trax">TrAX Examples</a><br>
  17. <a href="#servlet">Sample Servlet</a><br>
  18. <a href="#books">The Book List Stylesheet</a><br>
  19. <a href="#sql">SQL Extensions</a><br>
  20. <a href="#shakespeare">Shakespeare Example</a><br>
  21. <a href="#bible">Bible Example</a><br>
  22. <!--<a href="#applet">Applet Examples</a><br>-->
  23. <a href="#jdom">JDOM Example</a><br>
  24. </td>
  25. </tr>
  26. </table>
  27. </font></div>
  28. <hr>
  29. <h2><a name="trax">JAXP examples</a></h2>
  30. <p>Saxon supports the JAXP Java API (package javax.xml.transform), originally known as TrAX,
  31. for invoking the stylesheet processor.
  32. This API is useful when you want to write your own applications that invoke Saxon.</p>
  33. <p>A sample program that illustrates many features of the JAXP transformation interface is included in
  34. the distribution as <b>TraxExamples.java</b>. Source XML and XSL files for use with this
  35. program are included in the <b>trax</b> directory. To run the program, use the command:
  36. <p><code><strong><font color="#FF0080">
  37. cd $saxonhome/samples/trax
  38. java TraxExamples <br>
  39. </font></strong></code></p>
  40. <p>One of the examples shows how to use TrAX to process input from a third-party (non-Saxon)
  41. DOM document, and how to
  42. send output to a third-party DOM document. This example tests to see if either Crimson or
  43. Xerces is present on the classpath; if not, it should fail cleanly. You will need to change the
  44. source code and recompile if you want to
  45. use it with a different DOM implementation.</p>
  46. <hr>
  47. <h2><a name="servlet">SaxonServlet</a></h2>
  48. <p>This is a general-purpose servlet that takes the name of a source document and stylesheet
  49. as URL parameters, and processes the stylesheet to create a result document which is sent back
  50. to the browser for display. The result document may have any media type, though HTML and XML are
  51. the most likely.</p>
  52. <p>The servlet maintains a cache of prepared stylesheets; if the same stylesheet is used repeatedly,
  53. it is only parsed and validated once, which will often greatly improve performance. Prepared style
  54. sheets are thread-safe so they can be used to serve documents to several users concurrently.</p>
  55. <p>The URLs for the source document and the stylesheet document are supplied in the URL,
  56. which will typically take the form:</p>
  57. <p><code><strong><font color="#FF0080">
  58. http://server.com/servlets/SaxonServlet?source=doc.xml&style=sheet.xsl
  59. </font></strong></code></p>
  60. <p><i>Note: Internet Explorer may assume that if the URL ends with ".xml" or ".xsl", as in
  61. the above example, then the returned file will be XML - even if the media type in the HTTP header
  62. is set to "text/html". You can prevent this behaviour by adding an extra dummy parameter, for example
  63. "&amp;x=y".</i></p>
  64. <p>The source and style parameters identify the source document and stylesheet by URL.
  65. These are interpreted relative to the servlet context. This means that specifying
  66. say "style=/styles/styleone.xsl" in the URL will locate the stylesheet in this file relative to the
  67. root directory for the web server.</p>
  68. <p>The stylesheet is prepared the first time it is used, and held in memory in a cache. The cache may
  69. be cleared (for example, if a stylesheet has been changed) using a URL such as:</p>
  70. <p><code><strong><font color="#FF0080">
  71. http://server.com/servlets/SaxonServlet?clear-stylesheet-cache=yes
  72. </font></strong></code></p>
  73. <p>This code is provided purely as a sample, in the expectation that you will customise it to your
  74. particular site requirements.</p>
  75. <p><b>Security warning: if you implement a servlet that is prepared to execute untrusted stylesheets,
  76. be sure to disable the use of extension functions. Otherwise users will be able to submit stylesheets for execution
  77. that call arbitrary Java methods on your server, and these will typically have permissions to access private resources
  78. on the server machine.</b></p>
  79. <h2><a name="books">The Book List Stylesheet</h2>
  80. <p>This is a very simple sample stylesheet to illustrate several SAXON extensions.
  81. It uses the XML file <b>books.xml</b> (derived from a file issued originally by Microsoft).
  82. You will find this in the samples/data directory. The DTD is in <b>books.dtd</b></p>
  83. <p> There is a style sheet <b>books.xsl</b> that can be used to display the data: run this as
  84. follows, with the samples directory as the current directory:<p>
  85. <p><code><strong><font color="#FF0080">
  86. java com.icl.saxon.StyleSheet data/books.xml styles/books.xsl ><i>output.html</i><br>
  87. </font></strong></code></p>
  88. <p>This produces an HTML page showing the data. (The output isn't very pretty, if you have
  89. time to write an improved version, please send it in).</p>
  90. <p>The stylesheet takes a parameter named "top-author". This is the name of the "author of the week",
  91. and the default value is "Bonner". To run the stylesheet with a different top author, try:</p>
  92. <p><code><strong><font color="#FF0080">
  93. java com.icl.saxon.StyleSheet data/books.xml styles/books.xsl top-author=Danzig ><i>output.html</i><br>
  94. </font></strong></code></p>
  95. <p>It is possible (under those operating systems I know of) to write the author's name in quotes if
  96. it contains spaces, e.g. top-author="A. A. Milne".
  97. <p>There is another style sheet, books-csv.xsl, which
  98. converts the data into a comma-separated-values file.</p>
  99. <p>There is also a Java program ShowBooks.java (in the samples/java directory) to process the books.xml
  100. file without using XSL.
  101. This can be run directly from the command line. It produces on the standard output an
  102. HTML page showing the book list.</p>
  103. <p>To run the application, first make sure the compiled program (ShowBooks.class) is on
  104. the classpath, then execute</p>
  105. <p><code><strong><font color="#FF0080">
  106. java&nbsp; ShowBooks&nbsp; samples/data/books.xml&nbsp; &gt;test1.html<br>
  107. </font></strong></code></p>
  108. <h2><a name="sql">An SQL style sheet</a></h2>
  109. <p>SAXON implements the <i>element extensibility</i> feature of the XSLT standard, allowing
  110. you to extend the XSLT language by implementing your own stylesheet elements.
  111. A specimen extension has been written to illustrate this feature: it
  112. allows you to create stylesheets to load data into a relational database.</p>
  113. <p>To use the SQL extension elements in a stylesheet, you need to define a namespace prefix
  114. (for example "sql") in the extension-element-prefixes attribute of the xsl:stylesheet element,
  115. and to map this prefix to namespace URI that ends in "/com.icl.saxon.sql.SQLElementFactory".</p>
  116. <p>This extension defines four new stylesheet elements: <b>sql:connect</b>, <b>sql:insert</b>,
  117. <b>sql:column</b>, and <b>sql:close</b>: <ul>
  118. <li>sql:connect creates a database connection. It has attributes "driver", "database", "user", and "password",
  119. all of which are attribute value templates (so the values can be passed in as parameters).
  120. the driver attribute names the JDBC driver class to be used. The
  121. database name must be name that JDBC can associate with an actual database, and in the sample
  122. stylesheet this database must contain a a table "Book" with three character columns,
  123. "Title", "Author", and "Category".</li>
  124. <li>sql:insert performs an SQL INSERT statement. This causes a row to be added to the table identified
  125. by the "table" attribute.
  126. <li>sql:column is used as a child element of sql:insert, and identifies the name and value of a column
  127. to be included in the INSERT statement. The name of the column is identified by the "name" attribute,
  128. the value may be indicated either by evaluating the expression contained in the "select" attribute, or
  129. as the expanded contents of the sql:column element. The value is always interpreted as a String.
  130. (Remember this is purely a demonstration of extensibility, in a real system there would be a need to
  131. cater for SQL columns of other data types).</li>
  132. <li>sql:close closes the database connection.</ul>
  133. <p>A specimen stylesheet that uses these XSL extension is <b>books-sql.xsl</b>. This loads the contents
  134. of the books.xml file into a database table.<p>
  135. <p>To run this stylesheet you will need to do the following:</p>
  136. <ul>
  137. <li>Create a database (e.g. Microsoft Access) containing a table "Book" with three character columns,
  138. "Title", "Author", and "Category".</li>
  139. <li>Register this database as a JDBC data source. (If you use Microsoft Access, register it as an
  140. ODBC data source called, say, Books, and then it will automatically be available under JDBC as
  141. "jdbc:odbc:Books".</li>
  142. <li>Modify the &lt;sql:connect&gt; element in the stylesheet to specify the correct JDBC connection
  143. name for the database, and if necessary to supply a username and password. Alternatively you can
  144. supply the driver class, database name, username, and password as parameters on the command line.</li>
  145. <li>Execute the stylesheet from the command line, as follows:
  146. </ul>
  147. <p><code><strong><font color="#FF0080">
  148. java com.icl.saxon.StyleSheet data/books.xml styles/books-sql.xsl <br>
  149. </font></strong></code></p>
  150. <p>The database will be populated with data from the books.xml document.</p>
  151. <hr>
  152. <h2><a name="shakespeare">Shakespeare Example</a></h2>
  153. <p>This example works on an input file
  154. containing a Shakespeare play. You can use any of the Shakespeare plays in Jon Bosak's
  155. distribution at <a href="http://metalab.unc.edu/bosak/xml/eg/shaks200.zip">
  156. http://metalab.unc.edu/bosak/xml/eg/shaks200.zip</a>,
  157. but for convenience one of them, <em>Othello</em>, is included in the SAXON distribution
  158. (in the samples\data directory).</p>
  159. <h3>Shakespeare stylesheet</h3>
  160. <p>There is an XSL stylesheet, play.xsl, which processes an input play in XML and
  161. generates a set of linked HTML files (one for the play and one for each scene) in an
  162. output directory. To run this, create a directory (say playhtml) and execute the following
  163. from the command line:</p>
  164. <p><strong><font color="#FF0080"><code>
  165. cd samples<br>
  166. java com.icl.saxon.StyleSheet data/othello.xml styles/play.xsl dir=playhtml</code></font></strong></p>
  167. <p>The last parameter sets the value of the constant <strong>dir</strong> to the value <strong>playhtml</strong>;
  168. this constant is referenced from the style sheet when creating output files.</p>
  169. <!--
  170. <h4>Shakespeare play splitter</h4>
  171. <p>The Java application <strong>Loadplay.java</strong> splits a large XML document into
  172. several smaller linked XML documents.</p>
  173. <p>LoadPlay is a free-standing Java application which you can run from the command line.
  174. It takes as input a full Shakespeare play (for example <b>othello.xml</b> which is
  175. included in the distribution) and splits it into a number of separate XML files, one for
  176. the &quot;front material&quot; of the play, and one for each scene in the play.</p>
  177. <p>To run the program:</p>
  178. <ul>
  179. <li>Ensure it is on your CLASSPATH </li>
  180. <li>Ensure you have installed a SAX parser and registered it in the <code>ParserManager.properties</code>
  181. file </li>
  182. <li>Change directory to the <code>samples/data</code> directory containing the file <code>othello.xml</code>
  183. </li>
  184. <li>Enter the command line:<br>
  185. <font color="#FF0080"><code>java LoadPlay othello</code></font> </li>
  186. </ul>
  187. <p>A directory called &quot;othello&quot; is created and the play and scene files are
  188. generated within it. The program creates an output directory with the same name (othello)
  189. as the input file (othello.xml). The parameter should therefore be the name of the play
  190. file without its &quot;.xml&quot; suffix.</p>
  191. <h4>Shakespeare servlets</h4>
  192. <p>There are two programs (ShowPlay and ShowScene) for displaying the generated plays and scenes at a web browser.
  193. They are written as Java servlets, and can be run in any web server that supports the
  194. servlet interface.</p>
  195. <p>To run them, follow the following steps:</p>
  196. <ol>
  197. <li>Ensure your web server is properly configured to run servlets</li>
  198. <li>Copy the two files <strong>ShowPlay.class</strong> and <strong>ShowScene.class</strong>
  199. into the relevant servlets directory, which depends on which web server you are using. It
  200. may well be a directory called <strong>servlets</strong>.</li>
  201. <li>Create an HTML page which references the <strong>ShowPlay</strong> servlet. It should
  202. contain a hyperlink such as:<br>
  203. <strong><small><font color="#FF0080">&lt;a
  204. href=servlets/ShowPlay?dir=plays&amp;play=othello&gt;Othello, the Moor of Venice&lt;/a&gt;</font></small></strong><br>
  205. Here the <strong>dir</strong> parameter is a directory containing all the Shakespeare
  206. plays, and the <strong>play</strong> parameter is a subdirectory containing the various
  207. XML files constituting the text of <em>Othello</em>, which you prevoiusly created using
  208. the <strong>LoadPlay</strong> program.</li>
  209. <li>Select this HTML page from your browser and click on the link to <em>Othello</em>. You
  210. should be able to view the front page of the play, and browse through its individual
  211. scenes.</li>
  212. </ol>
  213. <p>If you have problems getting this to work, and there seem to be no useful diagnostics
  214. visible at the web browser, check in the web server log files. Also, try getting any
  215. sample servlets supplied with the web server to work first: this will ensure that the
  216. environment is OK. The most likely cause of problems is that the code you need (servlets,
  217. SAXON, the XML parser) is not all on the class path used by the web server: this is not
  218. necessarily the same class path as you use from the command line.<code><br>
  219. </code></p>
  220. -->
  221. <hr>
  222. <h2 align="left"><a name="bible">The Bible</a></h2>
  223. <p align="left">The stylesheet <b>bible.xsl</b> takes as input an XML file containing the text of the Old
  224. or New Testament. These files are not included in the SAXON distribution for space
  225. reasons, but can be downloaded from
  226. <a href="http://sunsite.unc.edu/pub/sun-info/standards/xml/eg/rel200.zip">
  227. http://sunsite.unc.edu/pub/sun-info/standards/xml/eg/rel200.zip</a>
  228. or from various mirror sites. They were prepared by Jon Bosak.</p>
  229. <p align="left">The output of the stylesheet is a set of 292 HTML files in a single
  230. directory, which together provide a frames-oriented rendition of the text.
  231. The application also works with the Old Testament text, but not with the other religious
  232. texts included in Jon Bosak's distribution.</p>
  233. <p align="left">To run the stylesheet first create an output
  234. directory (say htmldir), then execute the following from the command line:</p>
  235. <p align="left"><strong><small><font color="#FF0080">java&nbsp; com.icl.saxon.StyleSheet
  236. &nbsp; data\nt.xml&nbsp; styles\bible.xsl&nbsp; dir=htmldir</font></small></strong></p>
  237. <p align="left">The final parameter sets the value of the XSLT parameter &quot;dir&quot; to
  238. the value &quot;htmldir&quot;, which is referenced within the stylesheet to select a
  239. directory for output files.</p>
  240. <!--<h2><a name="applet">Applet Examples</a></h2>
  241. <p>The directory samples/applet contains two demonstrations showing how to use
  242. Saxon from an applet embedded in an HTML page.</p>
  243. <p>The subdirectory /demo contains a demonstration taken straight from the Xalan
  244. sample application, showing a window with three frames, containing the source document,
  245. stylesheet, and result. Click on "index.html" to open this in the browser, then click on the
  246. "Transform" button to run a transformation. Note that the HTML page is written in such
  247. a way that expects the saxon.jar file to be located in the place where it will be
  248. unpacked when you unzip the Saxon distribution .zip file; if you move any files, you
  249. will need to change the URL.</p>
  250. <p>The subdirectory /family-tree contains a demonstration showing some members of
  251. the Kennedy family; open the HTML file to view the data. This demonstration is adapted
  252. from the one in my book <i>XSLT Programmer's Reference</i>. It illustrates how repeated
  253. transformations using the same source document and the same stylesheet can be used to
  254. browse around a data-set, without making repeated visits to the server.</p>
  255. <hr>-->
  256. <h2><a name="jdom">JDOM Example</a></h2>
  257. <p>Saxon includes an adapter that allows the source tree to be a JDOM document.</p>
  258. <p>To use this facility:</p>
  259. <ul>
  260. <li>The JAR file saxon-jdom.jar must be on the classpath</li>
  261. <li>JDOM 1.0 must be installed and on the classpath</li>
  262. </ul>
  263. <p>The sample application JDOMExample.java illustrates how a JDOM tree can be used
  264. with Saxon. It combines two scenarios: running XPath expressions directly against a
  265. JDOM tree, from the Java application; and invoking a transformation with the JDOM document
  266. supplied as the Source object.</p>
  267. <p>The application is designed to take two arguments, the <code>books.xml</code> file
  268. in the samples/data directory, and the <code>total.xsl</code> file in the samples/styles
  269. directory. The application builds a JDOM tree, modifies it <i>in situ</i> to add extra
  270. attributes, and then references these attributes from the stylesheet.</p>
  271. <hr />
  272. <p align="center">Michael H. Kay<br>
  273. <a href="http://www.saxonica.com/">Saxonica Limited</a><br>
  274. 22 June 2005</p>
  275. </body>
  276. </html>