XSLサブテンプレートの使用例
これらは、XSLサブテンプレートを使用してレポートで実現できる書式設定の例です。
HTMLフォーマットによるXMLデータの処理
HTML書式がすでに含まれているXMLデータがあり、その書式をレポートに保持する場合は、XSLサブテンプレートを使用してHTML書式コマンドを、「パブリッシャ」で処理できるXSL同等の書式にマップすることで、その書式を保持できます。
HTMLはXHTML形式である必要があります。 つまり、すべてのHTMLタグには、データ内に開始タグと終了タグが必要です。 たとえば、データが休憩に単純な<BR>
を使用する場合、この解答を使用する前に終了</BR>
を追加する必要があります。
次に、HTML形式のサンプル・データをいくつか示します:
<DATA>
<ROW>
<PROJECT_NAME>Project Management</PROJECT_NAME>
<PROJECT_SCOPE>
<p>Develop an application to produce <i>executive-level summaries</i> and detailed project reports. The application will allow users to: </p>
<p>Import existing MS Project files </p>
<p>Allow the user to map file-specific resources to a central database entities (i.e., people) and projects; </p>
<p>Provide structured output that can be viewed by staff and executives. </p>
</PROJECT_SCOPE>
<PROJECT_DEFINITION><b>Information about current projects is not readily available to executives.</b> Providing this information creates a reporting burden for IT staff, who may already maintain this information in Microsoft Project files. </PROJECT_DEFINITION>
</ROW>
</DATA>
次の図に示すように、これらのタグによって提供される書式設定を保持するために、これを表示するレポート要件を想定します:
次のサブテンプレートは、XSL構文を使用して、XMLデータの3つのHTMLタグと一致します。 次に、テンプレートは、一致したHTML文字列をそのXSLFOに相当するものに置き換えます。
<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="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構文を使用するには:
データ部分への書式設定の動的適用
このサブテンプレートの適用は、化学式、数学的計算、または上付きおよび下付きを必要とするドキュメントに役立ちます。
たとえば、次のサンプルXMLデータでは、CO2はCO2として表示され、H2OはH2 Oとして表示される必要があります。
<ROWSET>
<ROW>
<FORMULA>CO2</FORMULA>
</ROW>
<ROW>
<FORMULA>H2O</FORMULA>
</ROW>
</ROWSET>
これを行うには、XSLサブテンプレートを使用します。 XSL構文を使用すると、パラメータとしてFORMULAフィールドを受け入れ、一度に1文字を読み取る"chemical_formatter"など、任意の名前でテンプレートを定義できます。 文字を0と比較 - 9桁で、一致がある場合、その文字は次のXSL FO構文を使用して添字になります:
<fo:inline baseline-shift="sub" font-size="75%">
XSLテンプレート文のサンプル・コードを次に示します:
<xsl:template name="chemical_formatter">
<! - accepts a parameter e.g. H2O - >
<xsl:param name="formula"/>
<! - Takes the first character of the string and tests it to see if it is a number between 0-9 - > <xsl:variable name="each_char"
select="substring($formula,1,1)"/>
<xsl:choose>
<xsl:when test="$each_char='1' or $each_char='2'
or $each_char='3' or $each_char='4' or $each_char='5'
or $each_char='6' or $each_char='7' or $each_char='8'
or $each_char='9' or $each_char='0'">
<! - if it is numeric it sets the FO subscripting properties - >
<fo:inline baseline-shift="sub" font-size="75%">
<xsl:value-of select="$each_char"/>
</fo:inline>
</xsl:when>
<xsl:otherwise>
<! - otherwise the charater is left as is - >
<fo:inline baseline-shift="normal">
<xsl:value-of select="$each_char"/>
</fo:inline>
</xsl:otherwise>
</xsl:choose>
<! - test if there are other chars in the string, if so the recall the template - >
<xsl:if test="substring-after($formula,$each_char) !=''">
<xsl:call-template name="chemical_formater">
<xsl:with-param name="formula">
<xsl:value-of select="substring-after($formula,$each_char)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
これにより、FORMULA値H2Oの書式設定テンプレートがコールされます。 レンダリングされると、式が期待どおりに表示されます: H2 O.