Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-24-2009, 09:02 AM
Member
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
khdani is on a distinguished road
Default xml sort using xslt
hello,
i try to sort xml using this xslt:
Code:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="link to w3c" Version="1.0">
<xsl:output method="text"/>
<xsl:template match="Messages">
<xsl:apply-templates>
<xsl:sort select="EMAIL" order="descending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match = "Message">
ID: <xsl:apply-templates select="ID"/>EMAIL: <xsl:apply-templates select="EMAIL"/>
SUBJECT: <xsl:apply-templates select="SUBJECT"/>
DATE: <xsl:apply-templates select ="DATE"/>
BODY: <xsl-apply-templates select = "BODY"/>
<xsl:text></xsl:tetx>
</xsl:template></xsl:stylesheet>
and this code in java:
Code:
		Source xsl = new StreamSource(xslBuilder.toString());
		Source xml = new StreamSource(this.path);
			DOMResult result = new DOMResult(this.doc);
			TransformerFactory transFactory = TransformerFactory.newInstance();
			javax.xml.transform.Transformer transformer = transFactory.newTransformer(xsl);
			transformer.transform(xml, result);
(the xsl is in string)
but i get an error: no protocol
and fatal error: could not compile stylesheet

please help...
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-24-2009, 04:33 PM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
Default
Is your xslBuilder correctly initialised? Is this.path correct? Does both your xml and your xsl follow the w3c standards exactly?

(possibly useful link: XSL Transformations (XSLT) Version 2.0)
__________________
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-24-2009, 04:57 PM
Member
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
khdani is on a distinguished road
Default
xslBuilder is StringBuilder, the whole xsl is created in StringBuilder
'this.path' is also correct
both xml and xsl follow w3c:
xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<Mail>
	<Message>
		<ID>1</ID>
		<EMAIL>mail@mail.com</EMAIL>
		<SUBJECT>Subject1</SUBJECT>
		<DATE>10/05/2009</DATE>
		<BODY><![CDATA[Hello :)]]></BODY>
	</Message>
</Mail>
xsl
Code:
		StringBuilder xslBuilder = new StringBuilder();
		xslBuilder.append("<?xml version=\"1.0\" encoding =\"UTF-8\" ?>");
		xslBuilder.append("<xsl:stylesheet xmlns:xsl=\"link wo w3c" Version=\"1.0\">");
		xslBuilder.append("<xsl:output method=\"text\"/>");
		xslBuilder.append("<xsl:template match=\"Messages\">");
		xslBuilder.append("<xsl:apply-templates>");
		xslBuilder.append("<xsl:sort select=\"" + sortArg+"\" order=\"" + order + "\"/>");
		xslBuilder.append("</xsl:apply-templates>");
		xslBuilder.append("</xsl:template>");
		
		xslBuilder.append("<xsl:template match = \"Message\">");
		xslBuilder.append("ID: <xsl:apply-templates select=\"ID\"/>");
		xslBuilder.append("EMAIL: <xsl:apply-templates select=\"EMAIL\"/>");
		xslBuilder.append("SUBJECT: <xsl:apply-templates select=\"SUBJECT\"/>");
		xslBuilder.append("DATE: <xsl:apply-templates select =\"DATE\"/>");
		xslBuilder.append("BODY: <xsl-apply-templates select = \"BODY\"/>");
		xslBuilder.append("<xsl:text>");
		xslBuilder.append("</xsl:tetx>");
		
		xslBuilder.append("</xsl:template>");
		
		xslBuilder.append("</xsl:stylesheet>");
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-24-2009, 10:36 PM
OrangeDog's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Cambridge, UK
Posts: 838
Rep Power: 2
OrangeDog is on a distinguished road
Default
Does this operation work when not executed in Java? That is, is the output as expected if you simply apply the stylesheet to the xml and view in a browser?
__________________
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-25-2009, 01:19 PM
Member
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
khdani is on a distinguished road
Default
well actually i did had some errors in xsl, but i fixed them and verified with XMLSpy. XMLSpy transforms the xml correctly but the java code gives same error
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-25-2009, 01:21 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 703
Rep Power: 2
RamyaSivakanth is on a distinguished road
Default
Could u paste the complete code with xsl?
__________________
Ramya
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-25-2009, 01:53 PM
Member
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
khdani is on a distinguished road
Default
i think i fixed the problem,
i should set systemID to some xslt file.
but i can't parse the result dom.
Code:
			DOMSource source = new DOMSource(this.doc);
			DOMResult result = new DOMResult();			
			TransformerFactory transFactory = TransformerFactory.newInstance();
			javax.xml.transform.Transformer transformer = transFactory.newTransformer(xsl);
			transformer.transform(source, result);	
			this.doc = (Document)result.getNode();
if i call:
[code]
this.doc.getDocumentElement().getElementsByTagName ("tagname");
i get an empty list...
but if i check in debugger the result node indeed has the xml dom tree transformed...
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-25-2009, 02:08 PM
Member
 
Join Date: Aug 2008
Posts: 14
Rep Power: 0
khdani is on a distinguished road
Default
fixed that too
thank you
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-20-2010, 09:04 AM
DSS DSS is offline
Member
 
Join Date: Jun 2010
Posts: 1
Rep Power: 0
DSS is on a distinguished road
Default
Sort XML by Date using XSL | Easy Tips and Tricks

Follow this link for the solution. Works great for me. Cheers!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Merge Sort to sort an ArrayList of Strings coldfire New To Java 3 03-13-2009 01:03 AM
XSLT - conditions JavaForums Java Blogs 0 09-23-2008 03:20 PM
XSLT and SAX Parser coolnfunky_raj New To Java 0 07-19-2008 12:25 AM
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 08:04 PM
XSLT Transforms ashleyh Java Servlet 0 03-08-2008 05:13 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:45 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org