Re: how to translate XML with XHTML-formatted element to FO

Hi Maik,

Mixing two XML languages like that is normally done with XML namespaces.
XHTML resides in the namespace "http://www.w3.org/1999/xhtml". XSLT
style sheets can be integrated easier when namespaces are used. Your
example would then look like:

<BOOK xmlns:xh="http://www.w3.org/1999/xhtml">
   ...
   <TITLE><xh:b>...</xh:b></TITLE>
   ...
</BOOK>

If your xhtml-to-fo.xsl style sheet doesn't use the XHTML namespace
and if you are sure there are no element name clashes with your BOOK
document type, you can drop the namespace and include the xhtml-to-fo.xsl
in the book-fo-fo.xsl with an xsl:include element.

That should work if the templates in book-to-fo.xsl use xsl:apply-templates
to forward further processing of their contents to other templates, instead
of "grabbing" their contents and emitting elements in the result tree for it.
This will result in more and smaller templates, which are more declarative.

For example:

<xsl:template match="TITLE">
   <fo:block font-size="120%" ...>
     <xsl:apply-templates select="@* | node()"/>
   </fo:block>
</xsl:template>

instead of:

<xsl:template match="TITLE">
   <fo:block font-size="120%" ...>
     <xsl:value-of select="text()"/>
   </fo:block>
</xsl:template>

The first version assumes you have a generic copy template that also
copies text.

<xsl:template match="@* | node()">
   <fo:copy>
     <xsl:apply-templates select="@* | node()"/>
   </fo:copy>
</xsl:template>

Now xhtml-to-fo.xsl can provide a template for "b", which
is triggered the moment you put a "b" around the text of TITLE. The
template for "b" will have to be written in the same way, i.e. like this:

<xsl:template match="xh:b">
   <fo:inline font-weight="bold">
     <xsl:apply-templates select="@* | node()"/>
   </fo:inline>
</xsl:template>

Regards,

Werner.

Magic Finger wrote:
> 
> Hello,
> 
> how can I translate XML files (like the following), which contain
> XHTML-formatted text in one element, to PDF using Apache FOP ?
> 
>  --- books.xml --->
>  <BOOK>
>    <AUTHOR>Walt Disney</AUTHOR>
>    <PRICE>19.90</PRICE>
>    <TITLE>&lt;b&gt;Donald Duck - &lt;i&gt;The True Story&lt;/i&gt; 
> &lt;/b&gt;</TITLE>
>  </BOOK>
> 
>  <BOOK>
>    <AUTHOR>Matt Groening</AUTHOR>
>    <PRICE>25.00</PRICE>
>    <TITLE>&lt;b&gt;Homer Simpson For President&lt;/b&gt;</TITLE>
>  </BOOK>
>    ...
>  <--- books.xml ---
> 
> I mean how should an appropriate XSL stylesheet be designed best to handle
> straight forward FO formatting for <AUTHOR> and <PRICE> as well as for
> additional transformation of XHTML-formatted <TITLE> ?
> 
> I just need some general ideas on how I can process XML files with
> sub-structured (XHTML-formatted) elements by one (or more) XSL
> stylesheet(s).
> 
> My problem is not to translate XHTML-formatted text to FO resp. PDF (for
> this purpose I already have a working "xhtml-to-fo.xsl" stylesheet).
> Rather I have no idea at the moment on how to integrate the existing
> xhtml-to-fo.xsl code and templates into the main XSL stylesheet for
> generating the FO output.
> 
> Currently I'm only able to translate content of <TITLE> element as plain
> text to FO output, i.e. FO output for <TITLE> shows the tags <b>...</b> and
> <i>...</i> as is, without applied bold and italic formatting on title text.
> 
> Any hints, tips or code fragments would be very much appreciated!
> 
> Best regards,
> Maik
> 
> 
> 

-- 
Werner Donné  --  Re BVBA
Engelbeekstraat 8
B-3300 Tienen
tel: (+32) 486 425803	e-mail: werner.donne@re.be

Received on Wednesday, 13 April 2005 16:05:09 UTC