0

I am trying to replace the first six characters of a sub-tag <color></color> value in a KML file:

Current:

<Style id='1'>
        <LineStyle>
                <color>9900e6ff</color>
                <width>2</width>
        </LineStyle>

Expected:

<Style id='1'>
        <LineStyle>
                <color>ffffffff</color>
                <width>2</width>
        </LineStyle>

Attempted sed Code:

sed -i s/<LineStyle><color>[0-9]*</color></LineStyle>/<LineStyle><color>ffffff[0-9][a-z]</color></LineStyle>/g' file.kml
arnpry
  • 1,103
  • 1
  • 10
  • 26
  • 2
    Please post valid XML. – Cyrus Jun 27 '22 at 16:32
  • 2
    Please [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858). I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Jun 27 '22 at 16:32
  • 1
    See: [Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms](https://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la) – Cyrus Jun 27 '22 at 18:45
  • Thank you for your comment, Cyrus! This is sample code from a KML file. You can learn more about them here --> https://developers.google.com/kml – arnpry Jun 28 '22 at 09:04

1 Answers1

1

I recommend as other commentators not to use regex for element search because of possible unpredictable behavior & it is better to prefer any package with xpath such as lxml or xml.

from lxml import etree as ET

some_xml = """
<Style id='1'>
    <LineStyle>
        <color>9900e6ff</color>
        <width>2</width>
    </LineStyle>
</Style>
"""

some_xml = ET.fromstring(some_xml)
if len(result := some_xml.xpath("descendant::color")) == 1:
    result[0].text = "ffffffff"
    print(ET.tostring(some_xml, encoding="unicode"))
Vovin
  • 720
  • 4
  • 16