xslt - 如何使用 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>
那有可能吗?
解决方案
这是一种可以做到的方法:
<?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
推荐阅读
- docker - 在 docker build 期间,add-apt-repository 返回“keyserver receive failed: No name”
- mongodb - PyMongo:如何使用聚合更新集合?
- c# - 如何在 C# 中远程高效地访问服务?异常处理非常慢
- rest - Rest API - 放置或补丁
- go - 如何获取 Go 依赖的列表
- c++ - 为什么这里不调用 std::string 的移动构造函数?
- ios - 在聚类/去聚类时保持选择地图注释
- python - AWS Chalice - 将 app.py 拆分为多个文件
- php - oci_execute(): ORA-01400: 无法插入 NULL
- java - AlertDialog上的Android onClickListner不起作用