如何使用XSLT使用电子表格将alt文本添加到图像文件中



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

这是我的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,并在alt=""中的引号之间添加C列中的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>

这样做可能吗?

以下是实现这一目标的方法:

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

或者更有效地使用密钥:

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