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

如何在XSLT中引入外部HTML片段并替换href相对路径?

问题:复用外部HTML导航栏并动态替换路径

需要复用一个外部HTML导航栏片段(同时供Python脚本生成HTML使用),通过XSLT将其插入输出文档,并且要把片段里href属性的路径替换为XSLT变量中的相对路径(原路径已改为{replace-me}/sdk/...形式)。


待复用的导航栏模板文件

<ul class="nav">
    <li class="fineprint">MyNiceGame Developer Mode Documentation</li>
    <li class="switchlang"><a href="../en/content.html"><img src="/deco/dco_en_sml.gif" alt="English" border="0"></img></a></li>
    <li><a href="../../sdk/index.html">Introduction</a></li>
    <li><a href="../../content.html">Contents</a></li>
    <li><a href="../../search.php">Search</a></li>
    <li><a href="../../sdk/console.html">Engine</a></li>
    <li><a href="../../sdk/cmdline.html">Command Line</a></li>
    <li><a href="../../sdk/files.html">Game Data</a></li>
    <li><a href="../../sdk/script/index.html">Script</a></li>
</ul>

原XSLT文档(需替换nav模板实现)

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="3.0" xpath-default-namespace="https://clonkspot.org" exclude-result-prefixes="xs">
    <xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.01//EN"
                doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>

    <xsl:template match="/clonkDoc">
        <html>
            <body>
                <xsl:call-template name="nav"/>
                <xsl:apply-templates select="func"/>
                <!-- other possible nodes under /clonkDoc -->
                <xsl:call-template name="nav"/>
            </body>
        </html>
    </xsl:template>

<xsl:template name="nav">
        <xsl:param name="relpath" tunnel="yes"/>
            <ul class="nav">
                <li class="fineprint">
                    <xsl:when test='lang("en")'>>MyNiceGame Developer Mode Documentation</xsl:when>
                </li>
                <!-- Other li elements -->
    </xsl:template>

示例源XML文件

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<clonkDoc xmlns="https://clonkspot.org"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="https://clonkspot.org ../../../clonk.xsd" xml:lang="de">
    <func>
        <!-- other nodes -->
    </func>
</clonkDoc>

期望输出的HTML文件

<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
   <!-- stuff -->
   </head>
   <body>
      <ul class="nav">
         <!-- 修正后的li元素,href链接已修改 -->
      </ul>
    <!-- 源文件中<func>节点生成的内容 -->
      <ul class="nav">
         <!-- 修正后的li元素,href链接已修改 -->
      </ul>
   </body>
</html>

针对默认命名空间场景的解决方案

针对XSLT中设置了xpath-default-namespace的情况,可使用以下改造后的XSLT实现需求:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="3.0" xpath-default-namespace="https://clonkspot.org" exclude-result-prefixes="xs">
    <xsl:output method="html" encoding="ISO-8859-1" doctype-public="-//W3C//DTD HTML 4.01//EN"
                doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>

    <xsl:template match="/clonkDoc">
        <html>
            <body>
                <xsl:apply-templates select="doc('file.html')//ul[@class = 'nav']" xpath-default-namespace="" mode="fix-links"/>
                <xsl:apply-templates select="func"/>
                <!-- other possible nodes under /clonkDoc -->
                <xsl:apply-templates select="doc('file.html')//ul[@class = 'nav']" xpath-default-namespace="" mode="fix-links"/>
            </body>
        </html>
    </xsl:template>

    <xsl:mode name="fix-links" on-no-match="shallow-copy"/>

    <xsl:template mode="fix-links" match="ul/li/a/@href" xpath-default-namespace="">
        <xsl:message>Value href: <xsl:value-of select="."></xsl:value-of></xsl:message>
        <xsl:attribute name="{name()}" select="replace(., '../../sdk', 'foobar')"/>
    </xsl:template>
</xsl:stylesheet>

注:将代码中的'foobar'替换为实际需要的相对路径变量即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 18:55:28