The primary goals of this example are to introduce you to the following: Other Relevant Info:
To Build: "mvn clean install" To Run: "mvn exec:java" Transforming xml with namespacesIn this example we build a very simple fragment transformer in XSLT. The point of this tutorial is purely to demonstrate how transforming xml with namespaces with Smooks. The use case for this example of transforming between two xml formats that use different namespace. This is a typical use case when one has a canonical data model. So here's the source xml that is to be transformed: <Order xmlns="http://milyn.codehaus.org/Smooks" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> And this is the expected result of our transformation: <CanonicalOrderFormat *xmlns="http://canonical.codehaus.org/Order"*> Notice that CanonicalOrderFormat has a different namepace and only contains a subset of the date from the Order xml instance. The Smooks ConfigurationIn order to apply this transformer to a message fragment, a Smooks Configuration needs to be created. This configuration will target the transformer at a particular message fragment. For more information on configuring XSLT resources, see XslContentHandlerFactory. Here's the configuration ("smooks-config.xml"): <?xml version="1.0"?> The resource-config tells Smooks to apply the transformation that is declared "inline" in the resource element on all <Order>. Notice that we are declaring namespace "http://milyn.codehaus.org/Smooks" with prefix "smk". This namespace is then used in the select expressions to extract the values from the Order xml instance. Also not the usage of exclude-result-prefixes which specifies that we wish to exclude the specified namespaces from the resulting xml. If this is not declared then then namespace for the prefix will added to the resulting xml, which is not something you want when you are trying to decouple. Executing The TransformationAgain, it's exactly the same as with the java-basic tutorial: // Instantiate Smooks with the config... Of course, you'd typically cache the Smooks instance. See the example/Main.java in the example source.
|