You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSLT1在关联XML数据库中定位匹配的节点文本

解决方案

你需要的双语内容并排展示效果可通过「遍历英文库的W开头周节点 + 按节点名匹配旁遮普库对应节点」的逻辑实现,完全符合XSLT 1.0的语法要求,修改后的完整脚本如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msa="http://www.publictalksoftware.co.uk/msa">
    <xsl:output method="html" indent="yes" version="4.01"
      doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
      doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>

    <xsl:variable name="EnglishDB" select="document('MWBData_ENG.XML')"/>
    <xsl:variable name="PunjabiDB" select="document('MWBData_PAN.XML')"/>
    <xsl:template match="/">
        <html>
            <head>
                <meta http-equiv="X-UA-Compatible" content="IE=edge" />
                <title>Terminology List</title>
                <style>
                    table, td, th { border: 1px solid #ccc; border-collapse: collapse; padding: 8px; }
                </style>
            </head>
            <body>
                <table>
                    <!-- 可选表头 -->
                    <tr>
                        <th>周标识</th>
                        <th>英文主题</th>
                        <th>旁遮普语主题</th>
                    </tr>
                    <!-- 仅遍历英文库中所有W开头的周节点 -->
                    <xsl:apply-templates select ="$EnglishDB/MeetingWorkBook/*[starts-with(name(), 'W')]"/>
                </table>
            </body>
        </html>
    </xsl:template>
    
    <!-- 匹配W开头周节点的模板 -->
    <xsl:template match="*[starts-with(name(), 'W')]">
        <xsl:variable name="current_week_name" select="name()"/>
        <!-- 按节点名定位旁遮普库中对应周的主题内容 -->
        <xsl:variable name="punjabi_theme" select="$PunjabiDB/MeetingWorkBook/*[name() = $current_week_name]/WatchtowerStudyTheme"/>
        <tr>
            <td>
                <xsl:value-of select="$current_week_name"/>
            </td>
            <td>
                <xsl:value-of select="WatchtowerStudyTheme"/>
            </td>
            <td>
                <!-- 对应周无旁遮普语数据时显示空值,也可自定义提示内容 -->
                <xsl:value-of select="$punjabi_theme"/>
            </td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

关键逻辑说明

  • 使用starts-with(name(), 'W')筛选节点,避免误处理MeetingWorkBook下其他非周节点
  • 通过*[name() = $current_week_name]条件筛选,直接在旁遮普语库中定位到同名的周节点,不需要硬编码节点名称
  • 如果需要兼顾处理旁遮普语库存在但英文库不存在的周节点,可额外增加一次旁遮普语库的遍历,做去重处理即可

内容的提问来源于stack exchange,提问作者Andrew Truckle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 02:24:00