Transforming XML files by using XSL style sheets

Configuring this project takes approximately 5 minutes.

Before you begin

Before you can work with the sample, you must first import it into your workspace:
  1. Click the following link: Import sample
  2. In the Import Project dialog box, accept the defaults and click Finish. The following projects are imported into your workspace:
    • BookCatalogWebService
    • BookCatalogWebServiceEAR

About this task

Using the imported sample code, you see how you can use XSL style sheet to transform XML code in the following ways:
  • You can use the XSL style sheet to transform an XML file into a new XML file that contains contents that are defined by the XSL file.
  • Using a Web Service, you can transform XML input content into XML content that is defined by the XSL file that is called by the Web Service.
To use an XSL style sheet to transform an XML file into a new XML file:
  1. In the Enterprise Explorer view, open BookCatalogWebService > WebContent.
  2. Open the Catalog.xml file and inspect its contents. The file contains Book (<tns: Book>) and Magazine (<tns: Magazine>) elements. There is no order to the elements in the file.
  3. Press the Ctrl key and click the following files:
    • Catalog.xml
    • Catalog.xsl
    Both files are selected.
  4. Right-click either selected file and click Run As > XSL Transformation. The Console view opens and shows the names of the transformation input files and the result file.
  5. The result file _Catalog_transform.xml is located in the WebContent folder. Open the result file and inspect the contents. You should see that it contains only Book elements that are ordered first by country and then by publishing date.

What to do next

You can also run a similar transformation using a Web service.

To use a Web Service that calls an XSL transformation to transform input XML content:
  1. Ensure that you have a WebSphere Application Server V8.5.5.5 server defined:
    By default a server is created for you when you install WebSphere Application Server. This server can be seen in the Servers view. However, if you want to create a new server, complete the following steps:
    1. Click File > New > Other > Server > Server > Next.
    2. Select WebSphere V8.5.5 Server as the server type, and click Next.
    3. If you do not have a runtime environment attached to your workspace, you are prompted to enter the installation directory of WebSphere Application Server.
    4. On the server settings page, click Finish.
    5. To see the server you have created in the workspace, click Window > Show view > Other > Server > Servers > OK.
  2. Add the EAR project to the server:
    1. In the Servers view, right-click the WebSphere Application Server V8.5.5 server and click Add and Remove Projects.
    2. In the list of available projects, click BookCatalogWebServiceEAR, click Add > and then click Finish.
  3. Check the HTTP port used by WebSphere Application Server V8.5.5 server:
    1. In the Servers view, right-click the WebSphere Application Server V8.5.5 server and click Properties.
    2. In the left-hand side of the properties window that opens, click WebSphere Application Server.
    3. Check the value of HTTP port. If the value is not 9080, make a note of it; you will use this value in a later step.
  4. In the Servers view, right-click the WebSphere Application Server V8.5.5 server and click Start
  5. In the Enterprise Explorer view, navigate to the WebContent\WEB-INF\wsdl\Catalog.wsdl file.
    1. If the HTTP port for WebSphere Application Server V8.5.5 that you checked in step 3 is not 9080, then you must modify the WSDL file:
      1. Right-click Catalog.wsdl and then click Open With > Text Editor.
      2. Find the following text near the end of the file:
        <soap:address
        				location="http://localhost:9080/BookCatalogWebService/BookCatalogService" />
      3. Change the value of localhost to the HTTP port that you noted in Step 3. For example, if the HTTP port is 9082, then change the text to:
        <soap:address
        				location="http://localhost:9082/BookCatalogWebService/BookCatalogService" />
      4. Save your changes.
    2. In the Enterprise Explorer view, right-click Catalog.wsdl and then click Web Services > Test With Web Services Explorer. The Web Services Explorer opens.
  6. In the Actions window of the Web Services Explorer, under Operations, click generateReport.
  7. In the top-right corner of the Actions window, click Source.
  8. In the input window under <soapenv:Body>, delete the current content.
  9. Copy of the following content into the input window under <soapenv:Body> (this content is the same as the source of the Catalog.xml file, except the first line in Catalog.xml has been deleted):
    <tns:Catalog xmlns:tns="http://www.eclipse.org/webtools/Catalog"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.eclipse.org/webtools/Catalog Catalog.xsd">
    	<tns:Book>
    		<Title>Professional XML Schema</Title>
    		<PublishDate>2001-01-01</PublishDate>
    		<ISBN>1-861005-47-4</ISBN>
    		<Publisher>Wrox Press</Publisher>
    		<Country>USA</Country>
    	</tns:Book>
    	<tns:Magazine>
    		<Title>WebSphere Developer's Journal</Title>
    		<PublishDate>2001-02-01</PublishDate>
    	</tns:Magazine>
    	<tns:Book>
    		<Title>Java and XSLT</Title>
    		<PublishDate>2001-03-01</PublishDate>
    		<ISBN>0-596-00143-6</ISBN>
    		<Publisher>O'Reilly</Publisher>
    		<Country>Canada</Country>
    	</tns:Book>
    	<tns:Book>
    		<Title>XSLT 2.0</Title>
    		<PublishDate>2006-04-05</PublishDate>
    		<ISBN>0-596-125407</ISBN>
    		<Publisher>Hello World</Publisher>
    		<Country>Canada</Country>
    	</tns:Book>
    	<tns:Book>
    		<Title>Rational handbook</Title>
    		<PublishDate>2005-06-07</PublishDate>
    		<ISBN>2-345005-47-4</ISBN>
    		<Publisher>IBM</Publisher>
    		<Country>USA</Country>
    	</tns:Book>
    	<tns:Book>
    		<Title>XPath and XML 2.0</Title>
    		<PublishDate>2006-02-03</PublishDate>
    		<ISBN>2-536-897767</ISBN>
    		<Publisher>IBM</Publisher>
    		<Country>Canada</Country>
    	</tns:Book>
    </tns:Catalog>
  10. Click Go.
  11. Inspect the Status window of the Web Services Explorer. You should see that it contains only Book elements that are ordered first by country and then by publishing date.
  12. When you are finished, you can stop the server.

Feedback