我正在尝试修改xml中节点的内容并添加注释以告知 我要更新的内容,在这里我想将Version更新为VersionSWC。
Inupt xml :
<?xml version="1.0" encoding="UTF-8"?>
<file>
<path1>/SwComponentTypes/Version/RunVersion</path1>
<path2>/SwComponentTypes/Version/R_CntrBus_Version</path2>
</file>
Out I want :
<?xml version="1.0" encoding="UTF-8"?>
<file>
<!--Patching name of Version to VersionSWC -->
<path1>/SwComponentTypes/VersionSWC/RunVersion</path1>
<!--Patching name of Version to VersionSWC -->
<path2>/SwComponentTypes/VersionSWC/R_CntrBus_Version</path2>
</file>
答案 0 :(得分:0)
使用xslt 2.0,您可以尝试执行以下操作:
<?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"
exclude-result-prefixes="xs"
version="2.0">
<!-- identity template, copies everything -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- template override for path1 and path2 nodes -->
<xsl:template match="path1|path2">
<xsl:param name="old" select="'/Version/'"/>
<xsl:param name="new" select="'/VersionSWC/'"/>
<xsl:comment>
<xsl:value-of select="concat('Patching name of ', $old, ' to ', $new)"/>
</xsl:comment>
<xsl:copy>
<xsl:value-of select="replace(., $old, $new)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>