こちらのファイルを テキストエディタ(秀丸やメモ帳など)上で作成し、 building4-1.xmlという名前で保存しましょう。 これは、「要素、属性」のページで 作成したbuilding3-1.xmlに、画像の要素を増やしたものです。
そして、XSLTスタイルシートを作成します。building4-1.xslとい うファイル名で保存してください。 このファイルは、building3-3.xslに手を加えて作成しました。 このように見えるはずです。
<?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/" > <html> <head> <title> <xsl:value-of select="buildings/@title" /> </title> </head> <h1><xsl:value-of select="buildings/@title" /></h1> <table border="1"> <tr> <th>No.</th> <th>名前</th> <th>階数</th> <th>特徴</th> <th>画像</th> </tr> <xsl:apply-templates select="buildings" /> </table> </html> </xsl:template> <xsl:template match="buildings"> <xsl:for-each select="building"> <tr> <td><xsl:value-of select="@id" /></td> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="floor" /></td> <td><xsl:value-of select="comment" /></td> <td> <xsl:for-each select="image"> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="url" /> </xsl:attribute> <xsl:element name="img"> <xsl:attribute name="src"> <xsl:value-of select="thumnail_url" /> </xsl:attribute> <xsl:attribute name="width">60</xsl:attribute> <xsl:attribute name="height">45</xsl:attribute> </xsl:element> </xsl:element> </xsl:for-each> </td> </tr> </xsl:for-each> </xsl:template> </xsl:stylesheet>
上記の記述方法でもいいのですが、もう一つtemplateを使用してすっきりと 書き直してみたのが下記のものです。どちらでもいいのですが、本質的に 同じだということを理解してください。
<?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/" > <html> <head> <title> <xsl:value-of select="buildings/@title" /> </title> </head> <h1><xsl:value-of select="buildings/@title" /></h1> <table border="1"> <tr> <th>No.</th> <th>名前</th> <th>階数</th> <th>特徴</th> <th>画像</th> </tr> <xsl:apply-templates select="buildings" /> </table> </html> </xsl:template> <xsl:template match="buildings"> <xsl:for-each select="building"> <tr> <td><xsl:value-of select="@id" /></td> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="floor" /></td> <td><xsl:value-of select="comment" /></td> <td><xsl:apply-templates select="image" /></td> </tr> </xsl:for-each> </xsl:template> <xsl:template match="image"> <xsl:for-each select="."> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="url" /> </xsl:attribute> <xsl:element name="img"> <xsl:attribute name="src"> <xsl:value-of select="thumnail_url" /> </xsl:attribute> <xsl:attribute name="width">60</xsl:attribute> <xsl:attribute name="height">45</xsl:attribute> </xsl:element> </xsl:element> </xsl:for-each> </xsl:template> </xsl:stylesheet>
XSLTにおける条件分岐の方法には、 <xsl:if>〜</xsl:if>もあるのですが、これは真の場合の処理しか なく、偽の場合の処理がありません。偽の場合の処理にも対応したものとして、 下記の<xsl:choose>〜</xsl:choose>が使われ ます。
その使い方は下の通りです。
<xsl:choose> <xsl:when test="条件式"> (真の場合の処理) </xsl:when> <xsl:otherwise> (偽の場合の処理) </xsl:otherwise> </xsl:choose>
それでは、階数が2階以上なら背景を黄色にして目立つように表示する、という 状況を想定してください。building4-1.xslを元に、下に示された赤い行を追記して、 building4-2.xslというファイルを作ってください。 また、building4-1.xmlをコピーして、スタイルシートの指定先を変更した上で、 building4-2.xmlという名前で保存しましょう。 このように見えるはずです。
<?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/" > <html> <head> <title> <xsl:value-of select="buildings/@title" /> </title> </head> <h1><xsl:value-of select="buildings/@title" /></h1> <table border="1"> <tr> <th>No.</th> <th>名前</th> <th>階数</th> <th>特徴</th> <th>画像</th> </tr> <xsl:apply-templates select="buildings" /> </table> </html> </xsl:template> <xsl:template match="buildings"> <xsl:for-each select="building"> <xsl:variable name="floornum" select="floor" /> <xsl:choose> <xsl:when test="$floornum > 1"> <tr bgcolor="yellow" bordercolor="red"> <td><xsl:value-of select="@id" /></td> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="floor" /></td> <td><xsl:value-of select="comment" /></td> <td><xsl:apply-templates select="image" /></td> </tr> </xsl:when> <xsl:otherwise> <tr> <td><xsl:value-of select="@id" /></td> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="floor" /></td> <td><xsl:value-of select="comment" /></td> <td><xsl:apply-templates select="image" /></td> </tr> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> <xsl:template match="image"> <xsl:for-each select="."> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="url" /> </xsl:attribute> <xsl:element name="img"> <xsl:attribute name="src"> <xsl:value-of select="thumnail_url" /> </xsl:attribute> <xsl:attribute name="width">60</xsl:attribute> <xsl:attribute name="height">45</xsl:attribute> </xsl:element> </xsl:element> </xsl:for-each> </xsl:template> </xsl:stylesheet>
今度は、特定の文字列が含まれる要素のみを目立つように色を変えて表示させた い。文字列の検索には、下記のcontains関数を使います。
「検索対象」には、要素ノードや属性ノードを指定 します。「検索対象」に「検索したい文字列」が含まれていれば、trueを返す関数 です。
このcontains関数を使って、「建物名」に「グラウンド」が含まれる要素を 抽出してみましょう。 下記のXSLTスタイルシートを「building4-4.xsl」として 作成しましょう。また、そのXMLファイルは、 building4-1.xmlをそのままコピーし、スタイルシートの指定先だけ変更して、 building4-4.xmlというファイル名で保存しましょう。 こちらのページのように表示される はずです。
<?xml version="1.0" encoding="Shift_JIS"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/" > <html> <head> <title> <xsl:value-of select="buildings/@title" /> </title> </head> <h1><xsl:value-of select="buildings/@title" /></h1> <table border="1"> <tr> <th>No.</th> <th>名前</th> <th>階数</th> <th>特徴</th> <th>画像</th> </tr> <xsl:apply-templates select="buildings" /> </table> </html> </xsl:template> <xsl:template match="buildings"> <xsl:for-each select="building"> <xsl:choose> <xsl:when test="contains(name, 'グラウンド')"> <tr bgcolor="yellow" bordercolor="red"> <td><xsl:value-of select="@id" /></td> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="floor" /></td> <td><xsl:value-of select="comment" /></td> <td><xsl:apply-templates select="image" /></td> </tr> </xsl:when> <xsl:otherwise> <tr> <td><xsl:value-of select="@id" /></td> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="floor" /></td> <td><xsl:value-of select="comment" /></td> <td><xsl:apply-templates select="image" /></td> </tr> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> <xsl:template match="image"> <xsl:for-each select="."> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="url" /> </xsl:attribute> <xsl:element name="img"> <xsl:attribute name="src"> <xsl:value-of select="thumnail_url" /> </xsl:attribute> <xsl:attribute name="width">60</xsl:attribute> <xsl:attribute name="height">45</xsl:attribute> </xsl:element> </xsl:element> </xsl:for-each> </xsl:template> </xsl:stylesheet>
上記の問題3「条件分岐」において、表示結果にソートをかけるよう XSLTスタイルシートの設定を変更したい。
上記の要求を満たすXSLTスタイルシートを「building4-3.xsl」として作成せよ。そのXMLファイルは、 building4-1.xmlをそのままコピーし、スタイルシートの指定先だけ変更して、 building4-3.xmlというファイル名で保存せよ。 こちらのページのように表示される はずである。
上記の問題4「文字列の検索」において、要素building以下のどの要素でも「情報」という 文字列を含むものを抽出し、目立つ色で表示させるようXSLTスタイルシートの 設定を変更したい。
上記の要求を満たすXSLTスタイルシートを「building4-5.xsl」として作成せよ。そのXMLファイルは、 building4-1.xmlをそのままコピーし、スタイルシートの指定先だけ変更して、 building4-5.xmlというファイル名で保存せよ。 こちらのページのように表示される はずである。