Happy New Year everyone ... back on the horse again, I'll try and stay on this time!
I have had several requests on how to handle HTML formatted XML in their templates, to save you time finding my email and me answering here's the solution. When there is HTML formatted data in the source XML it needs to be converted to the XSLFO format prior to being put into the final document otherwise the raw HTML will be inserted so something like
<B>This is bold text</B>
needs to be converted to its XSLFO equivalent
<fo:inline font-weight="bold">This is bold text</fo:inline>
We can do this reasonably simply but their is one caveat. The HTML must be in the XHTML format i.e. <BR> is not going to work it needs to be <BR></BR> for a new line. So, if you have HTML in your data you'll need to clean it up to get XHTML using something like JTidy or Tidy. Once you have the cleaned up XHTML you're ready to convert it to the XSLFO equivalent.
To do this, the easiest way is to use a sub template, Im advocating that because you are likely to need to use the functionality across multiple templates. The sub template is going to be written in XSLFO, not the easiest language in the world but I have posted a sample template that handles much of the common HTML for you. Its basically a set of functions that accept the input and convert it if necessary to XSLFO. Take a look at the template and you'll see a series of functions with a similar format:
<xsl:template match="STRONG|B|b">The first line is looking for 'STRONG', 'B' or 'b' to match on the HTML markup for bold. The template then replaces the matched HTML string with its XSLFO equivalent. The posted sample has some more complex examples handling lists, bullets, etc. So thats the sub template, now we need to register and call in it in the main template.
<fo:inline font-weight="bold">
<xsl:apply-templates />
</fo:inline>
</xsl:template>
In the top of the main template we register our interest in the sub template thus:
<?import:file:///c:temphtmlmarkup.xsl?>Im using a the file URI to be able to run it on my desktop for the sample but you can use a full URL if you are using the Enterprise release or the XMLP URI reference if you are using EBS, see the user guide on referencing sub templates.
Our main template is now aware of the sub template, we now need to call the functions therein to get our html converted. For each field in our RTF template that might have HTML markup we need to call for the markup functions to be applied this requires a simple call:
<xsl:apply-templates select="PROJECTSCOPE"/>
Sadly we have to resort to XSL as the XMLP aliases do not support calling a template on a data element yet. This command tells the processor to apply all templates to the value of the element PROJECTSCCOPE. It will then cycle through the sub template functions looking for a match. You need to do this for all the fields that may have HTML markup in the XML data.
Now when you run the template against the XML the HTML markup will be converted to XSLFO markup et voila you have correctly formatted text in your output. You can get the complete example here.
Happy Templating
Comments (9)
Also we can see another XDO issue here:
XDO does not support CODEBASE (using relative path to subtemplate) - as standard feature of xslt transformers (including another Oracle's one).
It is a problem if we have to manage deployment and testings in different environments.
Posted by Victor Dusanyuk | January 10, 2007 5:30 AM
Posted on January 10, 2007 05:30
Really good list of HTML to XSL-FO conversions here: http://www.ibm.com/developerworks/library/x-xslfo2app/
Posted by Bryan | November 15, 2007 7:00 PM
Posted on November 15, 2007 19:00
Tim,
I have a situation, where I have a html tagged content stored in a column - primarily the Rich Text editor content. Thus, it has html tags
, , etc...
I am generating a report using a data template, associated with a RTF layout template, which in turn calls a sub-template, which is the one you provided in the example above. The tags did not get resolved.
What I noticed was that the xml generated by the data template does not contain html tags, but things like < and > instead of . If I provide a xml document with proper html tags, I get the converted output.
How can I get the xml file with the proper html tags?
Rgds.
Taheri
Posted by Taheri Saifee | August 24, 2008 10:48 AM
Posted on August 24, 2008 10:48
Hello,
I have the same problem as previous post....
i fight with this....
Paul
Posted by Paul B | August 28, 2008 7:09 AM
Posted on August 28, 2008 07:09
Paul
Can you use the forum and post your XML there?
Tim
Posted by Tim | August 30, 2008 7:21 AM
Posted on August 30, 2008 07:21
Tim,
I've got a case raised with PeopleSoft (Oracle) about a problem I have with your solution not working due to escaped HTML characters in the XML data source. They suggested I contact you directly about this - I've written it up in more detail here
http://forums.oracle.com/forums/thread.jspa?threadID=703696
Posted by Richard | September 16, 2008 3:55 AM
Posted on September 16, 2008 03:55
great code thanks...
Posted by Oyunlar | September 20, 2008 9:05 PM
Posted on September 20, 2008 21:05
Hey, how did you get the html formatting in your xml in the first place? Because when I have something like <b>this is bold</b> in a table in a database, it becomes something like <b>this is bold</b> thus "coding my code" and making it very difficult to use your solution to the problem of html formatted data.
Posted by Jason M. Burke | September 29, 2008 7:08 AM
Posted on September 29, 2008 07:08
I would like to pass a parameter to the XSL - something like this:
<xsl:apply-templates select="PROJECTSCOPE"/>
<xsl:with-param name="allowFonts">no</xsl:with-param>
</xsl:call-template>
This does not work. I need to have the parameter available to the entire XSL.
Thanks
Posted by Karen | October 3, 2008 2:33 PM
Posted on October 3, 2008 14:33