XSL如何复制相同节点并添加带有属性更改的副本

3

我需要复制一个节点及其子节点:

  • 首先:进行一次完全相同的复制
  • 然后:对其中的某些属性值进行修改,得到一个修改过的副本

以下是需要进行修改的部分内容:

<Configuration
    Name="Debug|Win32"
    OutputDirectory=".\Debug"
    IntermediateDirectory=".\Debug"
    ATLMinimizesCRunTimeLibraryUsage="FALSE"
    CharacterSet="2">
    <Tool
        Name="VCCLCompilerTool"
        Optimization="0"
        BasicRuntimeChecks="3"
        RuntimeLibrary="1"
        AssemblerListingLocation=".\Debug/"
        ObjectFile=".\Debug/"
        ProgramDataBaseFileName=".\Debug/"
        WarningLevel="4"
        SuppressStartupBanner="TRUE"
        DebugInformationFormat="4"
        CompileAs="0"/>
</Configuration>

在第二个副本中,我需要将所有的“Debug”更改为“Release”,并且还需要更改一些属性值。
谢谢。
2个回答

5

我发现一种使用模板模式的解决方案:

感谢Koynov

<xsl:template match="Configuration[@Name='Debug|Win32']">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <xsl:copy>
        <xsl:attribute name="WholeProgramOptimization">TRUE</xsl:attribute>
        <xsl:apply-templates select="@*|node()" mode="Release"/>
    </xsl:copy>
</xsl:template>

使用这个模板适用于所有属性:

    <xsl:template match="@*" mode="Release">
    <xsl:attribute name="{local-name()}">
        <xsl:call-template name="replace-string">
            <xsl:with-param name="text"     select="."/>
            <xsl:with-param name="replace"  select="'Debug'"/>
            <xsl:with-param name="with"     select="'Release'"/>
        </xsl:call-template>
    </xsl:attribute>
</xsl:template>

当然还有“replace-string”模板。感谢您再次使用我们的服务。

2
这里是一个适用于您需求的XSLT转换:

  <xsl:template match="Configuration">
    <xsl:copy-of select="."/>
    <xsl:call-template name="CopyWithReplace">
      <xsl:with-param name="Node" select="."/>
      <xsl:with-param name="PatternToReplace" select="string('Debug')"/>
      <xsl:with-param name="ValueToReplaceWith" select="string('Release')"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="CopyWithReplace">
    <xsl:param name="Node"/>
    <xsl:param name="PatternToReplace"/>
    <xsl:param name="ValueToReplaceWith"/>
    <xsl:variable name="ReplacedNodeName">
      <xsl:call-template name="SearchAndReplace">
        <xsl:with-param name="input" select="name($Node)"/>
        <xsl:with-param name="search-string" select="$PatternToReplace"/>
        <xsl:with-param name="replace-string" select="$ValueToReplaceWith"/>      </xsl:call-template>
    </xsl:variable>
    <xsl:element name="{$ReplacedNodeName}">
      <xsl:for-each select="@*">
        <xsl:variable name="ReplacedAttributeName">
          <xsl:call-template name="SearchAndReplace">
            <xsl:with-param name="input" select="name()"/>
            <xsl:with-param name="search-string" select="$PatternToReplace"/>
            <xsl:with-param name="replace-string" select="$ValueToReplaceWith"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="ReplacedAttributeValue">
          <xsl:call-template name="SearchAndReplace">
            <xsl:with-param name="input" select="."/>
            <xsl:with-param name="search-string" select="$PatternToReplace"/>
            <xsl:with-param name="replace-string" select="$ValueToReplaceWith"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:attribute name="{$ReplacedAttributeName}">
          <xsl:value-of select="$ReplacedAttributeValue"/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="child::*" >
        <xsl:call-template name="CopyWithReplace">
          <xsl:with-param name="Node" select="."/>
          <xsl:with-param name="PatternToReplace" select="$PatternToReplace"/>
          <xsl:with-param name="ValueToReplaceWith" select="$ValueToReplaceWith"/>
        </xsl:call-template>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

  <xsl:template name="SearchAndReplace">
    <xsl:param name="input"/>
    <xsl:param name="search-string"/>
    <xsl:param name="replace-string"/>
    <xsl:choose>
      <!-- See if the input contains the search string -->
      <xsl:when test="$search-string and contains($input,$search-string)">
        <!-- If so, then concatenate the substring before the search string to the replacement string
          and to the result of recursively applying this template to the remaining substring. -->
        <xsl:value-of select="substring-before($input,$search-string)"/>
        <xsl:value-of select="$replace-string"/>
        <xsl:call-template name="SearchAndReplace">
          <xsl:with-param name="input" select="substring-after($input,$search-string)"/>
          <xsl:with-param name="search-string" select="$search-string"/>
          <xsl:with-param name="replace-string" select="$replace-string"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <!-- There are no more occurrences of the search string so just return the current input string -->
        <xsl:value-of select="$input"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>   
</xsl:stylesheet>

祝你好运。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接