Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Retrieve objects from XSL transform

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 26951
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul,

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


Thanks
 
Paul Clapham
Sheriff
Posts: 26951
83
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic