从插入XML图像XSL文档(Inserting images from XML to XSL doc

2019-08-03 17:39发布

我想知道是否有任何方式我可以使用的元素或属性声明在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图像是由一个显示在表中的一个。

Answer 1:

这是不准确的解决方案,但我展示你如何使用,每个拷贝不同的标志!

 <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存在或不存在。 我假设它会永远在那里。你需要有一个检查,如果countryflag是创建img标签,否则它可能会创建一个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>


Answer 2:

你应该提供一个选择属性的应用模板元素。 丢弃也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>



文章来源: Inserting images from XML to XSL document