XSL for Business-to-Business (B2B) Communication
The previous section leveraged XSLT for document publishing. However,
XSLT can also be used in for B2B communication—the process of exchanging data
between two different companies. Developers can leverage XML to describe the
data in a vendor-indepen-dent fashion. In the ideal case, both companies will
agree upon a standard vocabulary for describing the data using a DTD or schema.
The vocabulary is composed of the XML element names used in the XML document.
However, in certain cases one of the compa-nies might like to use a different
vocabulary. This is where XSL enters the picture.
The example in this section describes a B2B scenario between a training
company, Hot Shot Training, and a software development company, AcmeSoft. The
computer training company maintains a database for the students that have
attended its courses. The train-ing company has developed an XML application
that produces the list of students for a given class.
The management team at AcmeSoft would like to retrieve this list from
the training com-pany’s XML application. However, once the data is retrieved,
AcmeSoft would like to store the data in a different XML format using its own
XML element names. The appli-cation interaction is illustrated in Figure 9.13.
The XML application at the training company is accessible using the HTTP
protocol. The first step is to request the XML document from the training
company. In step 2, the XML document is retrieved. In step 3, the document is
transformed using the supplied XSLT style sheet. Finally, the desired output
document is produced in step 4.
A sample output of the XML document is shown here:
<?xml version=”1.0”?> <trainingclass>
<title>J2EE Essentials</title>
<start_date>24 Sep 2001</start_date> <end_date>28 Sep
2001</end_date> <location>Philadelphia, PA</location>
<student>
<first_name>Riley</first_name>
<last_name>Scott</last_name>
<email>riley@acmesoft.web</email>
</student>
<student>
<first_name>Torrance</first_name>
<last_name>Lee</last_name>
<email>torrance.lee@acmesoft.web</email>
</student>
</trainingclass>
The development team at AcmeSoft has a different collection of element
names to describe a training class. The desired output of the converted XML
document for AcmeSoft is shown here:
<?xml version=”1.0”?> <employeelist>
<course_title>J2EE
Essentials</course_title> <course_date start=”24 Sep 2001” end=”28 Sep
2001” /> <location>Philadelphia, PA</location>
<employee>
<name>
<first>Riley</first>
<last>Scott</last>
</name>
<email>riley.scott@acmesoft.web</email>
</employee>
<employee>
<name>
<first>Torrance</first>
<last>Lee</last>
</name>
<email>torrance.lee@acmesoft.web</email>
</employee>
</employeelist>
Notice in both instances that the data is the same; it’s simply in a
different format. The format is different because of the element names used by
AcmeSoft. In the context of AcmeSoft, there are no students on the payroll;
instead, AcmeSoft has employees. Also, the AcmeSoft team has a different
approach for storing the class date. Finally, notice that AcmeSoft uses a
different structure for the employee name.
Therefore, a mechanism is needed to convert an XML document to another
XML format. XSLT offers a solution to this problem. An XSL style sheet can be
developed to convert the <trainingclass> document to the <employeelist> document. This approach will not require any changes by the training
company. The training company can continue to publish XML documents for its
training classes. The development team at AcmeSoft can develop an XSL style
sheet that contains the transformation rules. Once the style sheet is
developed, the XML document and style sheet can be passed to the XSLT
processor, which will generate the desired XML document for <employeelist>.
Creating the XSL Style Sheet
The XSL style sheet will contain the template for the <employeelist> document, and the XSL elements
will be leveraged to retrieve the data from the <trainingclass> docu-ment. The transformation is fairly straightforward, except for one
area. The training com-pany describes the date for the class using the elements
<start_date> and <end_date>, as shown here:
<start_date>24 Sep 2001</start_date>
<end_date>28 Sep 2001</end_date>
AcmeSoft stores the date as a single element with two attributes for the
start and end:
<course_date
start=”24 Sep 2001”
end=”28 Sep 2001”
/>
In this case, <xsl:attribute> can be used to create attributes for <course_date>:
<course_date>
<xsl:attribute name=”start”><xsl:value-of
select=”start_date”/> ➥ </xsl:attribute>
<xsl:attribute name=”end”><xsl:value-of
select=”end_date”/></xsl:attribute> </course_date>
The <xsl:attribute> element creates attributes for the parent element. In this example, the
parent element is course_date. This transformation will result in the following code:
<course_date
start=”24 Sep 2001”
end=”28 Sep 2001”
/>
The complete code for the style sheet is shown in Listing 9.13.
LISTING 9.13 <install_dir>\ch9_xsl\public_html\b2b\train2employee.xsl
<?xml version=”1.0”?>
<xsl:stylesheet
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”>
<xsl:template
match=”/trainingclass”> <employeelist>
<course_title><xsl:value-of
select=”title” /></course_title>
<!-- create attributes for the start and end course dates --> <course_date>
<xsl:attribute
name=”start”>
<xsl:value-of
select=”start_date”/> </xsl:attribute>
<xsl:attribute
name=”end”>
<xsl:value-of
select=”end_date”/> </xsl:attribute>
</course_date>
<location><xsl:value-of
select=”location”
/></location>
<!-- Perform a loop for each student in the training class --> <xsl:for-each select=”student”
<employee>
<name>
<first><xsl:value-of
select=”first_name”/></first> <last><xsl:value-of
select=”last_name”/></last>
</name>
<email><xsl:value-of
select=”email”/></email> </employee>
</xsl:for-each>
</employeelist>
</xsl:template>
</xsl:stylesheet>
Using the XSLT Processor
So far in this chapter, we’ve used client-side and server-side
techniques for XSLT pro-cessing. The client-side technique utilized a Web
browser that has support for XSLT. The server-side technique leveraged server
scripts developed in JSP and ASP. The JSP exam-ples used a JSP custom tag for
the XSLT processing, whereas the ASP examples lever-aged the Microsoft XML
server object.
For most B2B applications, the source XML document is retrieved by
another applica-tion. This may be a standalone application or a component of a
larger B2B application. In the case of a standalone application, the necessary
code to perform the XSLT process-ing needs to be developed. For example, a
Visual Basic or Visual C++ application can use the XSLT processor available
with the Microsoft XML API. In a similar fashion, a Java application can use
the XSLT processor available with the Apache Xalan API. The Apache Xalan API is
available at http://xml.apache.org.
Here’s the code for a standalone Java application that uses the Apache
Xalan API (note that the application accepts three command-line arguments—one
each for the input XML document, the XSL style sheet, and the name of the
output file):
java
XslTester <input XML>
<input XSL> <output
file>
The input XML document and the input XSL style sheet can be referenced
using file-names or URLs. Listing 9.14 contains the complete code for XslTester.java.
LISTING 9.14 <install_dir>\ch9_xsl\b2b\XslTester.java
import org.apache.xalan.xslt.*;
/**
* Usage: java XslTester <input XML> <input
XSL> <output file> */
public class XslTester{
public
static void main(String[]
args) {
try {
// Verify the correct arguments are passed in if
(args.length != 3) {
System.out.println(“Usage: java XslTester <input XML> <input
XSL> ➥ <output file>”);
System.exit(1);
}
System.out.println(“Processing: “
+ args[0] + “ and
“ + args[1]);
Step 1: Get a reference to the XSLT Processor XSLTProcessor myEngine =
XSLTProcessorFactory.getProcessor();
Step 2:
Get the XML
input document
XSLTInputSource
xmlSource = new
XSLTInputSource(args[0]);
// Step
3: Get the XSL
style sheet
XSLTInputSource
xslStylesheet = new
XSLTInputSource(args[1]);
// Step
4: Setup the output
target
XSLTResultTarget
xmlOutput = new
XSLTResultTarget(args[2]);
// Step 5: Now process it! myEngine.process(xmlSource, xslStylesheet,
xmlOutput);
System.out.println(“Created => “ + args[2]);
System.out.println(“Done!”);
}
catch (Exception exc) {
exc.printStackTrace();
}
}
}
The first task of the main method is verifying the correct number of
parameters. Next, the application retrieves a reference to the XSLT processing
engine using the factory method XSLTProcessorFactory.getProcessor(). The application then retrieves the input XML document and XSL style
sheet based on the first two command-line argu-ments. The result target for the
XSLT translation is configured to use the filename sup-plied as the third
command-line argument. Finally, the XSLT processing engine is invoked using the
objects xmlSource, xslStylesheet, and xmlOutput.
Running the Example
This example requires the Java Development Kit version 1.3 or higher.
Follow these steps to test it:
Open an MS-DOS window.
Move to the directory <install_dir>\ch9_xsl\b2b\.
Set up the Java classpath by
typing setpaths.
Execute the application by typing
the following:
java
XslTester trainingclass.xml train2employee.xsl testoutput.xml
View the testoutput.xml file in a text editor. Verify
that your document resembles this:
<?xml version=”1.0” encoding=”UTF-8”?>
<employeelist>
<course_title>J2EE
Essentials</course_title> <course_date start=”24 Sep 2001” end=”28 Sep
2001”/> <location>Philadelphia, PA</location>
<employee>
<name>
<first>Riley</first>
<last>Scott</last>
</name>
<email>riley@acmesoft.web</email>
</employee>
<employee>
<name>
<first>Torrance</first>
<last>Lee</last>
</name>
<email>torrance.lee@acmesoft.web</email>
</employee>
</employeelist>
The trainingclass.xml document is also available on the Web server at http:// localhost:8080/bookch9/hotshot/trainingclass.xml. The XslTester application also
supports a URL for the XML document and XSL style sheet. You can access the trainingclass.xml document via the Web server by
simply supplying the following URL:
java XslTester
http://localhost:8080/bookch9/hotshot/trainingclass.xml
train2employee.xsl testoutput.xml
This example demonstrates the technique used to retrieve an XML document
and per-form the XSLT conversion. Once the XML data is converted to the desired
XML output, the application can process it accordingly. For example, the
application can use the SAX and DOM APIs to parse the XML document and store
the results in a database. By lever-aging the SAX and DOM APIs, the application
is very flexible in how it processes/stores the converted XML document.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.