Home > XML IDE - XML Editor > DataDirect XML Converters > XML Converters Unleashed
Stylus Studio® Converters UnleashedBy: Ivan Pedruzzi, Product Architect, The Stylus Studio® Team. If you have processed XML using Java, you should be familiar with JAXP (Java API for XML Processing). Here is a simple program that processes an XML document using XSLT:
import javax.xml.transform.*;
Nice and easy if you are dealing with XML documents, but what happens if your data is not all XML? Assume for instance that we need to exchange data with a business partner who requires files using a specific XML dialect, but that our data is tabular and is stored as CSV files. Accessing Non-XML Files as XML in JavaHere's a sample from our CSV file of used motorcycles inventory:
make,model,year,mileage
Our partner requires an XML document that looks like what follows, and has provided an XML Schema so that we can ensure that the XML we provide complies with his requirements:
<?xml version="1.0"?>
The first step is simply to create an XML representation of our CSV data.
<?xml version="1.0"?>
Though some of the specifics vary (additional parent elements, different element names, and so on), because the data is now in XML format, XSLT can be used to create the final XML result. So, that's the theory (CSV to> XML to> XSLT to> XML), but how do you make it all work? Even assuming you have an XML document that represents data stored in CSV format, the prospect of writing XSLT can be daunting. It might sound intimidating, but using Stylus Studio® we can create solid, complex, bug-free XSLT in just a few steps.
You know what they say ... a picture is worth 1000 words. Stylus Studio® did most of the work for us — converting CSV to XML, writing XSLT code based on simple mappings — but what happens if we need to perform the same steps periodically, perhaps as part of larger application so that we can automate the process of exchanging inventory information with our partner? The answer lies in common URLs, and Stylus Studio®'s uncommon Java runtime library. Automating Non-XML Data Access in Java ApplicationsURL stands for Uniform Resource Locator. URLs allow us to use a symbolic name to reach a document that either exists somewhere or gets created dynamically when the address represented in the URL is hit. If you look closely inside the Stylus Studio® Project window, you quickly realize how converters work — an converter is represented as a URL, like FTP, HTTP, FILE, and so on. Let's take a closer look at an converter's components, using the following converter as an example:
Using The Stylus Studio® Runtime LibraryLet's go back to the original JAXP example. Wouldn't it be cool if we could use the converter URL to specify the argument for the Java StreamSource method — that is, to effectively use a CSV file as a source for an XSLT transformation, and do it all programmatically? Thanks to the Stylus Studio® runtime library, we can!
import javax.xml.transform.*;
JAXP defines a clever mechanism that allows XSLT processors to delegate URL resolution to an
external component called the
The
<xsl:for-each select="document('
converter:CSV:newline=crlf:sep=,:first=yes:
To register a
TransformerFactory tf = TransformerFactory.newInstance();
We hope you enjoyed this overview of Stylus Studio®'s runtime library. Happy Java programming!
Ivan Pedruzzi,
|
PURCHASE STYLUS STUDIO ONLINE TODAY!!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Attend a Live Webinar This Week!Learn about Stylus Studio's unique features and benefits in just under an hour. Register for the Stylus Studio QuickStart Training WebCast! Ask Someone You KnowDoes your company use Stylus Studio? Do your competitors? Engineers from over 100,000 leading companies use Stylus Studio, and now you can ask someone from your own organization about their experiences using Stylus Studio. |