Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Query regarding XSLT "Root element not set"

Reply
Thread Tools

Query regarding XSLT "Root element not set"

 
 
Amit Jain
Guest
Posts: n/a
 
      07-22-2009
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:40
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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      07-23-2009
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:40
>> 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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      07-23-2009
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:40
> 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(ne w 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"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xslutput 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<foobar>
<xsl:value-of select="message"/>
</foobar>
</xsl:template>
</xsl:stylesheet>

Arne
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      07-23-2009
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:40
>> 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(ne w 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"
>> xmlnssl="http://www.w3.org/1999/XSL/Transform">
>> <xslutput 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"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xslutput 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
 
Reply With Quote
 
Amit Jain
Guest
Posts: n/a
 
      07-23-2009
Hi All,

Thanks a lot.

Your proposed solution works.


regards,

Amit
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Query regarding Catalog resolver 'cvc-elt.1: Cannot find thedeclaration of element' Amit Jain Java 9 07-21-2009 11:23 AM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
xslt not working when xml has xmlns="urn" in root element. Lee XML 3 04-04-2007 06:07 PM
Query : Number of Attributes under an element & atleast 1 occurence of element/Attribute kosaraju.puneeth@gmail.com XML 0 10-26-2005 07:09 AM
XSLT: How to write "Yes" if element/attribute exist or "no" if not? Mark Richards XML 1 07-24-2004 03:08 PM



Advertisments