首页 > 解决方案 > 如何使用 XSLT 将替代文本添加到使用电子表格的图像文件中?

问题描述

我需要编写一个 XSLT 脚本,其中使用电子表格(我将其转换为 HTML)它在 B 列中查找图像文件名,然后从 C 列添加替代文本。

这是我的 HTML 代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" designation="" enumeration="">
    <head>
        <meta charset="utf-8" />
        <link type="text/css" rel="stylesheet" title="default" href="../../assets/css/main.css" />
        <title>lsac790101</title>
    </head>
    <body>
        <ol class="ktp-question-set">
            <li property="ktp:question" typeof="ktp:Question" class="ktp-question">
                <ol class="ktp-answer-set" data-studentresponses="172">              
                    <li property="ktp:answer" typeof="ktp:Answer" data-percentresponse="2">
                        <img src="../../img/chapterpreptest79/lsac790101-a.gif" data-graphic-ref="lsac790101-a.gif" alt="" />                      
                    </li>
                </ol>
            </li>
        </ol>
    </body>
</html>

因此,在这种情况下,我需要脚本在电子表格的 B 列中找到 lsac790101-a.gif,并将 C 列中的 alt 文本添加到alt="".

我需要它看起来像这样:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" designation="" enumeration="">
    <head>
        <meta charset="utf-8" />
        <link type="text/css" rel="stylesheet" title="default" href="../../assets/css/main.css" />
        <title>lsac790101</title>
    </head>
    <body>
        <ol class="ktp-question-set">
            <li property="ktp:question" typeof="ktp:Question" class="ktp-question">
                <ol class="ktp-answer-set" data-studentresponses="172">              
                    <li property="ktp:answer" typeof="ktp:Answer" data-percentresponse="2">
                        <img src="../../img/chapterpreptest79/lsac790101-a.gif" data-graphic-ref="lsac790101-a.gif" alt="Nottingham, Lakeville, Oldtown, Hidden Hills, and Sunnyside" />                      
                    </li>
                </ol>
            </li>
        </ol>
    </body>
</html>

这是我转换为 HTML 的电子表格文件:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <table class="tableizer-table">
            <thead>
                <tr class="tableizer-firstrow">
                    <th>contentItemName</th>
                    <th>image name</th>
                    <th>Alt Text</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>lsac790101</td>
                    <td>lsac790101-a.gif</td>
                    <td>Nottingham, Lakeville, Oldtown, Hidden Hills, and Sunnyside</td>
                </tr>
            </tbody>
        </table></body>
</html>

那有可能吗?

标签: xsltspreadsheet

解决方案


这是一种可以做到的方法:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    version="3.0">

  <xsl:output method="html" indent="yes"/>
  
  <xsl:variable name="spreadsheet" select="document('spreadsheet.xml')"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

    <xsl:template match="xhtml:img/@alt">
        <xsl:variable name="imgName" select="../@data-graphic-ref"/>
        <xsl:attribute name="alt" select="$spreadsheet//xhtml:td[.=$imgName]/following-sibling::xhtml:td[1]"/>
    </xsl:template>
  
</xsl:stylesheet>

看到它在这里工作:https ://xsltfiddle.liberty-development.net/6q1SDkx/2

或者更有效地使用 key :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    version="3.0">

  <xsl:output method="html" indent="yes"/>
  
  <xsl:variable name="spreadsheet" select="document('spreadsheet.xml')"/>
  
  <xsl:key name="imgTD" match="xhtml:td" use="."/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

    <xsl:template match="xhtml:img/@alt">
        <xsl:variable name="imgName" select="../@data-graphic-ref"/>
        <xsl:attribute name="alt" select="key('imgTD',$imgName,$spreadsheet)/following-sibling::xhtml:td[1]"/>
    </xsl:template>
  
</xsl:stylesheet>

看到它在这里工作:https ://xsltfiddle.liberty-development.net/6q1SDkx/4


推荐阅读