PDA

View Full Version : A sample XSLT transformation from Qt rich text to XHTML



patrik08
19th June 2006, 23:53
I like a clean xhtml code .... why? faster and bring him to other format... advantage
QTextEdit html code, cold shower run only so:

xslt code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cms="http://www.pulitzer.ch/2005/PuliCMS/1.0"
xmlns:s="http://www.pulitzer.ch/2005/shop/shema/1.0/"
xmlns:php="http://php.net/xsl"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:regexp="http://exslt.org/regular-expressions"
exclude-result-prefixes="dc dcterms regexp xmlns cms s php">

<xsl:output method="xhtml" encoding="utf-8"/>
<!-- QTextEdit to xhtml http://doc.trolltech.com/4.1/qtextedit.html -->
<!-- Write by PPK-Webprogramm www.ciz.ch -->
<!-- replace class name to your own... -->
<!-- <root><QTextEdit_html/></root> and convert here! -->

<!-- base root -->
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<!-- base root -->
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template>

<!-- span aberation -->
<xsl:template match="span">
<xsl:variable name="qtform" select="@style" />
<xsl:if test="contains($qtform, 'underline')">
<u>
<xsl:apply-templates/>
</u>
</xsl:if>
<xsl:if test="contains($qtform, 'italic')">
<i>
<xsl:apply-templates/>
</i>
</xsl:if>
<xsl:if test="contains($qtform, 'font-weight')">
<b>
<xsl:apply-templates/>
</b>
</xsl:if>
</xsl:template>

<!-- links href + target -->
<xsl:template match="a">
<!-- qlineedit remove target link.html#target=_self format -->
<a>
<xsl:attribute name="href"><xsl:value-of select="substring-before(@href,'#target=')" /></xsl:attribute>
<xsl:attribute name="target"><xsl:value-of select="substring-after(@href,'#target=')" /></xsl:attribute>
<xsl:apply-templates/>
</a>
</xsl:template>

<xsl:template match="div">
<div class="UnaColonna">
<xsl:apply-templates/>
</div>
</xsl:template>


<xsl:template match="img">
</xsl:template>

<xsl:template match="li">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>

<xsl:template match="ol">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>

<xsl:template match="ul">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>

<!-- remove table on html or set to css2 but maintain to pdf apache xslt-fop -->
<xsl:template match="table">
<var class="_fo_table_">
<xsl:apply-templates/>
</var>
</xsl:template>
<xsl:template match="tr">
<var class="_fo_table-row_">
<xsl:apply-templates/>
</var>
</xsl:template>
<xsl:template match="td">
<var class="_fo_table-cell_">
<xsl:apply-templates/>
</var>
</xsl:template>
<!-- remove table -->

<!-- paragraph put your own classname to align paragraph -->
<xsl:template match="p">
<p>
<xsl:if test="@align = 'center'">
<xsl:attribute name="class">Centrato</xsl:attribute>
</xsl:if>
<xsl:if test="@align = 'justify'">
<xsl:attribute name="class">Giustificato</xsl:attribute>
</xsl:if>
<xsl:if test="@align = 'right'">
<xsl:attribute name="class">AllineaDestra</xsl:attribute>
</xsl:if>
<xsl:if test="@align = 'left'">
<xsl:attribute name="class">AllineaSinistra</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</p>
</xsl:template>
<!-- paragraph -->
</xsl:stylesheet>

jacek
16th July 2006, 17:23
Best place for such examples is our wiki (http://wiki.qtcentre.org).