Hi -
I am trying to do this basic rudimentary XSL transformation using Java.
input.xml:
<?xml version="1.0" encoding="UTF-8"?>
<GrandParent>
<Parent>
<Son>ABC</Son>
<Son>EFG</Son>
</Parent>
</GrandParent>
output_expected.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Me>
<Child>
<Grandson>ABC</Grandson>
<Grandson>EFG</Grandson>
</Child>
</Me>
output_actual.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Me>
<Child>
<Grandson />
<Grandson />
</Child>
</Me>
I have the above mentioned input.xml and want to get
output_expected.xml .
Instead I am getting output_actual.xml (with the children text nodes of GrandSon missing).
The corresponding xsl I had written is:
xform.xsl:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:for-each select="GrandParent"> <Me> <xsl:for-each select="Parent"> <Child> <xsl:for-each select="Son"> <Grandson> <xsl:value-of select="Son" /> </Grandson> </xsl:for-each> </Child> </xsl:for-each> </Me> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The corresponding Java source file to do the transformation in Java is:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource;
/**
* Use the TraX interface to perform a transformation in the simplest manner possible (3 statements).
*/
public class SimpleTransform {
public static void main(String args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
String inputXml = "input.xml";
String inputXsl = "xform.xsl";
String outputXml = "output.xml";
// Use the static TransformerFactory.newInstance method to instantiate // a TransformerFactory. The javax.xml.transform.TransformerFactory // system property setting determines the actual class to instantiate -- // org.apache.xalan.transformer.TransformerImpl. TransformerFactory tFactory = TransformerFactory.newInstance;
// Use the TransformerFactory to instantiate a Transformer that will work with // the stylesheet you specify. This method call also processes the stylesheet // into a compiled Templates object. Transformer transformer = tFactory.newTransformer(new StreamSource(inputXsl));
// Use the Transformer to apply the associated Templates object to an XML document // (foo.xml) and write the output to a file (foo.out). transformer.transform(new StreamSource(inputXml), new StreamResult(new FileOutputStream(outputXml)));
System.out.println("************* The result is in " + outputXml + "*************"); } }
I am using Java 5.
Any idea - where I am getting it wrong - Java code / XSLT ?
I am trying to do this basic rudimentary XSL transformation using Java.
input.xml:
<?xml version="1.0" encoding="UTF-8"?>
<GrandParent>
<Parent>
<Son>ABC</Son>
<Son>EFG</Son>
</Parent>
</GrandParent>
output_expected.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Me>
<Child>
<Grandson>ABC</Grandson>
<Grandson>EFG</Grandson>
</Child>
</Me>
output_actual.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Me>
<Child>
<Grandson />
<Grandson />
</Child>
</Me>
I have the above mentioned input.xml and want to get
output_expected.xml .
Instead I am getting output_actual.xml (with the children text nodes of GrandSon missing).
The corresponding xsl I had written is:
xform.xsl:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:for-each select="GrandParent"> <Me> <xsl:for-each select="Parent"> <Child> <xsl:for-each select="Son"> <Grandson> <xsl:value-of select="Son" /> </Grandson> </xsl:for-each> </Child> </xsl:for-each> </Me> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The corresponding Java source file to do the transformation in Java is:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource;
/**
* Use the TraX interface to perform a transformation in the simplest manner possible (3 statements).
*/
public class SimpleTransform {
public static void main(String args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
String inputXml = "input.xml";
String inputXsl = "xform.xsl";
String outputXml = "output.xml";
// Use the static TransformerFactory.newInstance method to instantiate // a TransformerFactory. The javax.xml.transform.TransformerFactory // system property setting determines the actual class to instantiate -- // org.apache.xalan.transformer.TransformerImpl. TransformerFactory tFactory = TransformerFactory.newInstance;
// Use the TransformerFactory to instantiate a Transformer that will work with // the stylesheet you specify. This method call also processes the stylesheet // into a compiled Templates object. Transformer transformer = tFactory.newTransformer(new StreamSource(inputXsl));
// Use the Transformer to apply the associated Templates object to an XML document // (foo.xml) and write the output to a file (foo.out). transformer.transform(new StreamSource(inputXml), new StreamResult(new FileOutputStream(outputXml)));
System.out.println("************* The result is in " + outputXml + "*************"); } }
I am using Java 5.
Any idea - where I am getting it wrong - Java code / XSLT ?