Problem with XSLT and Java

crazydiode

Usenet Archive
Joined
May 12, 2006
Messages
3
Reaction score
0
HI All,
I am new to XSLT. I am trying to use xslt with xml in my java code. I am basically trying to replace one node of the original xml with my own node. I defined the xslt as follows:

<xsl:stylesheet xmlns:xsl=" http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="directions"> myNode = <xsl:value-of select="directions"/> </xsl:template> <xsl:template match="@*|node"> <xsl:copy> <xsl:apply-templates select="@*|node"/> </xsl:copy> </xsl:template> <xsl:template/>` </xsl:stylesheet>

where "directions" is the node that i want to replace with "mynode". But when i run it, this doesnt happpen... I dont know what wrong i am doing here.

Here's the original XML that i want to transform
<report latitude="41.2° N" longitude="71.6° W">
<locality>Block Island</locality>
<temperature units="°C">16</temperature>
<humidity>88%</humidity>
<dewpoint units="°C">14</dewpoint>
<wind>
<direction>
<directions>NE
</directions>
</direction>
<speed units="km/h">16.1</speed>
<gust units="km/h">31</gust>
</wind>
<pressure units="hPa">1014</pressure>
<condition>overcast</condition>
<visibility>13 km</visibility>
</report>

My JavaCode:
Source xsltSource = new
StreamSource("T:/Technology/Seena/DartAdTesting/DartAdTags.xsl"); // Source of XSL File Source source = new DOMSource(doc); // Document to be transformed Result result = new StreamResult(System.out); // Where to put result document (console here)

//Start off with a factory object
TransformerFactory tf = TransformerFactory.newInstance;

//Use the factory to read the XSLT file into a Templates object Templates transformation = tf.newTemplates(xsltSource);

//Create a Transformer object from the Templates object Transformer transformer = transformation.newTransformer;

//Finally, perform the transformation
transformer.transform(source, result); // This will output the result.

Any help is very much appreciated.. thanks a lott..
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
You haven't shown us how you built that DOM... You are using the DOM Level 2 calls (createElementNS and so on), *NOT* the Level 1 calls (createElement without namespace awareness), right? Depending on your XSLT processor, a namespace-unaware DOM may not be supported even if you aren't using namespaces.
 

crazydiode

Usenet Archive
Joined
May 12, 2006
Messages
3
Reaction score
0
I am not sure what you meant by "how you built that DOM".. i am using a domsource and then using transfomer to get the final xml....
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
crazydiode said:
I am not sure what you meant by "how you built that DOM".. i am using a domsource and then using transfomer to get the final xml....

What is the argument to the DOMSource's constructor? Presumably a DOM, since if you were reading from a file you would have used a StreamSource.

So: Where did that DOM come from?
 

crazydiode

Usenet Archive
Joined
May 12, 2006
Messages
3
Reaction score
0
Yeah.. i am creating the DOMSOurce as

DOMSource source = new DOMSource(node); // Document to be transformed StreamResult result = new StreamResult(fos);

and this is in saveXMLDocument(Node node) method, so i am passing the node to the DOMSource constructor...

am i making any mistkae here ??
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
crazydiode said:
Yeah.. i am creating the DOMSOurce as
DOMSource source = new DOMSource(node);

I say again: First thing I would check is that your node is indeed the document structure you think it is, and (because some processors are picky about this) was constructed using DOM Level 2 namespace-aware nodes.
 

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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top