我对XSLT非常陌生,根本不熟悉其背后的逻辑。我有以下XML:
<config>
<connection port="4404" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="4405" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="4406" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<option>
<maxNumberOfDownloads>10</maxNumberOfDownload>
</option>
</config>
在应用了XLST-Copy XML标记和替换属性值的一些转换之后,我能够复制连接标记并根据以前的值更改端口属性。
<config>
<connection port="4404" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="7804" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="4405" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="7805" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="4406" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="7806" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<option>
<maxNumberOfDownloads>10</maxNumberOfDownloads>
</option>
</config>
这是我的XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- Initial template to copy all nodes and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="connection[@port]">
<xsl:copy>
<!-- Copy all nodes and attributes -->
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="port">
<xsl:value-of select="concat('78', substring(@port,3,2))"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
目标:除了更改端口属性外,我还试图将属性enabled="false"
更改为新复制的元素(具有包含'78'端口的连接元素(
所需的XML输出:
<config>
<connection port="4404" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="4405" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="4406" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<option>
<maxNumberOfDownloads>10</maxNumberOfDownloads>
</option>
<connection port="7804" type="tcp">
<selection name="test-mode" enabled="false"/>
</connection>
<connection port="7805" type="tcp">
<selection name="test-mode" enabled="false"/>
</connection>
<connection port="7806" type="tcp">
<selection name="test-mode" enabled="false"/>
</connection>
</config>
是否有方法在复制过程中直接更新属性,或者是否有必要创建新模板
这可以一次完成。
输入XML
<config>
<connection port="4404" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="4405" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="4406" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<option>
<maxNumberOfDownloads>10</maxNumberOfDownloads>
</option>
</config>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="connection[@port]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="port">
<xsl:value-of select="concat('78', substring(@port,3,2))"/>
</xsl:attribute>
<!--<xsl:apply-templates/>-->
<selection>
<xsl:apply-templates select="selection/@*"/>
<xsl:attribute name="enabled">
<xsl:value-of select="'false'"/>
</xsl:attribute>
</selection>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输出
<config>
<connection port="4404" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="7804" type="tcp">
<selection name="test-mode" enabled="false"/>
</connection>
<connection port="4405" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="7805" type="tcp">
<selection name="test-mode" enabled="false"/>
</connection>
<connection port="4406" type="tcp">
<selection name="test-mode" enabled="true"/>
</connection>
<connection port="7806" type="tcp">
<selection name="test-mode" enabled="false"/>
</connection>
<option>
<maxNumberOfDownloads>10</maxNumberOfDownloads>
</option>
</config>