4

XSLでテーブルを作成して画像を入力する代わりに、要素または属性を使用してXMLファイルで画像を宣言し、XSLファイルでこの画像を使用してテーブルに入力する方法があるかどうか疑問に思いました。テーブルセルに1つずつ入れます。これが私の現在のXMLドキュメントです(テストしているだけなので不完全です)。

<?xml version= "1.0"?>
<?xml-stylesheet type="text/xsl" href="stylesheet4.xsl"?>
    <countries>
        <country> 
            <countryname>United States</countryname>
            <countryflag>bg_locale.jpg</countryflag>
        </country>

        <country>
            <countryname>United Kingdom</countryname>
        </country>

        <country>
            <countryname>Deutschland</countryname>
        </country>
        </countries>

これが私が作成したXSLファイルと私が使用しようとした方法です:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/countries">
<html>
<body bgcolor="black">

<div id="container" style="100%">

<div id="header" style="background-color:black; height:60px"></div>


<div id="content_container" align="center">
    <div id="content" align="left" style="background: url('bg_locale.jpg');height:845px;width:848px">
    Content goes here
    <img src="logo_timber.jpg" align="left"/><br/>
<br/>
<br/>
<br/>
<table border="1">

<tr>
<xsl:apply-templates/>
</tr>

</table>
</div>

</div>

</div>
</body>
</html>

</xsl:template>

<xsl:template match="country">
<tr><td><xsl:value-of select="countryflag"/></td></tr>
</xsl:template>

</xsl:stylesheet>

ご覧のとおり、テーブルを作成し、XSLでXMLファイルから画像を取得して、すべての国のフラグ画像が1つずつテーブルに表示されるようにします。

4

2 に答える 2

5

これは正確な解決策ではありませんが、for-eachを使用してさまざまなフラグをコピーする方法を示しています。

 <xsl:template match="/countries">
    <table>
      <xsl:for-each select="country">
        <tr>
          <td>
            <xsl:value-of select="countryname"/>
          </td>
          <td>
            <xsl:element name="img">
              <xsl:attribute name="src">
                <xsl:value-of select="countryflag"/>
              </xsl:attribute>
              <xsl:attribute name="align">left</xsl:attribute>
            </xsl:element>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

これにより、異なる国のフラグの値が異なる行にコピーされます。

追伸:これは単なるサンプルコードです!カントリーフラッグが存在するかどうかはチェックしていません。常に存在すると想定しています。imgタグを作成する前に、安全側にカントリーフラグが存在するかnullであるかどうかを確認する必要があります。そうしないと、srcがnullのimgタグが作成される可能性があります。

編集:<xsl:element>タグのないより簡単な解決策..

 <xsl:template match="/countries">
    <table>
      <xsl:for-each select="country">
        <tr>
          <td>
            <xsl:value-of select="countryname"/>
          </td>
          <td>
            <img src="{countryflag}" align="left"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
于 2012-12-08T18:35:36.753 に答える
3

apply-templates要素にselect属性を指定する必要があります。それを囲むtrタグも破棄します。

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:template match="/countries">
    <html>
    <head></head>
        <body bgcolor="black">
            <div id="container" style="100%">
                <div id="header" style="background-color:black; height:60px"></div>
                <div id="content_container" align="center">
                    <div id="content" align="left" style="background: url('bg_locale.jpg');height:845px;width:848px">
                    Content goes here
                    <img src="logo_timber.jpg" align="left"/><br/>
                    <br/>
                    <br/>
                    <br/>
                    <table border="1">
                        <xsl:apply-templates select="country" />
                    </table>
                    </div>
                </div>
            </div>
        </body>
    </html>
</xsl:template>

<xsl:template match="country">
    <tr>
        <td>
            <xsl:value-of select="countryflag"/>
        </td>
    </tr>
</xsl:template>

于 2012-12-09T02:23:35.227 に答える