Problem with XSLT and Java

Discussion in 'XML' started by crazydiode, May 12, 2006.

  1. crazydiode

    crazydiode Guest

    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..
    crazydiode, May 12, 2006
    #1
    1. Advertising

  2. 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.
    Joe Kesselman, May 12, 2006
    #2
    1. Advertising

  3. crazydiode

    crazydiode Guest

    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....
    crazydiode, May 12, 2006
    #3
  4. crazydiode wrote:
    > 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?
    Joe Kesselman, May 12, 2006
    #4
  5. crazydiode

    crazydiode Guest

    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 ??
    crazydiode, May 12, 2006
    #5
  6. crazydiode wrote:
    > 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.
    Joe Kesselman, May 12, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    651
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,675
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    621
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    715
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    667
Loading...

Share This Page