This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes Please point me in the right direction with XSL transformation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Please point me in the right direction with XSL transformation" Watch "Please point me in the right direction with XSL transformation" New topic
Author

Please point me in the right direction with XSL transformation

Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

I still have about 7 chapters to go to get a better understanding of XSLT, but I've done basic XML->HTML transformations already. I have a nice HTML/CSS template that I'd like to use for my project, but since an XSL file is XML, then the parser will be picky - and rightly so. HTML has its quirks (ok, a lot of them), and since it does, how can I make sure the parser actually parses everything while ignoring all the HTML in a file?

Having something like this works great:



But what if it's just a normal file with attributes and CSS, tags which are not closed, etc? Surely there must be a way and this is where I'm stuck with my project.

Oh, by the way - I'm using Saxon Home Edition for XSLT. Unfortunately it's version 6.5.5 as I just downloaded the Ubuntu package, but will probably upgrade it tomorrow to 9.3.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

If I remember right, the correct approach is to make sure tags are closed. Imagine you're writing XHTML - make sure everything is well formed (CSS isn't a problem - it's in a <style> tag and should be plain text other than that). Then, the important part is the <xsl:output method="html"/> line. That tells the transformer to output HTML. So you put in a <br/> and it will output <br>, for instance.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Thanks Matthew. I suppose I need to convert the HTML file to XHTML.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I'm not sure I understand what you mean by "the HTML file" now. Do you have an HTML file which is going to be transformed by that XSLT? If so, yes, it has to be well-formed XML, but that isn't the usual process. Usually people just start with XML which doesn't have to be HTML in any way, and transform that into HTML.

Or if you're producing an HTML file from that XSLT, as your example shows, then what Matthew said is correct. Generate output with matching open and close tags and so on, and it's the transformer's job to produce real HTML. You don't have to post-process it.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Paul Clapham wrote:Do you have an HTML file which is going to be transformed by that XSLT? If so, yes, it has to be well-formed XML, but that isn't the usual process. Usually people just start with XML which doesn't have to be HTML in any way, and transform that into HTML.


Yes, exactly. I have a nice HTML/CSS template and I need XSLT to insert some text from an XML file into it. I did the following to remedy the problem:

1. Used tidyhtml to clean up the file, available at http://tidy.sourceforge.net/ but I downloaded it via macports.
2. Removed the remaining errors until it validated here: http://validator.w3.org/check
3. Got rid of the following line:



which caused a Transformation failed: org.xml.sax.SAXException: Scanner State 24 not Recognized error and drove me nuts.

Of course the HTML file was changed to .xsl and appropriate headers were added.

...and it worked!

Now I need to learn more XSLT and figure out how to use conditional statements, but I'm one step ahead.

Thanks again.
 
 
subject: Please point me in the right direction with XSL transformation