4

XSLT スタイルシートのデザイン パターンに、一般的なデータ表現と特定のデータ表現を分離するための適切なアプローチがあるかどうか疑問に思っていました。

私はしようとしていましたが、非常に混乱して迷子になりました。XSLT スタイルシートをより適切に分離する方法について読むことができるアドバイス、ヒント、ヒントをいただければ幸いです。また、以下の例は機能しないため、本当に助けていただければ幸いです =/ ありがとうございます!

一部のデータを再利用するさまざまな外観のさまざまな HTML ドキュメントを作成する必要があります。たとえば、ドキュメントの日付、署名の詳細 (名前、役職) などです。また、私は非常に多くのグローバル変数を使用しています (XML は適切に構造化されておらず、ドキュメント全体でデータが再利用されているため)。

私がやろうとしていたのは、共通の HTML 構造を作成するすべてのテンプレートを 1 つのスタイルシートに移動し、すべての特定のビットを独自のスタイルシートに移動することです。

次のようなもの:

共通テンプレート スタイルシート「commonTemplates.xsl」

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Variables Local -->
...

<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" 
    doctype-system="http://www.w3.org/TR/html4/loose.dtd" indent="yes" />

<xsl:template match="/Bookings">
    <html>
        <head>
            <!-- populated by a template in a specific stylesheet -->
            <title><xsl:call-template name="docTitle"/></title>
        </head>
        <body>
            <xsl:apply-templates />
        </body>
    </html>
</xsl:template>

<!-- general template for date -->
<xsl:template match="/Bookings/Booking" name="docDate">
  <p class="date"><xsl:value-of select="./@Date"/></p>
</xsl:template>

<!-- general template for signature -->
<xsl:template match="/Bookings/Booking/Users" name="signature">
    <xsl:param name="signatureLine" select="'Yours sincerely,'"/>
    <div id="signature">
        <p><xsl:value-of select="$signatureLine"/></p>
        <p class="details">
            <!-- populated by a template in a specific stylesheet -->
            <xsl:apply-templates select="." mode="signature"/>
        </p>
    </div>
</xsl:template>

<!-- dummy templates signatures otherwise it complains that there is no such template -->
<xsl:template name="docTitle"/>    
</xsl:stylesheet>

特定のテンプレート スタイルシート:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<!-- Imports -->
<xsl:import href="commonTemplates.xsl"/>

<!-- BODY CONTENT OF HTML PAGE -->
<xsl:template match="/Bookings/Booking">
    <xsl:call-template name="docDate"/>
    <!-- document's content -->
    <div>
        <xsl:call-template name="content" />
    </div>
</xsl:template>


<xsl:template name="docTitle">
    <xsl:text>Here is the document title</xsl:text>
</xsl:template>

<!-- some content at the end of which signature should be inserted -->
<xsl:template name="content">
    <p>SOME CONTENT</p>        
    <xsl:apply-templates />
</xsl:template>

<!-- specific rule to insert appropriate data for signature -->
<xsl:template match="/Bookings/Booking/Users" mode="signature">
    <span class="name"><xsl:value-of select="./@Name"/></span>
    <span class="job"><xsl:value-of select="./@Title"/></span>
</xsl:template>
</xsl:stylesheet>

残念ながら、署名のテンプレートは機能せず、その理由がわかりません :( ただし、docTitle では機能します。

結果の HTML は次のようになります。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title></title>
   </head>
   <body>      
      <p class="date">16 February 2010</p>
      <div id="secondDeposit">
         <p>SOME CONTENT</p>
         <!-- here I get lots of empty space -->
      </div>    
   </body>

そのようなアイデアが一般的に実装できるかどうか、そしてそれを適切に行う方法を考えていましたが、明らかに私のものは機能しません。

また、この場合、スタイルシートをインクルードまたはインポートするのにどちらのアプローチが適していますか? それらの1つを使用すると、すべての変数を再度リストする必要はないと思います。

助けていただければ幸いです!長い投稿で申し訳ありませんが、明確でない場合は.

ありがとうございました!

4

2 に答える 2

3

私は次の方法を使用します。

テンプレート ファイル(/templates/Main.xslt)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="utf-8" />
  <xsl:include href="../functions/General.xslt"/>
  <xsl:template match="/">
    <html xml:lang="en">
      <head>
      </head>
      <body>
         Template Content
         <xsl:call-template name="PageContent" />
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

関数ファイル(/functions/General.xslt)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Generic functions used in the site go in here -->
</xsl:stylesheet>

ページファイル(/default.xslt)

テンプレート内でさまざまなレイアウトを定義するために、これらのいくつかがあります。これは、変換を行うときに呼び出す XSLT ファイルです。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="templates/Main.xslt" />
    <xsl:output method="xml" encoding="utf-8" />
    <xsl:template match="/">
        <xsl:apply-imports />
    </xsl:template>
    <!-- CONTENT -->
    <xsl:template name="PageContent">
      <!-- Put your specific page stuff here -->
    </xsl:template>
</xsl:stylesheet>
于 2010-02-17T17:25:06.393 に答える