Thursday, April 28, 2022

[SOLVED] XLST - Copy XML tag and replace attribute value

Issue

I have the following XML file:

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

I would like to copy the connection tags, paste them after the </option> tag and replace the first 2 digits of the port to have something like this

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

Any hints on how this could be achieved?


Solution

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>


Answered By - Yitzhak Khabinsky
Answer Checked By - Mary Flores (WPSolving Volunteer)