[Solved] XLST – Copy XML tag and replace attribute value [closed]


XSLT based solution.

Input 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>

Output 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>

0

solved XLST – Copy XML tag and replace attribute value [closed]