Examples


Table of Contents

Web App Examples
Console App Examples
Example of a SAX Pipeline
Resources script
Examples of Converting XML To and From Flat Files
Converting a comma separated value (CSV) file to XML
Converting a Java properties file to XML
Serializing XML to a positional flat file
Examples of Converting SQL Query to XML Document
Resources script
Output XML file countries.xml
Example of Serializing XML to a Mail Message
Resources script
Mail Message and File Output
Examples of Streaming XML
Merging elements from one XML file into another

Web App Examples


The bookstore web app example may be found in the distribution under the webapps directory. It illustrates how to build a simple shoping cart.

Before building the sample web application, be sure to build the presentingxml project. From the command prompt in the top level presentingxml directory, enter build. See the "Getting Started" section of the on-line Guide for instructions on building the sample web application.

Console App Examples


Console app examples may be found in the distribution under the consoleapp directory.

Before running the sample console applications, be sure to build the presentingxml project. From the command prompt in the top level presentingxml directory, enter build.

Example of a SAX Pipeline

This example shows an implementation of the SAX Pipeline example in Michael Kay's XSLT Programmer's Reference book, Appendix F, Example 5. The conversion may be performed on the command line by entering


java -jar dir/presentingxml.jar -o original.html -r resources.xml pipeline

Resources script


<px:resources xmlns:px="http://www.presentingxml.com/PresentingXML">
  
  <px:page name="pipeline">
    <px:show>
      <px:transform>
        <px:systemContent href="documents/mixed-up.xml"/>
        <px:saxFilter className="PreFilter"/>
        <px:style href="styles/filter.xsl"/>     
        <px:saxFilter className="PostFilter"/>     
      </px:transform>
    </px:show>
  </px:page>

</px:resources>

PreFilter and PostFilter are the names of Java classes that implement the org.xml.sax.XMLFilter interface.

Examples of Converting XML To and From Flat Files

Converting a comma separated value (CSV) file to XML

The example below illustrates how to prepare a resources script that will convert a flat file to XML. The resulting stream of XML elements is validated with Sun's multi schema validator. The conversion may be performed on the command line by entering


java -jar dir/presentingxml.jar -o countries.xml -r resources.xml countries

Resources script


<px:resources xmlns:px="http://www.presentingxml.com/PresentingXML"
              xmlns:myapp="http://www.presentingxml.com/myapp"                   
              ns="http://www.presentingxml.com/myapp">
  
  <px:page name="countries">                         
    <px:show>
      <px:transform>
        <px:msvFilter schema="data/countries.xsd"/>
        <px:content ref="myapp:countries"/>
      </px:transform>
    </px:show>
  </px:page>
  
  <px:flatFileContent name="countries" href="data/countries.csv">
    <px:fileLayout>
      <px:commentSymbol value="#"/>
      <px:record>
        <px:fieldDelimiter value=","/>
        <px:field name="code"/>
        <px:field name="name" quote="yes"/>
      </px:record>
    </px:fileLayout>      
    <px:recordSetMapping>
      <countries>
        <px:recordMapping>
          <country>
            <px:fieldAttributeMap field="code" attribute="countryCode"/>
            <px:fieldElementMap field="name" element="countryName"/>  
          </country>  
        </px:recordMapping>
      </countries>
    </px:recordSetMapping>  
  </px:flatFileContent>
  
</px:resources>

Input flat file countries.csv


ATF,"FRENCH SOUTHERN TERRITORIES, D.R. OF"
VUT,VANUATU
WLF,WALLIS & FUTUNA ISLANDS

XML Schema file countries.xsd


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <xsd:element name="countries" type="CountriesType"/>

 <xsd:complexType name="CountriesType">
  <xsd:sequence>
      <xsd:element name="country" type="CountryType" minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="CountryType">
  <xsd:sequence>
   <xsd:element name="countryName" type="xsd:string"/>
  </xsd:sequence>
  <xsd:attribute name="countryCode" type="CountryCode"/>
 </xsd:complexType>

  <xsd:simpleType name='CountryCode'>
    <xsd:restriction base='xsd:string'>
      <xsd:length value='3' fixed='true'/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

Output XML file countries.xml


<?xml version="1.0" encoding="utf-8"?>
<countries>
  <country countryCode="ATF">
    <countryName>FRENCH SOUTHERN TERRITORIES, D.R. OF</countryName>
  </country>
  <country countryCode="VUT">
    <countryName>VANUATU</countryName>
  </country>
  <country countryCode="WLF">
    <countryName>WALLIS &amp; FUTUNA ISLANDS</countryName>
  </country>
</countries>

Converting a Java properties file to XML

The example below illustrates how to prepare a resources script that will convert a Java properties file to XML. The conversion may be performed on the command line by entering


java -jar dir/presentingxml.jar -o messages.xml -r resources.xml messages

Resources script


<px:resources xmlns:px="http://www.presentingxml.com/PresentingXML"
              xmlns:myapp="http://www.presentingxml.com/myapp"                   
              ns="http://www.presentingxml.com/myapp">
  
  <px:page name="messages">                         
    <px:show>
      <px:transform> 
        <px:content ref="myapp:messages"/>
      </px:transform> 
    </px:show>
  </px:page>
  
  <px:flatFileContent name="messages" href="data/messages.properties">
    <px:fileLayout>
      <px:commentSymbol value="#"/>
      <px:record>
        <px:fieldDelimiter value="="/>
        <px:field name="name"/>
        <px:field name="value" quote="yes"/>
      </px:record>
    </px:fileLayout>
    <px:recordSetMapping>
      <messages>
        <px:recordMapping>
          <px:fieldElementMap field="value" element="{name}"/>
        </px:recordMapping>
      </messages>
    </px:recordSetMapping>
  </px:flatFileContent>
  
</px:resources>

Input property file messages.properties


parser.ope.2=')' or '-[' or '+[' or '&[' is expected.
parser.descape.1=Invalid Unicode hex notation.

Output XML file messages.xml


<?xml version="1.0" encoding="utf-8"?>
<messages>
  <parser.ope.2>')' or '-[' or '+[' or '&amp;[' is expected.</parser.ope.2>
  <parser.descape.1>Invalid Unicode hex notation.</parser.descape.1>
</messages>

Serializing XML to a positional flat file

The example below illustrates how to prepare a resources script that will convert an XML document into a positional flat file. The conversion may be performed on the command line by entering


java -jar dir/presentingxml.jar -o books.txt -r resources.xml books-txt

Resources script


<px:resources xmlns:px="http://www.presentingxml.com/PresentingXML"
              xmlns:myapp="http://www.presentingxml.com/myapp"                   
              ns="http://www.presentingxml.com/myapp">
  
  <px:page name="books-txt"> 
    <px:show>
      <px:flatFileSerializer>
        <px:fileLayout ref="myapp:booksFileLayout"/>
        <px:recordSetMapping ref="myapp:booksFileMapping"/>
      </px:flatFileSerializer>
      <px:transform>
        <px:systemContent href="data/books.xml"/>
      </px:transform>
    </px:show>
  </px:page>
  
  <px:fileLayout name="booksFileLayout">
    <px:record>
      <px:field name="category" width="1"/>
      <px:field name="author" width="30"/>
      <px:field name="title" width="30"/>
      <px:field name="price" width="10" justify="right"/>
    </px:record>
  </px:fileLayout>      
  
  <px:recordSetMapping name="booksFileMapping">
    <myapp:books>
      <px:recordMapping>
        <myapp:book>
          <px:fieldElementMap field="title" element="myapp:title"/>  
          <px:fieldAttributeMap field="category" attribute="categoryCode"/>
          <px:fieldElementMap field="author" element="myapp:author"/>
          <px:fieldElementMap field="price" element="myapp:price"/>
        </myapp:book>  
      </px:recordMapping>
    </myapp:books>
  </px:recordSetMapping>  
  
</px:resources>

Input XML file books.xml


<?xml version="1.0" encoding="utf-8"?>
<myapp:books xmlns:myapp="http://www.presentingxml.com/myapp">
  <myapp:book categoryCode="F">
    <myapp:title>Factotum</myapp:title>
    <myapp:author>Charles Bukowski</myapp:author>
    <myapp:price>22.95</myapp:price>
    </myapp:book>
  <myapp:book categoryCode="C">
    <myapp:title>Building Parsers with Java</myapp:title>
    <myapp:author>Steven John Metsker</myapp:author>
    <myapp:price>39.95</myapp:price>
  </myapp:book>
</myapp:books>

Output positional flat file books.txt


FCharles Bukowski              Factotum                           22.95
CSteven John Metsker           Building Parsers with Java         39.95

Examples of Converting SQL Query to XML Document

The example below illustrates how to prepare a resources script that will convert the results of SQL query into an XML document.

The java run time needs to be able to find the database driver. The sample in the consoleapp/sql directory is set up for the Oracle driver, oracle.jdbc.driver.OracleDriver. To configure Presenting XML to recognize this driver, copy the Oracle classes12.jar file (not distributed) to the presentingxml/lib/local directory. Of course, you can specify a different driver, making the appropriate changes in the sample resources script and copying the right jar file to presentingxml/lib/local.

Once you have everything configured, rebuild the Presenting XML jar file in the presentingxml directory.

The SQL to XML conversion may then be performed on the command line by entering


java -jar dir/presentingxml.jar -o employees.txt -r resources.xml employees job=ANALYST

Resources script

<px:resources xmlns:px="http://www.presentingxml.com/PresentingXML"
              xmlns:myapp="http://www.presentingxml.com/myapp"                   
              ns="http://www.presentingxml.com/myapp">
  
  <px:page name="employees">                         
    <px:parameter ns="" name="job"/>                       
    
    <px:show>
      <px:content ref="myapp:employees"/>
    </px:show>
  </px:page>
  
  <px:jdbcConnectionPool name="jdbc-pool"
                         driver="oracle.jdbc.driver.OracleDriver"
                         databaseURL="jdbc:oracle:thin:@127.0.0.1:1521:dev"
                         user="scott"
                         password="dev"
                         minConnections="2"
                         testStatement="SELECT * FROM DUAL"/>
                         
  <px:sqlContent name="employees"> 
    <px:jdbcConnectionPool ref="myapp:jdbc-pool"/>
    <px:sqlQuery>       
      SELECT EMPNO, ENAME AS NAME,JOB FROM EMP WHERE JOB = '{$job}' 
    </px:sqlQuery>       
    <px:recordSetMapping>
      <employees>
        <px:recordMapping>
          <employee>
            <px:fieldAttributeMap field="EMPNO" attribute="employee-no"/>  
            <px:fieldElementMap field="NAME" element="name"/>  
            <px:fieldElementMap field="JOB" element="job"/>
          </employee>  
        </px:recordMapping>
      </employees>
    </px:recordSetMapping>  
  </px:sqlContent>

</px:resources>

Output XML file countries.xml


<?xml version="1.0" encoding="utf-8"?>
<employees>
  <employee employee-no="7788">
    <name>SCOTT</name>
    <job>ANALYST</job>
  </employee>
  <employee employee-no="7902">
    <name>FORD</name>
    <job>ANALYST</job>
  </employee>
</employees>

Example of Serializing XML to a Mail Message

The example below illustrates how to prepare a resources script that will transform an XML document to XSL-FO, format it to PDF, and send the result as an attachment in a mail message.

This example requires the mail module.

Once you have everything configured, rebuild the Presenting XML jar file in the presentingxml directory.

The XML to mail message conversion may then be performed on the command line by entering


java -jar dir/presentingxml.jar -o output/pulp.pdf -r resources.xml 

Resources script

<px:resources xmlns:px="http://www.presentingxml.com/PresentingXML"
                      xmlns:myapp="http://www.presentingxml.com/myapp"                   
                      ns="http://www.presentingxml.com/myapp">

  <!-- Set up this mail account, substituting your sntp host, display name, and email address -->
  <!-- You will also need to enter the 'to' and 'cc' recipient addresses in the  px:sendMail element -->
  <px:mailAccount name="myMailAccount" 
                           smtpHost="changeme"> 
    <px:sender displayName="changeme" 
                     emailAddress="changeme"/>
  </px:mailAccount>
  
  <px:page name="pulp">                         
      <!--  Our second action is to send an email with the pulp novel as an attachment   -->
      <px:sendMail subject="test" 
                   to="changeme"
                   cc="changeme">
        <px:mailAccount ref="myapp:myMailAccount"/>
        <px:message>

          <!-- Put some HTML in the message body --> 
          <px:part>
            <px:transform>           
              <px:style ref="myapp:welcome/html"/>
            </px:transform>        
          </px:part>
          
          <px:attachment filename="pulp.pdf">
            <px:fopSerializer/>
            <px:transform>
               <px:style ref="myapp:novel/fo"/>
               <px:content ref="myapp:pulp"/>
            </px:transform>
          </px:attachment>      
        </px:message>
        <px:doFault>
            <!-- If send mail fails (e.g. no smtp host), just proceed with the next action -->
            <!-- (the error will still be logged) -->
        </px:doFault>
      </px:sendMail> 
                                        
      <!--  Our second action is to write the pulp novel to a file   -->
      <px:show>
        <px:fopSerializer/>
        <px:transform>
           <px:style ref="myapp:novel/fo"/>
           <px:content ref="myapp:pulp"/>
        </px:transform>
      </px:show>
    
  </px:page>

  <px:systemContent name="pulp" href="data/pulp.xml"/>        

  <px:style name="novel/fo" href="data/novel.xsl"/>

  <px:style name="welcome/html" href="data/welcome.xsl"/>
  
</px:resources>

Mail Message and File Output

This example produces two outputs:

  • A mail message with an HTML body part displaying "Welcome to Presenting Books" and a PDF attachment.
  • A PDF file pulp.pdf written to the output directory.

Examples of Streaming XML

Merging elements from one XML file into another

The example below illustrates how to prepare a resources script that will merge elements from one XML file into another. This is accomplished through a stream filter and so is more efficient than performing an XSLT transform, especially for very large documents. The conversion may be performed on the command line by entering


java -jar dir/presentingxml.jar -o book-category.xml -r filter.xml books

Resources script


<px:resources xmlns:px="http://www.presentingxml.com/PresentingXML"
              xmlns:myapp="http://www.presentingxml.com/myapp"                   
              ns="http://www.presentingxml.com/myapp">
  
  <px:page name="books">                         
    <px:show>
      <px:transform>
        <px:streamFilter>
          <px:input match="book"/>
          <px:variable ns="" name="category" value="{@category}"/>
          <px:appendChild>
            <px:transform>
              <px:streamFilter>
                <px:output match="category[@code=$category]"/>
              </px:streamFilter>  
              <px:content ref="myapp:categories"/>
            </px:transform>
          </px:appendChild>  
        </px:streamFilter>
        <px:systemContent href="documents/books.xml"/>
      </px:transform>
    </px:show>
  </px:page>

  <px:systemContent name="categories" href="documents/categories.xml">
    <px:cachingOptions caching="yes" revalidate="none"/>      
  </px:systemContent>

</px:resources>

First input XML file books.xml


<books>
  <book category="S">
      <title>Number, the Language of Science</title>
      <author>Danzig</author>
      <price>5.95</price>
      <quantity>3</quantity>
  </book>
  <book category="F">
    <title>Factotum</title>
    <publisher>Black Sparrow Press</publisher>
    <author>Bukowski, Charles</author>
    <price>22.95</price>
    <quantity>5</quantity>
  </book>
</books>  

Second input XML file categories.xml


<categories>
  <category code="S" desc="Science"/>
  <category code="C" desc="Computing"/>
  <category code="X" desc="Crime"/>
  <category code="F" desc="Fiction"/>
</categories>

Output XML file book-category.xml


<books>
  <book category="S">
    <title>Number, the Language of Science</title>
    <author>Danzig</author>
    <price>5.95</price>
    <quantity>3</quantity>
    <category code="S" desc="Science"/>
  </book>
  <book category="F">
    <title>Factotum</title>
    <publisher>Black Sparrow Press</publisher>
    <author>Bukowski, Charles</author>
    <price>22.95</price>
    <quantity>5</quantity>
    <category code="F" desc="Fiction"/>
  </book>
</books>