Home > Software engineering >  Removing Digits From XML Element
Removing Digits From XML Element

Time:11-03

I am trying to remove digits from an elements that are located from child elements. Can anyone help with with the xslt needed for this? Here is an example that should remove "_0" from the element "mgt_interface_0" . Thanks in advance!

Piece from source XML:

<mgt_interfaces dataType="fXml">
         <number_of_mgt_interfaces dataType="fInt">1</number_of_mgt_interfaces>
         <mgt_interface_0 dataType="fXml" modelIndex="0">
            <ip_address dataType="fSting">###.##.###.###</ip_address>
            <name dataType="fString">Port</name>
            <netmask dataType="fString">###.###.###.###</netmask>
            <gateway dataType="fString">###.##.###.###</gateway>
            <mac_address dataType="fString">##:##:##:##:##:##</mac_address>
            <state dataType="fString">Enabled</state>
         </mgt_interface_0>

Output XML:

<mgt_interfaces dataType="fXml">
         <number_of_mgt_interfaces dataType="fInt">1</number_of_mgt_interfaces>
         <mgt_interface dataType="fXml" modelIndex="0">
            <ip_address dataType="fSting">###.##.###.###</ip_address>
            <name dataType="fString">Port</name>
            <netmask dataType="fString">###.###.###.###</netmask>
            <gateway dataType="fString">###.##.###.###</gateway>
            <mac_address dataType="fString">##:##:##:##:##:##</mac_address>
            <state dataType="fString">Enabled</state>
         </mgt_interface_0>

I do not have much experience in XSLT so I need some help and suggestions. Thanks!

CodePudding user response:

If you know in advance that the numbered elements are named mgt_interface_0, mgt_interface_1, mgt_interface_2 etc. then you can remove the numbering quite simply using:

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="*[starts-with(name(), 'mgt_interface_')]">
    <mgt_interface>
        <xsl:apply-templates select="@*|node()"/>
    </mgt_interface>
</xsl:template>
          
</xsl:stylesheet> 

Of course, this assumes there are no elements named mgt_interface_detail, for example.

CodePudding user response:

If you want to remove all digits and also remaining trailing underscores, the XSLT-1.0 code would be a bit more complicated:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
    <xsl:output  method="xml" indent="yes" omit-xml-declaration="yes"/>

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

    <xsl:template match="*">
        <xsl:variable name="wo_numbers" select="translate(name(),'0123456789','')" />
        <xsl:variable name="new_name">
          <xsl:choose>
            <xsl:when test="string-length($wo_numbers) != string-length(name())">
         <xsl:choose>
            <xsl:when test="substring($wo_numbers,string-length($wo_numbers),1)='_'">
                <xsl:value-of select="substring($wo_numbers,1,string-length($wo_numbers)-1)" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$wo_numbers" />
            </xsl:otherwise>
        </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
            <xsl:value-of select="name()" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:element name="{$new_name}">
          <xsl:copy-of select="@*" />
          <xsl:apply-templates select="node()" />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

For your example given, the output should be as expected.

  • Related