XSLT - 如果父属性包含特定文本,则更改子属性
XSLT - Change child attribute if parent attribute contains specific text
我对 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>
应用 的一些转换后,我能够复制连接标记并根据以前的值更改端口属性。
<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>
我对 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>
应用
<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>