clarification regarding xslt in java

R

Rakesh

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 ?
 
J

Jan Thomä

Hi Rakesh,

I am trying to do this basic rudimentary XSL transformation using
Java.

You could try this:

<xsl:for-each select="Son">
<Grandson>
<!-- Use text() here, as the for-each changes the scope
to the Son element. So when you do select="Son" you would
actually select something like
Son/Son/text()
which does not exist, which is why you don't get the text
-->
<xsl:value-of select="text()"/>
</Grandson>
</xsl:for-each>


Hope that helps.

Best regards,
Jan
 
R

Rakesh

Hi Rakesh,



You could try this:

<xsl:for-each select="Son">
<Grandson>
<!-- Use text() here, as the for-each changes the scope
to the Son element. So when you do select="Son" you would
actually select something like
Son/Son/text()
which does not exist, which is why you don't get the text
-->
<xsl:value-of select="text()"/>
</Grandson>
</xsl:for-each>

Hope that helps.

Best regards,
Jan

Thanks Jan.
Entering that helped me. It worked.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top