我對 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 tag and replace attribute value應用一些轉換后,我能夠復制連接標記并根據以前的值更改埠屬性。
<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>
有沒有辦法在復制程序中直接更新屬性,或者是否有必要創建一個新模板?
uj5u.com熱心網友回復:
它可以一口氣完成。
輸入 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396656.html
上一篇:如何將xml和xml-stylesheet節點添加到VBA中的xml檔案?
下一篇:如何在布局中放置四個框?
