aspose file tools*
The moose likes XML and Related Technologies and the fly likes Retrieve objects from XSL transform Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Retrieve objects from XSL transform" Watch "Retrieve objects from XSL transform" New topic
Author

Retrieve objects from XSL transform

bhargavi kakarala
Ranch Hand

Joined: Jan 03, 2002
Posts: 82
Hi,

I have a xslt file I used following java code to transform it .I want to retrieve each node in XSLT as an object or collection of objects and pass them as parameters to other Java code.
Is it possible to retrieve values without passing them to stream or Is there any other alternative way to do that

XML DOCUMENT

<sample>
<directory >
<name>c:\abc\fgfg</name>
<textfilespec>".txt"</textfilespec>
<filespec>".csv"</filespec>
<processing.filesuffix>.txt</processing.filesuffix>
<finishsuffix>.csv</finishsuffix>
</directory>
</sample>

XSLT file

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2004/07/xpath-functions" xmlns:xdt="http://www.w3.org/2004/07/xpath-datatypes">
<xsl utput method="text/html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="sample/directory">
<xsl:text>DIRECTORY NAME:</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>TEXT FILE: </xsl:text>
<xsl:value-of select="textfilespec"/>
<xsl:text>csv FILE:</xsl:text>
<xsl:value-of select="filespec"/>
<xsl:text>FILE SUFFIX:</xsl:text>
<xsl:value-of select="processing.filesuffix"/>
<xsl:text>FINISH SUFFIX</xsl:text>
<xsl:value-of select="finishsuffix"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>



public static void main(String[] args) throws TransformerException {
if (args.length != 2) {
System.err.println("Usage:"+" [xmlFileName] [xsltFileName]");

System.exit(1);
}

File xmlFile = new File(args[0]);
File xsltFile = new File(args[1]);

Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);

TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);

Result result = new StreamResult("C:/XSLT.txt");
System.out.println(result);
trans.transform(xmlSource, result);

trans.transform(xmlSource, new StreamResult(System.out));

}


Thanks in advance
[ December 15, 2005: Message edited by: bhargavi kakarala ]

Bhargavi
SCJP2,SCWD,SCBCD,IBM Certified SOA Associate,IBM Certified Architectural Design of SOA Solutions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Instead of transforming the XML document to HTML, use a DocumentBuilder to parse it into a DOM Document object. Then write code to extract the relevant nodes from that.
bhargavi kakarala
Ranch Hand

Joined: Jan 03, 2002
Posts: 82
Thanks Paul,

but I dont want to use DOM. Is there any other way to retrive values from this


Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I don't want to use DOM either. It's annoying to write and easy to get wrong. But when it's the best tool for the job at hand then I use it.

But if you have some real-life reason for not using DOM, then you could parse the original XML using a SAX parser and write a ContentHandler to capture the data.

Or if somebody says you have to use the output of that transformation (maybe some manager who doesn't understand things) then you could send its output to a StringWriter and parse the resulting string to extract the data.
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Why not try using xpath javax.xml.xpath package in java 1.5 or xalan.jar for earlier versions of java

I guess since you know xslt you must know xpath so the learning curve will be less

I've just made use of xpath i my project and you wont beleive how much coding I have avoided using xpath

just my 2 cents

xpath uses DOM under its hood I guess

-Rajagopal
bhargavi kakarala
Ranch Hand

Joined: Jan 03, 2002
Posts: 82
Thanks for your quick response.

Paul :I have also written SAX Parser.But I wanted to try to retrieve values from XSLT transformation too sothat, I can have a common class to transform any number of XSLT files.

Rajagopal :I have written XPATH using Java1.5 but the problem with that is I couldn't generalize the elements(If I have more than one set of same type). If there is a way to retrive collections using that without passing specific XPATH expression for every element please let me know.

Thanks
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
Originally posted by bhargavi kakarala:
Rajagopal :I have written XPATH using Java1.5 but the problem with that is I couldn't generalize the elements(If I have more than one set of same type). If there is a way to retrive collections using that without passing specific XPATH expression for every element please let me know.

Thanks[/QB]


I have not worked with the jdk 1.5 version of xpath because I am stuk with jdk 1.3 and xalan but I think the

Object evaluate(String expression,
Object item,
QName returnType)

method can also return a NodeList which I guess is what you are looking for

-Rajagopal
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Retrieve objects from XSL transform