-1

Goal: Within SVG g id="RoundShapes" for all path, poly, rect, polygon, ellipse, and circle tags: remove class. Leave class in subgroups (g tags) within the RoundShapes group.

I'm able to pull up an element tree; but I don't know python well enough to use the XPATHS or to remove the classes, and apply the removed classes to the svg_text string which is a larger SVG.

Combining a few references, this is what I have started.

Any help in achieving the goal would be appreciated.

I previously attempted this with re.sub and Regex but was directed by several people to try XML parsing instead...

Code:

def remove_RD_group_classes(svg_text):
    """
    Within SVG <g id="RoundShapes"> for all path, poly, rect, polygon, ellipse, and circle tags, remove class.  Leave class in subgroup g tags.
    """
    
    import xml.etree.ElementTree as ET
    
    root = ET.fromstring(svg_text)
    
    roundshapes = root.findall("//*[@id='RoundShapes']")
    
    for classes_removed in roundshapes.xpath('//*[@class]'):
    classes_removed.attrib.pop('class')

Here is the svg_text that is loaded as an example:

<svg xmlns="http://www.w3.org/2000/svg" id="Shapes" viewBox="0 0 612 792">
    <defs>
        <style>.cls-1{fill:#730e31;}.cls-1,.cls-11,.cls-12,.cls-2,.cls-4,.cls-5,.cls-6,.cls-7,.cls-8,.cls-9{stroke:#231f20;stroke-miterlimit:10;}.cls-2{fill:#2c318e;}.cls-3{fill:#231f20;}.cls-4{fill:#e60680;}.cls-5{fill:#ee2742;}.cls-6{fill:#2a3e98;}.cls-7{fill:#00aeef;}.cls-8{fill:#62449b;}.cls-9{fill:#e6a923;}.cls-10{fill:#ed1c24;}.cls-11{fill:#a8cf38;}.cls-12{fill:none;}</style>
    </defs>
    <g id="PolyShapes">
        <polygon class="cls-1" points="331.2 451.55 190.02 502.69 75.14 406 101.45 258.17 242.62 207.03 357.5 303.72 331.2 451.55"/>
        <polygon class="cls-2" points="487.4 188.74 395.4 243.94 301.6 191.88 299.79 84.61 391.79 29.41 485.59 81.48 487.4 188.74"/>
        <g id="OtherShapesAndLines">
            <g id="Layer_1" data-name="Layer 1">
                <path class="cls-3" d="M143.08,124.94c-4.72,3.18-9.54,6.24-14.18,9.54-4.31,3.08-7.27,7.23-5.43,12.72,1.41,4.23,5.1,7.91,7.82,11.36,3.07,3.9,6.24,7.73,9.46,11.51,12.79,15,26.48,29.14,39.1,44.26,12.13,14.52,23.41,30.23,30.75,47.79,3.3,7.89,8.23,19.14,4.57,27.67-1.71,4-5.57,6.62-7.3,10.7a18.18,18.18,0,0,0,.19,14.11c4.08,10.18,13.55,14.28,20.85,21.6,6.41,6.42,5,17.7-4.44,20.43-7.41,2.13-4.26,13.72,3.19,11.57A24.26,24.26,0,0,0,244.32,351c2.33-9-.91-17.46-7.44-23.75-5.59-5.4-18-10.69-18.4-19.64-.21-4.12,4.43-7.12,6.37-10.42a27.62,27.62,0,0,0,3.42-14.39c-.19-9.77-4-19.28-7.91-28.1a162.5,162.5,0,0,0-14.2-25.41c-11.07-16.4-24.31-31.19-37.55-45.85-7.39-8.19-14.82-16.36-21.91-24.82q-2.77-3.3-5.47-6.67c-1.57-2-3.3-5.25-5.26-6.82-1.53-1.22-.58-1.62-1-.24.3-1,3.7-2.5,4.72-3.19l9.45-6.36c6.38-4.29.38-14.69-6-10.36Z"/>
            </g>
        </g>
    </g>
    <g id="DefinedPolyShapes">
        <polygon id="PinkStar" class="cls-4" points="300.86 650.54 248.69 604.37 181.09 621.17 208.88 557.29 172.01 498.19 241.35 504.88 286.17 451.55 301.23 519.57 365.8 545.71 305.77 581.05 300.86 650.54"/>
    </g>
    <g id="RoundShapes">
        <g id="ReddishRound">
            <ellipse class="cls-5" cx="402.27" cy="406.98" rx="25.62" ry="44.57"/>
        </g>
        <g id="BluishRound" class="bluishround">
            <ellipse class="cls-6" cx="268.91" cy="468.5" rx="39.15" ry="35.95"/>
            <g id="BluishRoundOvals">
                <ellipse class="cls-7" cx="-5.99" cy="192.88" rx="55.37" ry="28.93"/>
                <ellipse class="cls-7" cx="57.64" cy="239.16" rx="26.45" ry="128.1"/>
            </g>
        </g>
        <g id="OtherRound">
            <circle class="cls-8" cx="72.52" cy="255.69" r="58.68"/>
            <ellipse class="cls-9" cx="393.6" cy="198.25" rx="50.83" ry="34.3"/>
            <g id="free_draw" data-name="free draw">
                <path class="cls-3" d="M264.19,356.46a78.69,78.69,0,0,0-16.12,17.84c-4,6.06-8.49,13.77-8.61,21.24-.32,19.43,29,16.81,41.19,13.29,8.39-2.41,14-6.67,15.38-15.65a54.7,54.7,0,0,0-1.71-24.81c-2.13-6.82-5.55-15.45-12.62-18.44a11.93,11.93,0,0,0-10.67.49c-3.15,1.89-4.11,5.48-6.83,7.66-6,4.84,2.52,13.28,8.49,8.49,1.64-1.32,2.41-3.05,3.75-4.57a15.28,15.28,0,0,1,.89-1.37l.13.56c.64.18,1.29,1.56,1.62,2.06a38.53,38.53,0,0,1,5.09,14c.84,4.73,1.21,10.52-.15,15.18-1.47,5-9.15,5.73-13.69,6.32a43,43,0,0,1-16.3-.7c-2.37-.64-2.85-1.2-2.4-3.72a24.4,24.4,0,0,1,2.4-6.28A68.61,68.61,0,0,1,272.68,365c6-4.87-2.54-13.32-8.49-8.49Z"/>
                <path class="cls-3" d="M364.05,442.28c-12.83,7-14.81,23.26-6,34.37,9,11.36,25.48,13.4,38.74,9.81,13.95-3.78,25.66-14.94,27.85-29.54,2-13.28-5.21-27.24-18.08-32-8.49-3.13-15.93-.08-23.17,4.46l-24.89,15.65c-6.52,4.11-.5,14.49,6.06,10.37L383,443.81c4.78-3,11.28-9,17.33-8.18,12.27,1.59,15,17.39,10.11,26.47-5.8,10.81-19.31,15.62-31.05,13.57-5.45-.95-10.9-3.67-13.53-8.77-2.83-5.49-1.19-11.28,4.25-14.26,6.77-3.71.72-14.08-6.06-10.36Z"/>
                <path class="cls-3" d="M289.19,194c-19.47,6.15-41.16,15-45.88,37.27-4.21,19.92,7.41,39.36,28.09,42.33,45.88,6.61,63.66-55.14,39.64-86.47-13.28-17.32-40.76-21.21-54.58-2.33-4.57,6.24,5.84,12.24,10.36,6.06,8.44-11.53,26-6.92,33.87,2.43C310.37,204.69,312,222.2,308,236.2S292.9,262,277.55,262.05c-14.3.09-24.51-10.76-23.06-25.11,1.95-19.44,21.91-26.36,37.89-31.41,7.34-2.31,4.21-13.9-3.19-11.57Z"/>
                <path class="cls-3" d="M186.17,35.2c-7.1,11.86-15.15,33.11-1.69,43.48,12,9.26,31.73,8.1,44.26.62s20.37-22.29,18-36.9a32,32,0,0,0-29.4-26.88c-19.17-1.22-33.68,13-44.31,27.14-4.65,6.17,5.77,12.16,10.36,6.06,8-10.62,19.4-22.46,33.95-21.2,13.23,1.14,20.62,15.15,16.77,27.21C229.86,68,215.78,74.51,202.6,72.87c-6.52-.82-13.64-2.88-13.28-10.6.34-7.38,3.47-14.77,7.21-21,4-6.64-6.39-12.68-10.36-6Z"/>
                <path class="cls-3" d="M111.11,184.61c-1.86,16.63.44,35.24,13.61,47,12.52,11.19,29.66,11.15,43.22,1.69,13.79-9.61,23.27-25.33,26.94-41.6,3.23-14.31,2.69-32.46-9.79-42.39-7.73-6.15-18-7-27.56-6.45a88.59,88.59,0,0,0-31,7.74c-7,3.12-1,13.47,6.06,10.36a72.66,72.66,0,0,1,28.53-6.23c7.57-.08,15.59.92,19.92,7.89,7.89,12.7,2.27,31.54-4.53,43.42-6.92,12.09-21.61,27.39-37,21.06-16.54-6.77-18.08-27.3-16.38-42.5.85-7.67-11.15-7.61-12,0Z"/>
                <path class="cls-3" d="M195.49,299l-1.67,3.3a6.43,6.43,0,0,0-.6,4.63,6,6,0,0,0,11,1.43l1.66-3.3a6.36,6.36,0,0,0,.6-4.62,6,6,0,0,0-7.38-4.2,6,6,0,0,0-3.58,2.76Z"/>
                <path class="cls-3" d="M181.56,380.67a132.43,132.43,0,0,0,57.94,45.09c2.94,1.18,6.69-1.31,7.38-4.19,7.86-33.07-19.48-61.28-47.13-74.39-12.09-5.73-29-11.51-40.08-.47-5.29,5.29-7.37,13-9.09,20.08a129.69,129.69,0,0,0-3.51,27.75c-.17,7.72,11.83,7.72,12,0a113.55,113.55,0,0,1,2.26-20.88c1.15-5.55,2.25-12.79,5.87-17.39,7.2-9.15,23.29-.58,31,3.54,20.66,11.08,43.26,32.75,37.12,58.57l7.38-4.19a117.78,117.78,0,0,1-50.77-39.57c-4.5-6.18-14.92-.2-10.36,6.05Z"/>
                <path class="cls-10" d="M129,503.44c-11.5,8.36-23.83,18-26.94,32.74-2.82,13.4,3.23,26.6,16,32.14s29,1.33,37.86-9.41,7.54-26.77-.69-37.54c-4.63-6-15-.08-10.36,6.06,5.09,6.66,7.7,16.37,2,23.57s-17.17,10.12-25.39,5.72-9.88-13.67-6.54-21.83c3.75-9.18,12.33-15.45,20.08-21.09,6.19-4.5.21-14.91-6-10.36Z"/>
                <path class="cls-10" d="M428.44,303.58a90.76,90.76,0,0,0-2.24,45.25c1.18,5.74,3.38,13,10.36,13.53,6.4.48,10.78-7,13.69-11.66,7.23-11.64,10.29-25.27,11.9-38.72,1.34-11.2,2.27-29.18-9.52-35.67-7.86-4.32-14.41,2.91-19.27,8.29A102.14,102.14,0,0,0,419,305c-3.64,6.8,6.71,12.87,10.36,6.06a90.13,90.13,0,0,1,10.72-16c1.56-1.82,3.79-5.23,5.94-6.32,2.67-1.35.24-1.83,2.26,0,.84.76,1.21,2.53,1.52,3.54,3.21,10.37.27,23.76-2.38,34a67.89,67.89,0,0,1-6,15.65c-1.38,2.52-3.54,4.79-4.8,7.32l-1,1.22q4.39,2.3,3.34.09c.18-.92-.8-3-1-4A78.21,78.21,0,0,1,440,306.77c2.29-7.4-9.29-10.56-11.57-3.19Z"/>
                <g id="Layer_15" data-name="Layer 15">
                    <path class="cls-10" d="M184.51,283.15c-4.06,9.29-10.66,30.15,3.21,34.5,11.9,3.73,27.61.9,36.64-7.79,8.15-7.84,12.85-20.36,12.67-31.61-.08-5.35-1.36-10.9-4.82-15.11-4.42-5.38-10.88-6.45-17.5-6.54-7.72-.11-7.73,11.89,0,12,3.55,0,7.15-.1,9,3.36,1.44,2.62,1.45,6,1.21,8.94a34.16,34.16,0,0,1-7.11,18.39c-5,6.12-12.2,7.71-19.83,7.53-1.09,0-5.7.35-6.43-.51-.28-.35,0-3,.09-3.68a38.66,38.66,0,0,1,3.2-13.42c3.07-7-7.26-13.12-10.36-6.06Z"/>
                    <path class="cls-10" d="M373,184.76a181.71,181.71,0,0,0-48.85,23.55c-6.38,4.39-13.43,9.17-16.71,16.48a15.7,15.7,0,0,0,5.35,19.67c6.24,4.47,14.65,5.46,22.07,6.27A103.48,103.48,0,0,0,405.26,232c14.53-10.69,25.67-29.4,12.83-45.91-4.74-6.09-13.17,2.46-8.49,8.48,4.92,6.33,1.32,15.22-3.25,20.54-5.23,6.08-12.46,10.56-19.52,14.19a91.75,91.75,0,0,1-45.54,9.9A82.2,82.2,0,0,1,330,238c-3.35-.61-8-1.14-10.67-3.51-5.6-4.95,5.84-12.32,9.39-14.82a170.39,170.39,0,0,1,47.43-23.35c7.35-2.3,4.21-13.89-3.19-11.57Z"/>
                </g>
            </g>
            <ellipse class="cls-11" cx="297.73" cy="273.45" rx="24.38" ry="51.65"/>
        </g>
        <circle class="cls-12" cx="327.59" cy="388.85" r="21.59"/>
    </g>
    <g id="SquareShapes">
        <rect class="cls-12" x="153.51" y="85.44" width="152.49" height="81.82"/>
        <rect class="cls-4" x="419.63" y="543.29" width="92.56" height="92.56"/>
        <rect class="cls-12" x="376.65" y="232.55" width="71.9" height="71.9"/>
        <g id="Freedraw">
            <path class="cls-3" d="M481.63,222.69a393.26,393.26,0,0,0-79.93,13c-4.1,1.11-5.09,5.33-3.59,8.81l3,7,5.18-9a197.38,197.38,0,0,0-48.19,2c-6.06,1-15.28,1.36-19.43,6.55-4.62,5.78-.78,15,.66,21.39a530,530,0,0,0,29.48,90.88c1.56,3.67,7,3.63,9.42,1.22,10.22-10,28.28-7.18,41.19-6.48,16.29.88,32.81,2,48.87-1.68a84.91,84.91,0,0,0,22.59-9c6.31-3.55,12.82-7.08,13.78-14.91s.66-16.06.48-23.91a137.13,137.13,0,0,0-2.52-24.25c-3.34-16.28-15.7-33.93-9.81-50.81,2.56-7.3-9-10.44-11.57-3.19-5.1,14.6.8,29.06,5.66,42.83,5.45,15.47,6.54,31.05,6.22,47.35,0,2.91.84,8.47-.41,11.08-1.17,2.43-5.56,4.13-7.89,5.45a74.06,74.06,0,0,1-23.26,8.47c-16,2.9-32.51.91-48.59.21-15-.66-31.73-.93-43.22,10.31l9.42,1.22a513.83,513.83,0,0,1-25.9-77.94c-1.54-6.27-2.24-13.22-4.35-19.28.19-1.8-.06-2.31-.76-1.53-.77.55-.45.54,1,0a27.91,27.91,0,0,0,4.66-1q5.15-1.06,10.34-1.83a185.32,185.32,0,0,1,42.17-1.12c5.05.42,6.79-5.28,5.18-9l-3-7-3.58,8.82a377,377,0,0,1,76.74-12.56c7.67-.46,7.72-12.46,0-12Z"/>
            <path class="cls-3" d="M379,478q-45.24-6-90.89-7.77a6.06,6.06,0,0,0-6,6,242.4,242.4,0,0,1-6.66,66.39c-.71,2.91.91,6.83,4.19,7.38a79.76,79.76,0,0,0,23.84.73c7.61-1,14.62-3.39,21.93-5.57,15.6-4.63,30.64,1.7,46.4,1.21a6.08,6.08,0,0,0,6-6q-.74-26.67.69-53.32c.41-7.72-11.59-7.7-12,0q-1.42,26.64-.69,53.32l6-6c-15,.46-29.38-5.22-44.36-2.13-7.13,1.47-13.83,4.47-21,5.93a64.78,64.78,0,0,1-23.64.26l4.19,7.38a254.29,254.29,0,0,0,7.09-69.58l-6,6Q333.73,484,379,490c3.24.43,6-3.07,6-6,0-3.59-2.8-5.58-6-6Z"/>
        </g>
        <g id="BluishRectangles">
            <rect class="cls-7" x="111.36" y="345.77" width="50.23" height="50.23"/>
            <rect class="cls-7" x="353.51" y="239.16" width="50.23" height="50.23"/>
            <rect class="cls-7" x="478.31" y="370.88" width="50.23" height="50.23"/>
        </g>
    </g>
</svg>

Here is expected output:

<svg xmlns="http://www.w3.org/2000/svg" id="Shapes" viewBox="0 0 612 792">
    <defs>
        <style>.cls-1{fill:#730e31;}.cls-1,.cls-11,.cls-12,.cls-2,.cls-4,.cls-5,.cls-6,.cls-7,.cls-8,.cls-9{stroke:#231f20;stroke-miterlimit:10;}.cls-2{fill:#2c318e;}.cls-3{fill:#231f20;}.cls-4{fill:#e60680;}.cls-5{fill:#ee2742;}.cls-6{fill:#2a3e98;}.cls-7{fill:#00aeef;}.cls-8{fill:#62449b;}.cls-9{fill:#e6a923;}.cls-10{fill:#ed1c24;}.cls-11{fill:#a8cf38;}.cls-12{fill:none;}</style>
    </defs>
    <g id="PolyShapes">
        <polygon class="cls-1" points="331.2 451.55 190.02 502.69 75.14 406 101.45 258.17 242.62 207.03 357.5 303.72 331.2 451.55"/>
        <polygon class="cls-2" points="487.4 188.74 395.4 243.94 301.6 191.88 299.79 84.61 391.79 29.41 485.59 81.48 487.4 188.74"/>
        <g id="OtherShapesAndLines">
            <g id="Layer_1" data-name="Layer 1">
                <path class="cls-3" d="M143.08,124.94c-4.72,3.18-9.54,6.24-14.18,9.54-4.31,3.08-7.27,7.23-5.43,12.72,1.41,4.23,5.1,7.91,7.82,11.36,3.07,3.9,6.24,7.73,9.46,11.51,12.79,15,26.48,29.14,39.1,44.26,12.13,14.52,23.41,30.23,30.75,47.79,3.3,7.89,8.23,19.14,4.57,27.67-1.71,4-5.57,6.62-7.3,10.7a18.18,18.18,0,0,0,.19,14.11c4.08,10.18,13.55,14.28,20.85,21.6,6.41,6.42,5,17.7-4.44,20.43-7.41,2.13-4.26,13.72,3.19,11.57A24.26,24.26,0,0,0,244.32,351c2.33-9-.91-17.46-7.44-23.75-5.59-5.4-18-10.69-18.4-19.64-.21-4.12,4.43-7.12,6.37-10.42a27.62,27.62,0,0,0,3.42-14.39c-.19-9.77-4-19.28-7.91-28.1a162.5,162.5,0,0,0-14.2-25.41c-11.07-16.4-24.31-31.19-37.55-45.85-7.39-8.19-14.82-16.36-21.91-24.82q-2.77-3.3-5.47-6.67c-1.57-2-3.3-5.25-5.26-6.82-1.53-1.22-.58-1.62-1-.24.3-1,3.7-2.5,4.72-3.19l9.45-6.36c6.38-4.29.38-14.69-6-10.36Z"/>
            </g>
        </g>
    </g>
    <g id="DefinedPolyShapes">
        <polygon id="PinkStar" class="cls-4" points="300.86 650.54 248.69 604.37 181.09 621.17 208.88 557.29 172.01 498.19 241.35 504.88 286.17 451.55 301.23 519.57 365.8 545.71 305.77 581.05 300.86 650.54"/>
    </g>
    <g id="RoundShapes">
        <g id="ReddishRound">
            <ellipse cx="402.27" cy="406.98" rx="25.62" ry="44.57"/>
        </g>
        <g id="BluishRound" class="bluishround">
            <ellipse cx="268.91" cy="468.5" rx="39.15" ry="35.95"/>
            <g id="BluishRoundOvals">
                <ellipse cx="-5.99" cy="192.88" rx="55.37" ry="28.93"/>
                <ellipse cx="57.64" cy="239.16" rx="26.45" ry="128.1"/>
            </g>
        </g>
        <g id="OtherRound">
            <circle cx="72.52" cy="255.69" r="58.68"/>
            <ellipse cx="393.6" cy="198.25" rx="50.83" ry="34.3"/>
            <g id="free_draw" data-name="free draw">
                <path d="M264.19,356.46a78.69,78.69,0,0,0-16.12,17.84c-4,6.06-8.49,13.77-8.61,21.24-.32,19.43,29,16.81,41.19,13.29,8.39-2.41,14-6.67,15.38-15.65a54.7,54.7,0,0,0-1.71-24.81c-2.13-6.82-5.55-15.45-12.62-18.44a11.93,11.93,0,0,0-10.67.49c-3.15,1.89-4.11,5.48-6.83,7.66-6,4.84,2.52,13.28,8.49,8.49,1.64-1.32,2.41-3.05,3.75-4.57a15.28,15.28,0,0,1,.89-1.37l.13.56c.64.18,1.29,1.56,1.62,2.06a38.53,38.53,0,0,1,5.09,14c.84,4.73,1.21,10.52-.15,15.18-1.47,5-9.15,5.73-13.69,6.32a43,43,0,0,1-16.3-.7c-2.37-.64-2.85-1.2-2.4-3.72a24.4,24.4,0,0,1,2.4-6.28A68.61,68.61,0,0,1,272.68,365c6-4.87-2.54-13.32-8.49-8.49Z"/>
                <path  d="M364.05,442.28c-12.83,7-14.81,23.26-6,34.37,9,11.36,25.48,13.4,38.74,9.81,13.95-3.78,25.66-14.94,27.85-29.54,2-13.28-5.21-27.24-18.08-32-8.49-3.13-15.93-.08-23.17,4.46l-24.89,15.65c-6.52,4.11-.5,14.49,6.06,10.37L383,443.81c4.78-3,11.28-9,17.33-8.18,12.27,1.59,15,17.39,10.11,26.47-5.8,10.81-19.31,15.62-31.05,13.57-5.45-.95-10.9-3.67-13.53-8.77-2.83-5.49-1.19-11.28,4.25-14.26,6.77-3.71.72-14.08-6.06-10.36Z"/>
                <path  d="M289.19,194c-19.47,6.15-41.16,15-45.88,37.27-4.21,19.92,7.41,39.36,28.09,42.33,45.88,6.61,63.66-55.14,39.64-86.47-13.28-17.32-40.76-21.21-54.58-2.33-4.57,6.24,5.84,12.24,10.36,6.06,8.44-11.53,26-6.92,33.87,2.43C310.37,204.69,312,222.2,308,236.2S292.9,262,277.55,262.05c-14.3.09-24.51-10.76-23.06-25.11,1.95-19.44,21.91-26.36,37.89-31.41,7.34-2.31,4.21-13.9-3.19-11.57Z"/>
                <path d="M186.17,35.2c-7.1,11.86-15.15,33.11-1.69,43.48,12,9.26,31.73,8.1,44.26.62s20.37-22.29,18-36.9a32,32,0,0,0-29.4-26.88c-19.17-1.22-33.68,13-44.31,27.14-4.65,6.17,5.77,12.16,10.36,6.06,8-10.62,19.4-22.46,33.95-21.2,13.23,1.14,20.62,15.15,16.77,27.21C229.86,68,215.78,74.51,202.6,72.87c-6.52-.82-13.64-2.88-13.28-10.6.34-7.38,3.47-14.77,7.21-21,4-6.64-6.39-12.68-10.36-6Z"/>
                <path d="M111.11,184.61c-1.86,16.63.44,35.24,13.61,47,12.52,11.19,29.66,11.15,43.22,1.69,13.79-9.61,23.27-25.33,26.94-41.6,3.23-14.31,2.69-32.46-9.79-42.39-7.73-6.15-18-7-27.56-6.45a88.59,88.59,0,0,0-31,7.74c-7,3.12-1,13.47,6.06,10.36a72.66,72.66,0,0,1,28.53-6.23c7.57-.08,15.59.92,19.92,7.89,7.89,12.7,2.27,31.54-4.53,43.42-6.92,12.09-21.61,27.39-37,21.06-16.54-6.77-18.08-27.3-16.38-42.5.85-7.67-11.15-7.61-12,0Z"/>
                <path d="M195.49,299l-1.67,3.3a6.43,6.43,0,0,0-.6,4.63,6,6,0,0,0,11,1.43l1.66-3.3a6.36,6.36,0,0,0,.6-4.62,6,6,0,0,0-7.38-4.2,6,6,0,0,0-3.58,2.76Z"/>
                <path d="M181.56,380.67a132.43,132.43,0,0,0,57.94,45.09c2.94,1.18,6.69-1.31,7.38-4.19,7.86-33.07-19.48-61.28-47.13-74.39-12.09-5.73-29-11.51-40.08-.47-5.29,5.29-7.37,13-9.09,20.08a129.69,129.69,0,0,0-3.51,27.75c-.17,7.72,11.83,7.72,12,0a113.55,113.55,0,0,1,2.26-20.88c1.15-5.55,2.25-12.79,5.87-17.39,7.2-9.15,23.29-.58,31,3.54,20.66,11.08,43.26,32.75,37.12,58.57l7.38-4.19a117.78,117.78,0,0,1-50.77-39.57c-4.5-6.18-14.92-.2-10.36,6.05Z"/>
                <path d="M129,503.44c-11.5,8.36-23.83,18-26.94,32.74-2.82,13.4,3.23,26.6,16,32.14s29,1.33,37.86-9.41,7.54-26.77-.69-37.54c-4.63-6-15-.08-10.36,6.06,5.09,6.66,7.7,16.37,2,23.57s-17.17,10.12-25.39,5.72-9.88-13.67-6.54-21.83c3.75-9.18,12.33-15.45,20.08-21.09,6.19-4.5.21-14.91-6-10.36Z"/>
                <path d="M428.44,303.58a90.76,90.76,0,0,0-2.24,45.25c1.18,5.74,3.38,13,10.36,13.53,6.4.48,10.78-7,13.69-11.66,7.23-11.64,10.29-25.27,11.9-38.72,1.34-11.2,2.27-29.18-9.52-35.67-7.86-4.32-14.41,2.91-19.27,8.29A102.14,102.14,0,0,0,419,305c-3.64,6.8,6.71,12.87,10.36,6.06a90.13,90.13,0,0,1,10.72-16c1.56-1.82,3.79-5.23,5.94-6.32,2.67-1.35.24-1.83,2.26,0,.84.76,1.21,2.53,1.52,3.54,3.21,10.37.27,23.76-2.38,34a67.89,67.89,0,0,1-6,15.65c-1.38,2.52-3.54,4.79-4.8,7.32l-1,1.22q4.39,2.3,3.34.09c.18-.92-.8-3-1-4A78.21,78.21,0,0,1,440,306.77c2.29-7.4-9.29-10.56-11.57-3.19Z"/>
                <g id="Layer_15" data-name="Layer 15">
                    <path  d="M184.51,283.15c-4.06,9.29-10.66,30.15,3.21,34.5,11.9,3.73,27.61.9,36.64-7.79,8.15-7.84,12.85-20.36,12.67-31.61-.08-5.35-1.36-10.9-4.82-15.11-4.42-5.38-10.88-6.45-17.5-6.54-7.72-.11-7.73,11.89,0,12,3.55,0,7.15-.1,9,3.36,1.44,2.62,1.45,6,1.21,8.94a34.16,34.16,0,0,1-7.11,18.39c-5,6.12-12.2,7.71-19.83,7.53-1.09,0-5.7.35-6.43-.51-.28-.35,0-3,.09-3.68a38.66,38.66,0,0,1,3.2-13.42c3.07-7-7.26-13.12-10.36-6.06Z"/>
                    <path  d="M373,184.76a181.71,181.71,0,0,0-48.85,23.55c-6.38,4.39-13.43,9.17-16.71,16.48a15.7,15.7,0,0,0,5.35,19.67c6.24,4.47,14.65,5.46,22.07,6.27A103.48,103.48,0,0,0,405.26,232c14.53-10.69,25.67-29.4,12.83-45.91-4.74-6.09-13.17,2.46-8.49,8.48,4.92,6.33,1.32,15.22-3.25,20.54-5.23,6.08-12.46,10.56-19.52,14.19a91.75,91.75,0,0,1-45.54,9.9A82.2,82.2,0,0,1,330,238c-3.35-.61-8-1.14-10.67-3.51-5.6-4.95,5.84-12.32,9.39-14.82a170.39,170.39,0,0,1,47.43-23.35c7.35-2.3,4.21-13.89-3.19-11.57Z"/>
                </g>
            </g>
            <ellipse cx="297.73" cy="273.45" rx="24.38" ry="51.65"/>
        </g>
        <circle cx="327.59" cy="388.85" r="21.59"/>
    </g>
    <g id="SquareShapes">
        <rect class="cls-12" x="153.51" y="85.44" width="152.49" height="81.82"/>
        <rect class="cls-4" x="419.63" y="543.29" width="92.56" height="92.56"/>
        <rect class="cls-12" x="376.65" y="232.55" width="71.9" height="71.9"/>
        <g id="Freedraw">
            <path class="cls-3" d="M481.63,222.69a393.26,393.26,0,0,0-79.93,13c-4.1,1.11-5.09,5.33-3.59,8.81l3,7,5.18-9a197.38,197.38,0,0,0-48.19,2c-6.06,1-15.28,1.36-19.43,6.55-4.62,5.78-.78,15,.66,21.39a530,530,0,0,0,29.48,90.88c1.56,3.67,7,3.63,9.42,1.22,10.22-10,28.28-7.18,41.19-6.48,16.29.88,32.81,2,48.87-1.68a84.91,84.91,0,0,0,22.59-9c6.31-3.55,12.82-7.08,13.78-14.91s.66-16.06.48-23.91a137.13,137.13,0,0,0-2.52-24.25c-3.34-16.28-15.7-33.93-9.81-50.81,2.56-7.3-9-10.44-11.57-3.19-5.1,14.6.8,29.06,5.66,42.83,5.45,15.47,6.54,31.05,6.22,47.35,0,2.91.84,8.47-.41,11.08-1.17,2.43-5.56,4.13-7.89,5.45a74.06,74.06,0,0,1-23.26,8.47c-16,2.9-32.51.91-48.59.21-15-.66-31.73-.93-43.22,10.31l9.42,1.22a513.83,513.83,0,0,1-25.9-77.94c-1.54-6.27-2.24-13.22-4.35-19.28.19-1.8-.06-2.31-.76-1.53-.77.55-.45.54,1,0a27.91,27.91,0,0,0,4.66-1q5.15-1.06,10.34-1.83a185.32,185.32,0,0,1,42.17-1.12c5.05.42,6.79-5.28,5.18-9l-3-7-3.58,8.82a377,377,0,0,1,76.74-12.56c7.67-.46,7.72-12.46,0-12Z"/>
            <path class="cls-3" d="M379,478q-45.24-6-90.89-7.77a6.06,6.06,0,0,0-6,6,242.4,242.4,0,0,1-6.66,66.39c-.71,2.91.91,6.83,4.19,7.38a79.76,79.76,0,0,0,23.84.73c7.61-1,14.62-3.39,21.93-5.57,15.6-4.63,30.64,1.7,46.4,1.21a6.08,6.08,0,0,0,6-6q-.74-26.67.69-53.32c.41-7.72-11.59-7.7-12,0q-1.42,26.64-.69,53.32l6-6c-15,.46-29.38-5.22-44.36-2.13-7.13,1.47-13.83,4.47-21,5.93a64.78,64.78,0,0,1-23.64.26l4.19,7.38a254.29,254.29,0,0,0,7.09-69.58l-6,6Q333.73,484,379,490c3.24.43,6-3.07,6-6,0-3.59-2.8-5.58-6-6Z"/>
        </g>
        <g id="BluishRectangles">
            <rect class="cls-7" x="111.36" y="345.77" width="50.23" height="50.23"/>
            <rect class="cls-7" x="353.51" y="239.16" width="50.23" height="50.23"/>
            <rect class="cls-7" x="478.31" y="370.88" width="50.23" height="50.23"/>
        </g>
    </g>
</svg>
B D
  • 303
  • 3
  • 12

1 Answers1

0

First, note that in your example, the element <g> which has the attribute id with the RoundShapes attribute value, only has child tags circle, ellipse and path, but not poly, rect or polygon. So the answer will only relate to the first three; you'll have to modify it to fit your actual xml.

Second, I would use a more robust xml parser, namely lxml. So:

from lxml import etree
doc = etree.XML(svg_text)

target_tags = ['circle', 'ellipse','path']

#you have to declare namespaces
ns = {'xx': 'http://www.w3.org/2000/svg'}

for target in target_tags:
    exp = f'//xx:g[@id="RoundShapes"]//xx:{target}'
    to_modify = doc.xpath(exp,namespaces=ns)
    for mod in to_modify:
        del mod.attrib['class']
print(etree.tostring(doc).decode())

The output should be your expected output.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Thanks Jack - I really didn't understand what you meant by lxml. I tried to install it on my Mac (OS Monterey) can't seem to get it to work. I reviewed several sources including https://stackoverflow.com/questions/19548011/cannot-install-lxml-on-mac-os-x-10-9 I'll keep trying to figure it out, thanks for your guidance – B D Feb 01 '22 at 11:52
  • @BD I'm afraid I don't no anything about the Mac, but lxml has been around forever, so there shouldn't be an issue. [Try starting from here](https://lxml.de/installation.html). – Jack Fleeting Feb 01 '22 at 12:23
  • I figured out how to run lxml on MacOS 12.2 -- I have to run python2 rather than python3. Now I get line 134 exp = f'//xx:g[@id="RoundShapes"]//xx:{target}' ^ SyntaxError: invalid syntax – B D Feb 09 '22 at 04:05
  • And I figured out that the f requires python3. I can't figure out how to install lxml for python3 on macos 12.2. Can this code be modified to work with python 2? – B D Feb 09 '22 at 05:10
  • @BD Probably, but I don't have python 2 installed, so I can't test it. Try using `exp = '//xx:g[@id="RoundShapes"]//xx:'+target` instead and see if it works. – Jack Fleeting Feb 09 '22 at 23:12