XSL 如何复制相同的 +添加属性更改的副本

发布于 2024-08-26 04:44:15 字数 748 浏览 12 评论 0原文

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

  • 首先:一个相同的副本
  • ,然后是更改了某些属性值的修改副本

以下是要更改的摘录:

<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>

在第二个副本中,我需要将所有“调试”更改为“发布”还有一些属性值。

谢谢

I need to copy a node and its sub-nodes:

  • first: one identical copy
  • followed by a modified copy with some attributes values changed

Here is the extract to change:

<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>

In the second copy I need to change all "Debug" to "Release" and some attribute values also.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最舍不得你 2024-09-02 04:44:15

感谢 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>

当然还有“替换字符串”模板。

再次感谢。

Thanks Koynov

I found however a solution using template modes:

<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>

With this template for all attributes:

    <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>

and of-course the "replace-string" template.

Thanks again.

挖个坑埋了你 2024-09-02 04:44:15

这是满足您需求的 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>

祝你好运。

Here is a xslt transformation for your needs:

  <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>

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文