Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Query regarding XSLT "Root element not set"

517 views
Skip to first unread message

Amit Jain

unread,
Jul 22, 2009, 9:28:45 AM7/22/09
to
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:output 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

Peter Duniho

unread,
Jul 22, 2009, 7:56:54 PM7/22/09
to
On Wed, 22 Jul 2009 06:28:45 -0700, Amit Jain <amitat...@gmail.com>
wrote:

> 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 ********** [...]

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.

Pete

Lew

unread,
Jul 22, 2009, 8:45:58 PM7/22/09
to
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

Arne Vajhøj

unread,
Jul 22, 2009, 10:38:50 PM7/22/09
to

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:output 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:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<foobar>


<xsl:value-of select="message"/>

</foobar>
</xsl:template>
</xsl:stylesheet>

Arne

Arne Vajhøj

unread,
Jul 22, 2009, 10:44:25 PM7/22/09
to

I also changed method from text to xml.

Arne

Amit Jain

unread,
Jul 23, 2009, 12:08:49 AM7/23/09
to
Hi All,

Thanks a lot.

Your proposed solution works.


regards,

Amit

0 new messages