GDS 82.0 GDS 82.0 GDS 82.0

XSLT" Remove child tags and rename parent tag based on substring condition and concatenation

42 Views Asked by At

This is my input XML

<?xml version="1.0" encoding="UTF-8"?>

<?xml version="1.0" encoding="UTF-8"?>
<Payload>
<a1>GDS</a1>
<a2>82.0</a2>
<tag0000>
<array>
<dateSent><string>20230130</string></dateSent>
<timeSent><string>1259</string></timeSent>
</array>
</tag0000>
</Payload>

I need to remove the array tag and remove string tag as well, and i need to rename tag0000 to Tagx00 if tag0000 comes and tagx200 is tag2000 comes, so basically in need the output like this

<?xml version="1.0" encoding="UTF-8"?>
<Payload>
<a1>GDS</a1>
<a2>82.0</a2>
<tagx000>
<dateSent>20230130</dateSent>
<timeSent>1259</timeSent>
</tagx000>
</Payload>

this is my code, i am relatively new to xslt

<?xml version="1.0" encoding="UTF-8"?>

\<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"\>
\<xsl:template match="string"\>
\<xsl:value-of select="."/\>
\</xsl:template\>
\<xsl:template match="@*|node()"\>
\<xsl:copy\>
\<xsl:apply-templates select="@*|node()"/\>
\</xsl:copy\>
\</xsl:template\>
\<xsl:template match="*/text()\[normalize-space()\]"\>
\<xsl:value-of select="normalize-space()"/\>
\</xsl:template\>
\<xsl:template match="*/text()\[not(normalize-space())\]" /\>
\<xsl:template match="*\[starts-with(name(), 'tag0') or starts-with(name(), 'tag2') \]"\>
\<xsl:element name="{concat('tagx', substring(name(), 5))}"\>
\<xsl:apply-templates select="*" /\>
\</xsl:element\>
\</xsl:template\>

<!-- rename item to its parent's name -->

\<xsl:template match="array"\>
\<xsl:element name="{name(..)}"\>
\<xsl:apply-templates/\>
\</xsl:element\>
\</xsl:template\>
\</xsl:stylesheet\>

but i am getting the result where <tag0000> is still coming alongwith tagx000

<?xml version="1.0" encoding="UTF-8"?>
<Payload>
<a1>GDS</a1>
<a2>82.0</a2>
<tagx000><tag0000>
<dateSent>20230130</dateSent>
<timeSent>1259</timeSent>
</tag0000></tagx000>
</Payload>

please help me do all the operations within one code

1

There are 1 best solutions below

1
michael.hor257k On

How about:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="array | string">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="tag0000">
    <tagx000>
        <xsl:apply-templates/>
    </tagx000>
</xsl:template>

<xsl:template match="tag2000">
    <tagx200>
        <xsl:apply-templates/>
    </tagx200>
</xsl:template>

</xsl:stylesheet>

If you want, you could combine the last two templates into something like:

<xsl:template match="tag0000 | tag2000">
    <xsl:element name="tagx{substring(name(), 4, 3)}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>