簡體   English   中英

將圖像從XML插入XSL文檔

[英]Inserting images from XML to XSL document

我想知道是否有任何方法可以使用元素或屬性在XML文件中聲明圖像,然后在XSL文件中使用此圖像將其輸入到表中,而不必在XSL中創建表並輸入圖像逐個進入表格單元格。 這是我當前的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文件中獲取圖像,然后將其放置在表中,以便逐個顯示每個countryflag圖像。

這不是確切的解決方案,但我向您展示如何使用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>

這將在不同的行中復制不同countryflag的值!

Ps:這只是一個示例代碼! 我沒有檢查是否存在countryflag。 我假設它將永遠存在..你需要在創建img標簽之前檢查countryflag是否存在/ null在更安全的結尾,否則它可能會創建帶有src的img標記為null。

編輯:更簡單的解決方案,沒有<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>

您應該為apply-templates元素提供select屬性。 丟棄包含它的tr-tags:

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM