Problem with XSLT and Java

C

crazydiode

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

Joe Kesselman

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.
 
C

crazydiode

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

Joe Kesselman

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

crazydiode

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

Joe Kesselman

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
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top