SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

特集記事

XSLTを使用したメールテンプレートの作り方

定型的なメールをXSLTでレイアウトする

XSLTファイルの生成

 それではXSLTファイルの中身を見てみましょう。

sample01.xsl
<?xml version="1.0" encoding="Shift-JIS"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="Shift-JIS" />
  <xsl:template match="/">
    <xsl:apply-templates select="data" />
  </xsl:template>

  <xsl:template match="data">
    <xsl:apply-templates select="header" />
    <xsl:apply-templates select="detail">
      <xsl:sort select="DATE" order="ascending"/>
    </xsl:apply-templates>
    <xsl:apply-templates select="footer" />
  </xsl:template>

  <xsl:template match="header">ご利用ありがとうございます。
    <xsl:text>&#x0A;</xsl:text>
    <xsl:value-of select="TANTOU_NAME"/><xsl:text>&#x0A;</xsl:text>
    売上日付 商品 数量 単価 金額
    <xsl:text>&#x0A;&#x0A;</xsl:text>
  </xsl:template>
  <xsl:template match="footer">
    <xsl:text>&#x0A;</xsl:text>お問い合わせ先
    <xsl:text>&#x0A;</xsl:text>
    <xsl:value-of select="MAIL"/><xsl:text>&#x0A;</xsl:text>
  </xsl:template>

  <xsl:template match="detail">
    <xsl:value-of select="DATE"/>
    <xsl:value-of select="ITEM"/>
    <xsl:value-of select="QUANTITY"/><xsl:text> </xsl:text>
    <xsl:value-of select="PRICE"/><xsl:text> </xsl:text>
    <xsl:value-of select="MONEY"/><xsl:text>&#x0A;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

XSLTソースコードの解説

出力形式の指定

 出力形式をテキスト型で宣言します。

<xsl:output method="text" encoding="Shift-JIS" />

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

大まかな枠組みの指定

 headerタグ、detailタグ、footerタグの順に表示しています。detailタグの内容は日付で昇順を行い表示しています。

<xsl:template match="data">
  <xsl:apply-templates select="header" />
  <xsl:apply-templates select="detail">
    <xsl:sort select="DATE" order="ascending"/>
  </xsl:apply-templates>
  <xsl:apply-templates select="footer" />
</xsl:template>

レイアウトの指定

 各タグ内の動的なタグのレイアウトを行っています。

<xsl:template match="header">ご利用ありがとうございます。
  <xsl:text>&#x0A;</xsl:text>
  <xsl:value-of select="TANTOU_NAME"/><xsl:text>&#x0A;</xsl:text>
  売上日付 商品 数量 単価 金額<xsl:text>&#x0A;&#x0A;</xsl:text>
</xsl:template>
<xsl:template match="footer">
  <xsl:text>&#x0A;</xsl:text>お問い合わせ先<xsl:text>&#x0A;</xsl:text>
  <xsl:value-of select="MAIL"/><xsl:text>&#x0A;</xsl:text>
</xsl:template>

<xsl:template match="detail">
  <xsl:value-of select="DATE"/>
  <xsl:value-of select="ITEM"/>
  <xsl:value-of select="QUANTITY"/><xsl:text> </xsl:text>
  <xsl:value-of select="PRICE"/><xsl:text> </xsl:text>
  <xsl:value-of select="MONEY"/><xsl:text>&#x0A;</xsl:text>
</xsl:template>
</xsl:stylesheet>

出力結果

 このXSLTファイルを使用すると、下記のようになります。

ご利用ありがとうございます。

○×様

売上日付 商品 数量 単価 金額
5月22日 バナナ 100 200 20,000
5月23日 みかん 120 50 6,000
5月23日 バナナ 100 100 10,000

お問い合わせ先
hoge@sample.com

 ちなみに

<xsl:sort select="DATE" order="ascending"/>

 の部分を

<xsl:sort select="ITEM" order="ascending"/>

 に変更すると

5月22日  バナナ  100 200 20,000
5月23日  バナナ  100 100 10,000
5月23日  みかん  120 50 6,000

 のようになります。

まとめ

 XSLTはレイアウトを柔軟に変更することができます。

 ソートはもちろん、1つ上のレコードと同じ場合は非表示にするなども簡単ですし、HTMLメールも作成できます。また、取得するデータが変わらなければ(生成するXMLデータに含まれてさえいれば)プログラムの変更さえ必要ありません。

 XSLTを覚える必要はありますが、柔軟なレイアウトが望まれる場合に有効な手段の1つになります。

この記事は参考になりましたか?

連載通知を行うには会員登録(無料)が必要です。
既に会員の方はを行ってください。
特集記事連載記事一覧

もっと読む

この記事の著者

kox(ケイオーエックス)

業務系保守担当

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/1430 2007/07/05 14:00

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー