首页 > 解决方案 > 如何编写 XSLT 脚本以将电子表格中的数据导入 HTML 文件?

问题描述

我需要创建一个 XSLT 脚本来将电子表格中的内容导入 HTML 文件。

我的电子表格有 3 列需要导入。

在此处输入图像描述

我将以这种格式将电子表格数据导出为 HTML:

<table class="tableizer-table">
            <thead>
                <tr class="tableizer-firstrow">
                    <th>CARD ID</th>
                    <th>PROMPT</th>
                    <th>RESPONSE</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>3</td>
                    <td>Acquisition</td>
                    <td>In classical conditioning, the process of taking advantage of reflexive
                        responses to turn a neutral stimulus into a conditioned stimulus.</td>
                </tr>
            </tbody>
        </table>

该数据需要添加到此 HTML 页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" designation="" enumeration="" data-uuid="61dac34c7de54289a58698d5bfc8e776">
   <head>
      <meta charset="utf-8"/>
      <link type="text/css" rel="stylesheet" title="default" href="../../assets/css/main.css"/>
      <title>usurpmcatfc0001</title>
   </head>
   <body>
      <section property="ktp:document" typeof="ktp:Document" class="ktp-document">
         <section class="ktp-document-meta">
            <section property="ktp:metadata" class="ktp-meta"><span property="atom:content-item-name" class="ktp-meta" data-value="usurpmcatfc0001"></span></section>
            <section property="ktp:tags" class="ktp-meta"><span property="ktp:topic" class="ktp-meta">MCAT</span><span property="ktp:topic" class="ktp-meta">Behavioral Sciences</span><span property="ktp:subsubtopic" class="ktp-meta">Sensation and Perception</span></section>
         </section>
         <section property="ktp:document-section" typeof="ktp:flashcards" class="ktp-document-section" data-title="Absolute Threshold">
            <p>The minimum of stimulus energy needed to activate a sensory system.</p>
         </section>
      </section>
   </body>
</html>

第 1 列的数据需要输入到本节的数据值中:

<section property="ktp:metadata" class="ktp-meta"><span property="atom:content-item-name" class="ktp-meta" data-value="[COLUMN 1 DATA]"></span></section>

第 2 列的数据需要输入到 data-title 中,第 3 列的数据需要输入到<p></p>本节的标签之间:

<section property="ktp:document-section" typeof="ktp:flashcards" class="ktp-document-section" data-title="Absolute Threshold">
            <p>The minimum of stimulus energy needed to activate a sensory system.</p>
         </section>

任何生成此脚本的帮助将不胜感激。

谢谢!

标签: xslt

解决方案


如果导出的 HTML 可以解析为 XML,您可以使用doceg 函数简单地读取它<xsl:variable name="excel-table" select="doc('exported-table.xml')//table[@class = 'tableizer-table']"/>,当然在列中读取就像 eg 一样简单$excel-table/tbody/tr/td[1]

因此,为您要操作的节点设置模板,例如

<xsl:template match="section[@property = 'ktp:metadata']/span/data-value">
   <xsl:attribute name="{name()}" select="$excel-table/tbody/tr/td[1]"/>
</xsl:template>

当然,基本处理将通过身份转换来完成,例如<xsl:mode on-no-match="shallow-copy"/>在 XSLT 3 中。

唯一的复杂之处似乎是主文档是命名空间中的 XHTML,xmlns="http://www.w3.org/1999/xhtml"因此您需要进行设置,xpath-default-namespace="http://www.w3.org/1999/xhtml"但如果表导出不在命名空间中,则需要使用变量,<xsl:variable name="excel-table" xpath-default-namespace="" select="doc('exported-table.xml')//table[@class = 'tableizer-table']"/>并且文档内的任何选择都需要执行相同的操作,例如<xsl:attribute name="{name()}" xpath-default-namespace="" select="$excel-table/tbody/tr/td[1]"/>.


推荐阅读