【发布时间】:2015-10-06 14:44:05
【问题描述】:
我的 xml 如下所示:
<tests>
<testcases id="1">
<command value="copy">
<files value="$$path$$/1.txt $$path$$/2.txt" />
</testcases>
我的 xsl 文件看起来像
<?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"
>
<xsl:param name="path">##MISSING##</xsl:param>
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:param name="find">$$project$$</xsl:param>
<xsl:variable name="attribute-name" select="name()"/>
<xsl:choose>
<xsl:when test="contains(., $find)">
<xsl:attribute name="{$attribute-name}">
<xsl:value-of select="concat(substring-before(., $find), $path, substring-after(., $find))"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
当我运行 xsl 工具时,我得到的输出为
<tests>
<testcases id="1">
<command value="copy">
<files value="E:/test/1.txt $$path$$/2.txt" />
</testcases>
您能否建议需要做什么来替换第二个 $$path$$ ?
【问题讨论】:
-
我建议使用递归模板调用。看看 JLRishe 提供的这个出色的答案,以获得一个很好的 XSLT 1.0 示例:stackoverflow.com/a/14597353/317052