XSLT - 复制 XML 标记并替换属性值

XLST - Copy XML tag and replace attribute value

我有以下 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>

我想复制连接标签,将它们粘贴到 </option> 标签之后,并将端口的前 2 位数字替换为这样的内容

<config>
  <connection port="4404" type="tcp">
      <selection name="test-mode1" enabled="true"/>
  </connection>
  <connection port="4405" type="tcp">
      <selection name="test-mode2" enabled="true"/>
  </connection>
  <connection port="4406" type="tcp">
      <selection name="test-mode3" enabled="true"/>
  </connection>

  <option>
    <maxNumberOfDownloads>10</maxNumberOfDownload>
  </option>

  <connection port="7804" type="tcp">
      <selection name="test-mode1" enabled="true"/>
  </connection>
  <connection port="7805" type="tcp">
      <selection name="test-mode2" enabled="true"/>
  </connection>
  <connection port="7806" type="tcp">
      <selection name="test-mode3" enabled="true"/>
  </connection>
  
</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</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/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

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