vrijdag 24 augustus 2012

BiPublisher 10g: XSLT for HTML translations

Context


BiPublisher: 10.1.3.4.1

XSLT File

Here is a file you can use to transform HTML code into RTF code.
See this blog on how to use it.

<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
    <!-- CUSTOMIZE html->fo for Rich Text Editor-->
    <!-- elements -->
<xsl:template match="BLOCKQUOTE">
 <fo:block start-indent="0.25in">
 <xsl:apply-templates select="*|text()"/>
 </fo:block>
</xsl:template>
<xsl:template match="P|p">
 <fo:block white-space-collapse="false" padding-bottom="3pt" linefeed-treatment="preserve">
  <xsl:apply-templates select="text()|*|@*"/>
 </fo:block>
</xsl:template>
<xsl:template match="ol|OL">
  <fo:list-block provisional-distance-between-starts="1cm"
    provisional-label-separation="0.5cm">
    <xsl:attribute name="space-after">
      <xsl:choose>
        <xsl:when test="ancestor::ul or ancestor::ol">
          <xsl:text>0pt</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>12pt</xsl:text>
          </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:attribute name="start-indent">
      <xsl:variable name="ancestors">
        <xsl:choose>
          <xsl:when test="count(ancestor::ol) or count(ancestor::ul)">
            <xsl:value-of select="1 + 
                                  (count(ancestor::ol) + 
                                   count(ancestor::ul)) * 
                                  1.25"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>1</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:value-of select="concat($ancestors, 'cm')"/>
    </xsl:attribute>
    <xsl:apply-templates select="*"/>
  </fo:list-block>
</xsl:template>

<xsl:template match="ol/li|OL/LI">
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block>
        <xsl:variable name="value-attr">
          <xsl:choose>
            <xsl:when test="../@start">
              <xsl:number value="position() + ../@start - 1"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:number value="position()"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="../@type='i'">
            <xsl:number value="$value-attr" format="i. "/>
          </xsl:when>
          <xsl:when test="../@type='I'">
            <xsl:number value="$value-attr" format="I. "/>
          </xsl:when>
          <xsl:when test="../@type='a'">
            <xsl:number value="$value-attr" format="a. "/>
          </xsl:when>
          <xsl:when test="../@type='A'">
            <xsl:number value="$value-attr" format="A. "/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:number value="$value-attr" format="1. "/>
          </xsl:otherwise>
        </xsl:choose>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block>
        <xsl:apply-templates select="*|text()"/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>


<xsl:template match="ul|UL">
  <fo:list-block provisional-distance-between-starts="1cm"
    provisional-label-separation="0.5cm">
    <xsl:attribute name="space-after">
      <xsl:choose>
        <xsl:when test="ancestor::ul or ancestor::ol">
          <xsl:text>0pt</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>12pt</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:attribute name="start-indent">
      <xsl:variable name="ancestors">
        <xsl:choose>
          <xsl:when test="count(ancestor::ol) or count(ancestor::ul)">
            <xsl:value-of select="1 + 
                                  (count(ancestor::ol) + 
                                   count(ancestor::ul)) * 
                                  1.25"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>1</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:value-of select="concat($ancestors, 'cm')"/>
    </xsl:attribute>
    <xsl:apply-templates select="*"/>
  </fo:list-block>
</xsl:template>

<xsl:template match="ul/li|UL/LI">
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block>&#x2022;</fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block>
        <xsl:apply-templates select="*|text()"/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>
    
    <!--xsl:template match="OL|UL">
        <fo:list-block>
            <xsl:apply-templates/>
        </fo:list-block>
    </xsl:template>
    <xsl:template match="OL/OL|UL/UL">
        <fo:list-item>
            <fo:list-item-label>
                <fo:block/>
            </fo:list-item-label>
            <fo:list-item-body start-indent="body-start()">
                <xsl:apply-templates/>
            </fo:list-item-body>
        </fo:list-item>
    </xsl:template>
    <xsl:template match="LI">
        <fo:list-item>
            <fo:list-item-label end-indent="label-end()">
                <fo:block>
                    <xsl:choose>
                        <xsl:when test='name(..)!="OL"'>
                            <xsl:text>&#xb7;</xsl:text>
                        </xsl:when>
                        <xsl:when test='name(../..)!="OL"'>
                            <xsl:number format="(a)"/>
                        </xsl:when>
                        <xsl:when test='name(../../..)!="OL"'>
                            <xsl:number format="(1)"/>
                        </xsl:when>
                        <xsl:when test='name(../../../..)!="OL"'>
                            <xsl:number format="(i)"/>
                        </xsl:when>
                        <xsl:when test='name(../../../../..)!="OL"'>
                            <xsl:number format="(A)"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>&#xb7;</xsl:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </fo:block>
            </fo:list-item-label>
            <fo:list-item-body start-indent="body-start()">
                <fo:block>
                    <xsl:apply-templates/>
                </fo:block>
            </fo:list-item-body>
        </fo:list-item>
    </xsl:template-->
    
    <xsl:template match="HR|hr">
        <fo:block text-align="justify">
            <fo:leader leader-pattern="rule">
                <xsl:apply-templates select="@*"/>
            </fo:leader>
        </fo:block>
    </xsl:template>
    <xsl:template match="BR|br">
        <xsl:text>
</xsl:text>
    </xsl:template>
    <xsl:template match="STRONG|strong|B|b">
        <fo:inline font-weight="bold">
            <xsl:apply-templates/>
        </fo:inline>
    </xsl:template>
    <xsl:template match="EM|I|i">
        <fo:inline font-style="italic">
            <xsl:apply-templates/>
        </fo:inline>
    </xsl:template>
    <xsl:template match="U|u">
        <fo:inline text-decoration="underline">
            <xsl:apply-templates/>
        </fo:inline>
    </xsl:template>

    <!-- attributes -->
    <xsl:template match="@align">
        <xsl:attribute name="text-align"><xsl:value-of select="."/></xsl:attribute>
    </xsl:template>

    <xsl:template match="@*">
</xsl:template>
</xsl:stylesheet>

Geen opmerkingen:

Een reactie posten