如何用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"> <xsl:template match="/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:templateId/@root = &apos;2.16.840.1.113883.10.20.22.2.10&apos;]"> <xsl:param name="RemoveTreatmentPlan" /> <xsl:choose> <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: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
相关产品推荐
相关产品推荐

