Results 1 to 9 of 9
  1. #1
    khdani is offline Member
    Join Date
    Aug 2008
    Posts
    14
    Rep Power
    0

    Default xml sort using xslt

    hello,
    i try to sort xml using this xslt:
    Java 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:
    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...

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    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

  3. #3
    khdani is offline Member
    Join Date
    Aug 2008
    Posts
    14
    Rep Power
    0

    Default

    xslBuilder is StringBuilder, the whole xsl is created in StringBuilder
    'this.path' is also correct
    both xml and xsl follow w3c:
    xml
    Java 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
    Java 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>");

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    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

  5. #5
    khdani is offline Member
    Join Date
    Aug 2008
    Posts
    14
    Rep Power
    0

    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

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    Could u paste the complete code with xsl?
    Ramya:cool:

  7. #7
    khdani is offline Member
    Join Date
    Aug 2008
    Posts
    14
    Rep Power
    0

    Default

    i think i fixed the problem,
    i should set systemID to some xslt file.
    but i can't parse the result dom.
    Java 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...

  8. #8
    khdani is offline Member
    Join Date
    Aug 2008
    Posts
    14
    Rep Power
    0

    Default

    fixed that too
    thank you

  9. #9
    DSS
    DSS is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 01:03 AM
  2. XSLT and SAX Parser
    By coolnfunky_raj in forum New To Java
    Replies: 0
    Last Post: 07-19-2008, 12:25 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  4. XSLT Transforms
    By ashleyh in forum Java Servlet
    Replies: 0
    Last Post: 03-08-2008, 05:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •