Query regarding XSLT "Root element not set"

Discussion in 'Java' started by Amit Jain, Jul 22, 2009.

  1. Amit Jain

    Amit Jain Guest

    Hi All,

    I am getting below exception will running program:
    ************* exception start **********
    false
    Exception in thread "main" java.lang.IllegalStateException: Root
    element not set
    at org.jdom.Document.getContent(Document.java:408)
    at SimpleXalan1.main(SimpleXalan1.java:70)
    ************* exception ends **********

    **************** Java Program Start *************
    public class SimpleXalan1{
    public static void main(String[] args)
    throws MalformedURLException, SAXException, Exception{

    SAXBuilder builder = new SAXBuilder();

    Document xsltdoc = builder.build("C://Workspace//xsltdemo//
    classes//message.xsl");
    Document inputDoc = builder.build("C://Workspace//xsltdemo//
    classes//message.xml");
    JDOMResult jdomResult = new JDOMResult();
    Transformer obj = TransformerFactory.newInstance().newTransformer
    (new JDOMSource(xsltdoc));
    obj.transform(new JDOMSource(inputDoc), jdomResult);
    Document resultDoc = jdomResult.getDocument();

    System.out.println(resultDoc.hasRootElement());
    System.out.println(resultDoc.getContent());
    }
    }

    *************** Java Program Ends *************

    *************** message.xml Start *************
    <?xml version="1.0" encoding="UTF-8"?>
    <message>Yep, it worked!</message>
    *************** message.xml Ends *************

    *************** message.xsl Start *************
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="text" encoding="UTF-8"/>
    <!-- simply copy the message to the result tree -->
    <xsl:template match="/">
    <xsl:value-of select="message"/>
    </xsl:template>
    </xsl:stylesheet>
    *************** message.xsl Ends *************
    Is there any problem with Java code or xml or xsl file.

    Please suggest me.

    Thanks & regards,
    Amit Jain
    Amit Jain, Jul 22, 2009
    #1
    1. Advertising

  2. Amit Jain

    Lew Guest

    Amit Jain wrote:
    >> I am getting below exception will running program:
    >> ************* exception start **********
    >> false
    >> Exception in thread "main" java.lang.IllegalStateException: Root
    >> element not set
    >> at org.jdom.Document.getContent(Document.java:408)
    >> at SimpleXalan1.main(SimpleXalan1.java:70)
    >> ************* exception ends ********** [...]


    Peter Duniho wrote:
    > Just like the exception and the return value from the method
    > "hasRootElement()" both say, your output document has no root element.
    > You probably want to use "copy-of" instead of "value-of", and you probably
    > want a copy of "." not "message".
    >
    > But in any case, your question is not a Java question at all. It's an
    > XSLT question and as such you should be posting it in an XSLT forum (e.g.
    > the XSLT mailing list). In the meantime, get a copy of the xsltproc
    > utility (if it's not already installed on your computer) and use that to
    > test your XSLT code, so that you're not trying to figure out the XML,
    > XSLT, and Java all at the same time.


    One also has to wonder about the doubled slashes in the path names.

    Amit Jain wrote:
    > Document xsltdoc =
    > builder.build("C://Workspace//xsltdemo//classes//message.xsl");
    > Document inputDoc =
    > builder.build("C://Workspace//xsltdemo//classes//message.xml");


    Why do you double the slashes?

    --
    Lew
    Lew, Jul 23, 2009
    #2
    1. Advertising

  3. Amit Jain

    Arne Vajhøj Guest

    Amit Jain wrote:
    > I am getting below exception will running program:
    > ************* exception start **********
    > false
    > Exception in thread "main" java.lang.IllegalStateException: Root
    > element not set
    > at org.jdom.Document.getContent(Document.java:408)
    > at SimpleXalan1.main(SimpleXalan1.java:70)
    > ************* exception ends **********
    >
    > **************** Java Program Start *************
    > public class SimpleXalan1{
    > public static void main(String[] args)
    > throws MalformedURLException, SAXException, Exception{
    >
    > SAXBuilder builder = new SAXBuilder();
    >
    > Document xsltdoc = builder.build("C://Workspace//xsltdemo//
    > classes//message.xsl");
    > Document inputDoc = builder.build("C://Workspace//xsltdemo//
    > classes//message.xml");
    > JDOMResult jdomResult = new JDOMResult();
    > Transformer obj = TransformerFactory.newInstance().newTransformer
    > (new JDOMSource(xsltdoc));
    > obj.transform(new JDOMSource(inputDoc), jdomResult);
    > Document resultDoc = jdomResult.getDocument();
    >
    > System.out.println(resultDoc.hasRootElement());
    > System.out.println(resultDoc.getContent());
    > }
    > }
    >
    > *************** Java Program Ends *************


    Modified version:

    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;

    import org.jdom.Document;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;
    import org.jdom.transform.JDOMResult;
    import org.jdom.transform.JDOMSource;

    public class SimpleXalan {
    public static void main(String[] args) throws Exception {
    SAXBuilder builder = new SAXBuilder();
    Document xsltdoc = builder.build("C:/message.xsl");
    Document inputDoc = builder.build("C:/message.xml");
    JDOMResult jdomResult = new JDOMResult();
    Transformer obj =
    TransformerFactory.newInstance().newTransformer(new JDOMSource(xsltdoc));
    obj.transform(new JDOMSource(inputDoc), jdomResult);
    Document resultDoc = jdomResult.getDocument();
    XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
    System.out.println(fmt.outputString(resultDoc));
    }
    }

    I changed the // in filenames to / and used some nicer
    output.

    > *************** message.xml Start *************
    > <?xml version="1.0" encoding="UTF-8"?>
    > <message>Yep, it worked!</message>
    > *************** message.xml Ends *************
    >
    > *************** message.xsl Start *************
    > <?xml version="1.0" encoding="UTF-8"?>
    > <xsl:stylesheet
    > version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput method="text" encoding="UTF-8"/>
    > <!-- simply copy the message to the result tree -->
    > <xsl:template match="/">
    > <xsl:value-of select="message"/>
    > </xsl:template>
    > </xsl:stylesheet>
    > *************** message.xsl Ends *************


    As the error message suggests you are missing the root element, so
    I added one with the traditional name of foobar:


    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml" encoding="UTF-8"/>
    <xsl:template match="/">
    <foobar>
    <xsl:value-of select="message"/>
    </foobar>
    </xsl:template>
    </xsl:stylesheet>

    Arne
    Arne Vajhøj, Jul 23, 2009
    #3
  4. Amit Jain

    Arne Vajhøj Guest

    Arne Vajhøj wrote:
    > Amit Jain wrote:
    >> I am getting below exception will running program:
    >> ************* exception start **********
    >> false
    >> Exception in thread "main" java.lang.IllegalStateException: Root
    >> element not set
    >> at org.jdom.Document.getContent(Document.java:408)
    >> at SimpleXalan1.main(SimpleXalan1.java:70)
    >> ************* exception ends **********
    >>
    >> **************** Java Program Start *************
    >> public class SimpleXalan1{
    >> public static void main(String[] args)
    >> throws MalformedURLException, SAXException, Exception{
    >>
    >> SAXBuilder builder = new SAXBuilder();
    >>
    >> Document xsltdoc = builder.build("C://Workspace//xsltdemo//
    >> classes//message.xsl");
    >> Document inputDoc = builder.build("C://Workspace//xsltdemo//
    >> classes//message.xml");
    >> JDOMResult jdomResult = new JDOMResult();
    >> Transformer obj = TransformerFactory.newInstance().newTransformer
    >> (new JDOMSource(xsltdoc));
    >> obj.transform(new JDOMSource(inputDoc), jdomResult);
    >> Document resultDoc = jdomResult.getDocument();
    >>
    >> System.out.println(resultDoc.hasRootElement());
    >> System.out.println(resultDoc.getContent());
    >> }
    >> }
    >>
    >> *************** Java Program Ends *************

    >
    > Modified version:
    >
    > import javax.xml.transform.Transformer;
    > import javax.xml.transform.TransformerFactory;
    >
    > import org.jdom.Document;
    > import org.jdom.input.SAXBuilder;
    > import org.jdom.output.Format;
    > import org.jdom.output.XMLOutputter;
    > import org.jdom.transform.JDOMResult;
    > import org.jdom.transform.JDOMSource;
    >
    > public class SimpleXalan {
    > public static void main(String[] args) throws Exception {
    > SAXBuilder builder = new SAXBuilder();
    > Document xsltdoc = builder.build("C:/message.xsl");
    > Document inputDoc = builder.build("C:/message.xml");
    > JDOMResult jdomResult = new JDOMResult();
    > Transformer obj =
    > TransformerFactory.newInstance().newTransformer(new JDOMSource(xsltdoc));
    > obj.transform(new JDOMSource(inputDoc), jdomResult);
    > Document resultDoc = jdomResult.getDocument();
    > XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
    > System.out.println(fmt.outputString(resultDoc));
    > }
    > }
    >
    > I changed the // in filenames to / and used some nicer
    > output.
    >
    >> *************** message.xml Start *************
    >> <?xml version="1.0" encoding="UTF-8"?>
    >> <message>Yep, it worked!</message>
    >> *************** message.xml Ends *************
    >>
    >> *************** message.xsl Start *************
    >> <?xml version="1.0" encoding="UTF-8"?>
    >> <xsl:stylesheet
    >> version="1.0"
    >> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >> <xsl:eek:utput method="text" encoding="UTF-8"/>
    >> <!-- simply copy the message to the result tree -->
    >> <xsl:template match="/">
    >> <xsl:value-of select="message"/>
    >> </xsl:template>
    >> </xsl:stylesheet>
    >> *************** message.xsl Ends *************

    >
    > As the error message suggests you are missing the root element, so
    > I added one with the traditional name of foobar:
    >
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput method="xml" encoding="UTF-8"/>
    > <xsl:template match="/">
    > <foobar>
    > <xsl:value-of select="message"/>
    > </foobar>
    > </xsl:template>
    > </xsl:stylesheet>


    I also changed method from text to xml.

    Arne
    Arne Vajhøj, Jul 23, 2009
    #4
  5. Amit Jain

    Amit Jain Guest

    Hi All,

    Thanks a lot.

    Your proposed solution works.


    regards,

    Amit
    Amit Jain, Jul 23, 2009
    #5
    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:
    633
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,657
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    610
    Laurens
    Jan 26, 2004
  4. David Blickstein
    Replies:
    1
    Views:
    373
    Dimitre Novatchev
    Sep 14, 2005
  5. Rakesh
    Replies:
    2
    Views:
    333
    Rakesh
    Apr 11, 2008
Loading...

Share This Page