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

如何用XSLT 1.0向现有XSLT中添加模板节点?

使用XSLT 1.0向现有XSLT文件末尾添加模板节点的实现问题

我需要用XSLT 1.0转换现有XSLT文件,完成三个任务:

  • 添加由参数指定名称的新参数(已完成)
  • 将第二个参数中的模板节点内容添加到现有XSLT末尾(本次聚焦此任务)
  • 将参数值加入输出XSLT的Identity模板

需求背景:ETL低代码场景中,接收用户输入的参数名和模板,自动添加到现有XSLT文件,避免用户直接修改复杂的原文件。


原XSLT文件

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:hl7="urn:hl7-org:v3" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" >
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="RemoveHistoryPastIllness" />

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()">
                <xsl:with-param name="RemoveHistoryPastIllness" select="$RemoveHistoryPastIllness" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template
        match="/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:templateId/@root = '2.16.840.1.113883.10.20.22.2.20']">
        <xsl:param name="RemoveHistoryPastIllness" />
        <xsl:choose>
            <!-- Need to remove the Illness History -->
            <xsl:when test="$RemoveHistoryPastIllness = 1"> </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()" />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

当前转换代码

目前仅能直接复制原XSLT,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl">  

    <xsl:output method="xml" indent="yes" />
    <xsl:param name="myTemplate" >
        <xsl:text disable-output-escaping="yes">
            &lt;xsl:template
        match=&quot;/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:templateId/@root = &amp;apos;2.16.840.1.113883.10.20.22.2.10&amp;apos;]&quot;&gt;
        &lt;xsl:param name=&quot;RemoveTreatmentPlan&quot; /&gt;
        &lt;xsl:choose&gt;
            &lt;xsl:when test=&quot;$RemoveTreatmentPlan = 1&quot;&gt; &lt;/xsl:when&gt;
            &lt;xsl:otherwise&gt;
                &lt;xsl:copy&gt;
                    &lt;xsl:apply-templates select=&quot;@* | node()&quot; /&gt;
                &lt;/xsl:copy&gt;
            &lt;/xsl:otherwise&gt;
        &lt;/xsl:choose&gt;
    &lt;/xsl:template&gt;
            </xsl:text>
    </xsl:param>

    <!--Identity Match-->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template> 
</xsl:stylesheet>

期望输出结果

将myTemplate参数中的内容作为实际模板节点添加到现有XSLT末尾:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:hl7-org:v3" xmlns:hl7="urn:hl7-org:v3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xs hl7"
    version="1.0">

    <xsl:output method="xml" indent="yes" />
    <xsl:param name="RemoveHistoryPastIllness" />

    <xsl:template match="@* | node()" name="ident">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()">
                <xsl:with-param name="RemoveHistoryPastIllness" select="$RemoveHistoryPastIllness" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template
        match="/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:templateId/@root = '2.16.840.1.113883.10.20.22.2.20']">
        <xsl:param name="RemoveHistoryPastIllness" />
        <xsl:choose>
            <!-- Need to remove the Illness History -->
            <xsl:when test="$RemoveHistoryPastIllness = 1"> </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()" />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template
        match="/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:templateId/@root = '2.16.840.1.113883.10.20.22.2.10']">
        <xsl:param name="RemoveTreatmentPlan" />
        <xsl:choose>
            <!-- Need to remove the Treatment Plan -->
            <xsl:when test="$RemoveTreatmentPlan = 1"> </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()" />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

遇到的问题

请问上述需求是否可行?我尝试在Identity模板的<xsl:copy>内的<xsl:apply-templates>上下添加节点,结果节点出现在<xsl:stylesheet>标签外;在其他位置添加则报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 16:27:01