【问题标题】:Multiple replacements in XML using XSLT使用 XSLT 在 XML 中进行多次替换
【发布时间】: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$$ ?

【问题讨论】:

标签: xml xslt


【解决方案1】:

这应该可以工作

<?xml version="1.0" encoding="utf-8"?>

<xsl:variable name="path" select="'E:/test'"/>

<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">$$path$$</xsl:param>
    <xsl:variable name="attribute-name" select="name()"/>

    <xsl:choose>
        <xsl:when test="contains(., $find)">
            <xsl:attribute name="{$attribute-name}">

                <xsl:call-template name="changeStrings">
                    <xsl:with-param name="val"  select="."/>
                    <xsl:with-param name="find" select="$find"/>
                    <xsl:with-param name="path" select="$path"/>
                </xsl:call-template>                        


            </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="changeStrings">
    <xsl:param name="val"/>
    <xsl:param name="path"/>
    <xsl:param name="find"/>

    <xsl:variable name="newval" select="concat(substring-before($val, $find), $path, substring-after($val, $find))"/>

    <xsl:choose>
        <xsl:when test="contains($newval,'$$')">
            <xsl:call-template name="changeStrings">
                <xsl:with-param name="val" select="$newval"/>
                <xsl:with-param name="find" select="$find"/>
                <xsl:with-param name="path" select="$path"/>
            </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
            <xsl:value-of select="$newval"/>                
        </xsl:otherwise>
    </xsl:choose>


</xsl:template>

请注意,我将您的参数“路径”更改为变量,只是为了使其更易于运行。 此外,您正在寻找 $$project$$,而不是 $$path$$。 在这个 XLS 中有很多更好的方法来做很多事情。

这里还有 XML,我稍作改动

<tests>
<testcases id="1">
    <command value="copy">
        <files value="$$path$$/1.txt $$path$$/2.txt" />
    </command>
</testcases>

【讨论】:

  • 我必须说我很失望。我提供了一个可行的解决方案,而 Alok Ranjan 甚至都懒得承认。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 2013-06-29
相关资源
最近更新 更多