将图像从XML插入XSL文档 [英] Inserting images from XML to XSL document

查看:108
本文介绍了将图像从XML插入XSL文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有什么方法可以使用元素或属性在XML文件中声明图像,然后在XSL文件中使用该图像将其输入到表中,而不必在XSL和一张一张地将图像输入到表格单元格中.这是我当前的XML文档(由于我正在测试中,所以不完整).

I was wondering if there is any way I can declare an image in the XML file using an element or attribute and then use this image in the XSL file to input it into a table instead of having to create the table in XSL and inputting the images into the table cells one by one. Here is my current XML document(incomplete as I am just testing it out).

<?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文件以及尝试使用的方法:

Here is the XSL file i created and the method i tried using:

<?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图像都一张一张地显示在表中.

As you can see I have created a table and want the XSL to get the image from the XML file and then have it so every countryflag image is displayed in the table one by one.

推荐答案

这不是确切的解决方案,但我向您演示如何使用for-each复制不同的标志!

This is not exact solution but I am demonstrating you how to use for-each to copy different flags!

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

这将在不同行中复制不同国家/地区标志的值!

This will copy value of different countryflag in different rows !

Ps:这只是示例代码!我没有检查countryflag是否存在.我假设它将一直在那里..在创建img标签之前,您需要检查countryflag是否在更安全的一端存在/为空,否则可能会创建src为null的img标签.

Ps: This is just a sample code! I am not checking if countryflag is present or not. I am assuming it will always be there.. You need to have a check if countryflag is present/null on safer end before creating img tag otherwise it might create img tag with src as null..

编辑:更简单的解决方案,没有<xsl:element>标记..

simpler solution with no <xsl:element> tag ..

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

这篇关于将图像从XML插入XSL文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆