在xml Python中替换标记数据

在xml Python中替换标记数据,python,xml,svg,Python,Xml,Svg,我将SVG解析为XML: <?xml version='1.0' encoding='ASCII' standalone='yes'?> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4000 4000"> <defs> <style>.cls-1{fill:#755841;}.cls-2{fill:#f7c394;}.cls-3{fill:#9b7657;}.cl

我将SVG解析为XML:

<?xml version='1.0' encoding='ASCII' standalone='yes'?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4000 4000">
    <defs>
        <style>.cls-1{fill:#755841;}.cls-2{fill:#f7c394;}.cls-3{fill:#9b7657;}.cls-4{fill:#7f6145;}.cls-5{fill:#ffc9a1;}.cls-6{fill:#ffcd98;}.cls-7{fill:#3d0c0c;}.cls-8{fill:#fff;}.cls-9{fill:#313cc4;}</style>
    </defs>
    <title>expanded</title>
    <g id="expanded">
        <path class="cls-1" d="M1685.94,3103.49a46.66,46.66,0,0,1-46.27-41.15c-7.68-64.58,15.14-139.41,64.25-210.71,43-62.41,102.73-116.13,159.81-143.72a46.66,46.66,0,1,1,40.6,84c-42.51,20.54-89.86,63.7-123.56,112.64-35.71,51.83-53.36,105.32-48.44,146.75a46.65,46.65,0,0,1-40.82,51.84A47.68,47.68,0,0,1,1685.94,3103.49Z"/>
        <path class="cls-2" d="M1698.65,3195a54.69,54.69,0,0,1-19.13-3.46c-16.08-6-29-18.39-38.52-36.88a116,116,0,0,1-11.55-36.67c-4.12-30.82,6.37-57.71,28.78-73.76a54.89,54.89,0,0,1,83.92,62.36,54.91,54.91,0,0,1-43.5,88.41Z"/>
        <path class="cls-3" d="M2254,3205.38a158.15,158.15,0,0,1-22.69-1.75c-39-5.71-88.52-24.82-151.33-58.42a18.93,18.93,0,1,1,17.86-33.39c129.79,69.42,172.74,59.15,186.78,46.84,10.58-9.26,9.64-25.54,6.89-55.92-3.21-35.45-7.19-79.56,15.19-130.25,17-38.39,46.73-75,81.7-100.39,40.59-29.51,83.81-41.73,125-35.36,26.53,4.1,53.15,16,74.94,33.57,25,20.14,41.51,45.85,47.69,74.35,5.06,23.34,3.71,51.06-3.62,74.15-9.4,29.64-28.13,50.84-52.75,59.68a78.83,78.83,0,0,1-26.81,4.62c-37.89,0-69.36-25.67-73.53-29.22a18.94,18.94,0,0,1,24.56-28.84c.33.28,33.72,28.3,63,17.8,17-6.1,25.39-22.67,29.45-35.49,5.33-16.81,6.37-37.76,2.7-54.68-5.46-25.19-21.73-42.65-34.42-52.86-16.45-13.25-37.22-22.6-57-25.65-67.11-10.39-138.54,50.89-166.24,113.62-18.43,41.73-15.08,78.81-12.12,111.54,3,33.64,5.92,65.43-19.65,87.82C2295.74,3199.28,2277.1,3205.38,2254,3205.38Z"/>
        <path class="cls-1" d="M1877.5,3396.06c-.93,0-1.85,0-2.79,0a68.61,68.61,0,0,1-65.82-71.3c3.54-88.69,25.64-150.48,41.77-195.59,4.87-13.62,9.07-25.38,12.13-35.94a68.61,68.61,0,1,1,131.83,38.09c-4.24,14.66-9.34,28.94-14.75,44.05-14.6,40.85-31.16,87.15-33.86,154.87A68.61,68.61,0,0,1,1877.5,3396.06Z"/>
        <path class="cls-2" d="M1761.27,3431A54.9,54.9,0,0,1,1739,3325.93c71.38-31.61,167.74-20.26,211.71-5.67a54.89,54.89,0,1,1-34.57,104.2c-26.37-8.75-93.26-15.61-132.68,1.85A54.74,54.74,0,0,1,1761.27,3431Z"/>
        <path class="cls-1" d="M2126.63,3397.47a68.62,68.62,0,0,1-68.44-64.68c-3.88-67.66-21.24-113.66-36.56-154.25-5.67-15-11-29.21-15.51-43.79a68.61,68.61,0,1,1,131.15-40.38c3.23,10.5,7.64,22.19,12.75,35.72,16.92,44.82,40.09,106.22,45.18,194.83a68.63,68.63,0,0,1-64.57,72.44C2129.29,3397.43,2128,3397.47,2126.63,3397.47Z"/>
        <path class="cls-2" d="M2243.71,3430.39a54.73,54.73,0,0,1-21.32-4.33c-39.72-16.77-106.49-8.74-132.7.47A54.89,54.89,0,0,1,2053.31,3323c43.71-15.35,139.86-28.39,211.78,2a54.9,54.9,0,0,1-21.38,105.47Z"/>
        <path class="cls-1" d="M2319.74,3100c-1.57,0-3.15-.08-4.75-.24a46.65,46.65,0,0,1-41.72-51.12c4.2-41.51-14.39-94.68-51-145.89-34.55-48.34-82.65-90.66-125.52-110.46a46.66,46.66,0,0,1,39.14-84.72c57.55,26.59,118.22,79.27,162.29,140.92,50.35,70.42,74.47,144.84,67.92,209.54A46.68,46.68,0,0,1,2319.74,3100Z"/>
        <path class="cls-2" d="M2308.5,3191.69a54.91,54.91,0,0,1-44.08-87.65,54.89,54.89,0,0,1,82.82-63.82c22.68,15.67,33.64,42.36,30.06,73.25-3.1,26.72-17.74,62.24-48.78,74.42A54.75,54.75,0,0,1,2308.5,3191.69Z"/>
    </g>
</svg>
进入

现在我想用中的字符串替换这个字符串,但是我不能

我正在使用xml.dom.minidom并尝试执行以下操作:

  result = re.sub('(cls-)([0-9]+)', func, style_as_string)
  style_as_string = doc.getElementsByTagName('style')[0].childNodes[0].data = result # doesn't work
    doc.getElementsByTagName('style')[0].appendChild(result) # doesn't work
将样式指定为字符串时,请尝试不选择.data。这样以后就可以设置数据了

例如

import re
from xml.dom.minidom import parseString

xml = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4000 4000">
    <defs>
        <style>.cls-1{fill:#755841;}.cls-2{fill:#f7c394;}.cls-3{fill:#9b7657;}.cls-4{fill:#7f6145;}.cls-5{fill:#ffc9a1;}.cls-6{fill:#ffcd98;}.cls-7{fill:#3d0c0c;}.cls-8{fill:#fff;}.cls-9{fill:#313cc4;}</style>
    </defs>
    <title>expanded</title>
    <g id="expanded">
        <path class="cls-1" d="M1685.94,3103.49a46.66,46.66,0,0,1-46.27-41.15c-7.68-64.58,15.14-139.41,64.25-210.71,43-62.41,102.73-116.13,159.81-143.72a46.66,46.66,0,1,1,40.6,84c-42.51,20.54-89.86,63.7-123.56,112.64-35.71,51.83-53.36,105.32-48.44,146.75a46.65,46.65,0,0,1-40.82,51.84A47.68,47.68,0,0,1,1685.94,3103.49Z"/>
        <path class="cls-2" d="M1698.65,3195a54.69,54.69,0,0,1-19.13-3.46c-16.08-6-29-18.39-38.52-36.88a116,116,0,0,1-11.55-36.67c-4.12-30.82,6.37-57.71,28.78-73.76a54.89,54.89,0,0,1,83.92,62.36,54.91,54.91,0,0,1-43.5,88.41Z"/>
        <path class="cls-3" d="M2254,3205.38a158.15,158.15,0,0,1-22.69-1.75c-39-5.71-88.52-24.82-151.33-58.42a18.93,18.93,0,1,1,17.86-33.39c129.79,69.42,172.74,59.15,186.78,46.84,10.58-9.26,9.64-25.54,6.89-55.92-3.21-35.45-7.19-79.56,15.19-130.25,17-38.39,46.73-75,81.7-100.39,40.59-29.51,83.81-41.73,125-35.36,26.53,4.1,53.15,16,74.94,33.57,25,20.14,41.51,45.85,47.69,74.35,5.06,23.34,3.71,51.06-3.62,74.15-9.4,29.64-28.13,50.84-52.75,59.68a78.83,78.83,0,0,1-26.81,4.62c-37.89,0-69.36-25.67-73.53-29.22a18.94,18.94,0,0,1,24.56-28.84c.33.28,33.72,28.3,63,17.8,17-6.1,25.39-22.67,29.45-35.49,5.33-16.81,6.37-37.76,2.7-54.68-5.46-25.19-21.73-42.65-34.42-52.86-16.45-13.25-37.22-22.6-57-25.65-67.11-10.39-138.54,50.89-166.24,113.62-18.43,41.73-15.08,78.81-12.12,111.54,3,33.64,5.92,65.43-19.65,87.82C2295.74,3199.28,2277.1,3205.38,2254,3205.38Z"/>
        <path class="cls-1" d="M1877.5,3396.06c-.93,0-1.85,0-2.79,0a68.61,68.61,0,0,1-65.82-71.3c3.54-88.69,25.64-150.48,41.77-195.59,4.87-13.62,9.07-25.38,12.13-35.94a68.61,68.61,0,1,1,131.83,38.09c-4.24,14.66-9.34,28.94-14.75,44.05-14.6,40.85-31.16,87.15-33.86,154.87A68.61,68.61,0,0,1,1877.5,3396.06Z"/>
        <path class="cls-2" d="M1761.27,3431A54.9,54.9,0,0,1,1739,3325.93c71.38-31.61,167.74-20.26,211.71-5.67a54.89,54.89,0,1,1-34.57,104.2c-26.37-8.75-93.26-15.61-132.68,1.85A54.74,54.74,0,0,1,1761.27,3431Z"/>
        <path class="cls-1" d="M2126.63,3397.47a68.62,68.62,0,0,1-68.44-64.68c-3.88-67.66-21.24-113.66-36.56-154.25-5.67-15-11-29.21-15.51-43.79a68.61,68.61,0,1,1,131.15-40.38c3.23,10.5,7.64,22.19,12.75,35.72,16.92,44.82,40.09,106.22,45.18,194.83a68.63,68.63,0,0,1-64.57,72.44C2129.29,3397.43,2128,3397.47,2126.63,3397.47Z"/>
        <path class="cls-2" d="M2243.71,3430.39a54.73,54.73,0,0,1-21.32-4.33c-39.72-16.77-106.49-8.74-132.7.47A54.89,54.89,0,0,1,2053.31,3323c43.71-15.35,139.86-28.39,211.78,2a54.9,54.9,0,0,1-21.38,105.47Z"/>
        <path class="cls-1" d="M2319.74,3100c-1.57,0-3.15-.08-4.75-.24a46.65,46.65,0,0,1-41.72-51.12c4.2-41.51-14.39-94.68-51-145.89-34.55-48.34-82.65-90.66-125.52-110.46a46.66,46.66,0,0,1,39.14-84.72c57.55,26.59,118.22,79.27,162.29,140.92,50.35,70.42,74.47,144.84,67.92,209.54A46.68,46.68,0,0,1,2319.74,3100Z"/>
        <path class="cls-2" d="M2308.5,3191.69a54.91,54.91,0,0,1-44.08-87.65,54.89,54.89,0,0,1,82.82-63.82c22.68,15.67,33.64,42.36,30.06,73.25-3.1,26.72-17.74,62.24-48.78,74.42A54.75,54.75,0,0,1,2308.5,3191.69Z"/>
    </g>
</svg>
"""


def func(matches):
    return "{}{}".format(matches.group(1), int(matches.group(2))+11)


doc = parseString(xml)

style_as_string = doc.getElementsByTagName('style')[0].childNodes[0]

result = re.sub('(cls-)([0-9]+)', func, style_as_string.data)

style_as_string.data = result

print doc.toxml()

可能会让您大吃一惊的是,该样式位于默认名称空间中http://www.w3.org/2000/svgI 别这么想。我使用doc.getElementsByTagName'style'[0].childNodes[0].data来获取数据。我只是无法覆盖数据。哦,你的代码有一条注释说获取数据也不起作用。我不使用minidom,所以我不确定,但谷歌快速搜索发现了这个问题/答案,建议使用GetElementsByTagnames。我正在毫无问题地查找元素,并以字符串形式获取它。我现在想做相反的事情。将字符串转换为元素
  result = re.sub('(cls-)([0-9]+)', func, style_as_string)
  style_as_string = doc.getElementsByTagName('style')[0].childNodes[0].data = result # doesn't work
    doc.getElementsByTagName('style')[0].appendChild(result) # doesn't work
import re
from xml.dom.minidom import parseString

xml = """
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4000 4000">
    <defs>
        <style>.cls-1{fill:#755841;}.cls-2{fill:#f7c394;}.cls-3{fill:#9b7657;}.cls-4{fill:#7f6145;}.cls-5{fill:#ffc9a1;}.cls-6{fill:#ffcd98;}.cls-7{fill:#3d0c0c;}.cls-8{fill:#fff;}.cls-9{fill:#313cc4;}</style>
    </defs>
    <title>expanded</title>
    <g id="expanded">
        <path class="cls-1" d="M1685.94,3103.49a46.66,46.66,0,0,1-46.27-41.15c-7.68-64.58,15.14-139.41,64.25-210.71,43-62.41,102.73-116.13,159.81-143.72a46.66,46.66,0,1,1,40.6,84c-42.51,20.54-89.86,63.7-123.56,112.64-35.71,51.83-53.36,105.32-48.44,146.75a46.65,46.65,0,0,1-40.82,51.84A47.68,47.68,0,0,1,1685.94,3103.49Z"/>
        <path class="cls-2" d="M1698.65,3195a54.69,54.69,0,0,1-19.13-3.46c-16.08-6-29-18.39-38.52-36.88a116,116,0,0,1-11.55-36.67c-4.12-30.82,6.37-57.71,28.78-73.76a54.89,54.89,0,0,1,83.92,62.36,54.91,54.91,0,0,1-43.5,88.41Z"/>
        <path class="cls-3" d="M2254,3205.38a158.15,158.15,0,0,1-22.69-1.75c-39-5.71-88.52-24.82-151.33-58.42a18.93,18.93,0,1,1,17.86-33.39c129.79,69.42,172.74,59.15,186.78,46.84,10.58-9.26,9.64-25.54,6.89-55.92-3.21-35.45-7.19-79.56,15.19-130.25,17-38.39,46.73-75,81.7-100.39,40.59-29.51,83.81-41.73,125-35.36,26.53,4.1,53.15,16,74.94,33.57,25,20.14,41.51,45.85,47.69,74.35,5.06,23.34,3.71,51.06-3.62,74.15-9.4,29.64-28.13,50.84-52.75,59.68a78.83,78.83,0,0,1-26.81,4.62c-37.89,0-69.36-25.67-73.53-29.22a18.94,18.94,0,0,1,24.56-28.84c.33.28,33.72,28.3,63,17.8,17-6.1,25.39-22.67,29.45-35.49,5.33-16.81,6.37-37.76,2.7-54.68-5.46-25.19-21.73-42.65-34.42-52.86-16.45-13.25-37.22-22.6-57-25.65-67.11-10.39-138.54,50.89-166.24,113.62-18.43,41.73-15.08,78.81-12.12,111.54,3,33.64,5.92,65.43-19.65,87.82C2295.74,3199.28,2277.1,3205.38,2254,3205.38Z"/>
        <path class="cls-1" d="M1877.5,3396.06c-.93,0-1.85,0-2.79,0a68.61,68.61,0,0,1-65.82-71.3c3.54-88.69,25.64-150.48,41.77-195.59,4.87-13.62,9.07-25.38,12.13-35.94a68.61,68.61,0,1,1,131.83,38.09c-4.24,14.66-9.34,28.94-14.75,44.05-14.6,40.85-31.16,87.15-33.86,154.87A68.61,68.61,0,0,1,1877.5,3396.06Z"/>
        <path class="cls-2" d="M1761.27,3431A54.9,54.9,0,0,1,1739,3325.93c71.38-31.61,167.74-20.26,211.71-5.67a54.89,54.89,0,1,1-34.57,104.2c-26.37-8.75-93.26-15.61-132.68,1.85A54.74,54.74,0,0,1,1761.27,3431Z"/>
        <path class="cls-1" d="M2126.63,3397.47a68.62,68.62,0,0,1-68.44-64.68c-3.88-67.66-21.24-113.66-36.56-154.25-5.67-15-11-29.21-15.51-43.79a68.61,68.61,0,1,1,131.15-40.38c3.23,10.5,7.64,22.19,12.75,35.72,16.92,44.82,40.09,106.22,45.18,194.83a68.63,68.63,0,0,1-64.57,72.44C2129.29,3397.43,2128,3397.47,2126.63,3397.47Z"/>
        <path class="cls-2" d="M2243.71,3430.39a54.73,54.73,0,0,1-21.32-4.33c-39.72-16.77-106.49-8.74-132.7.47A54.89,54.89,0,0,1,2053.31,3323c43.71-15.35,139.86-28.39,211.78,2a54.9,54.9,0,0,1-21.38,105.47Z"/>
        <path class="cls-1" d="M2319.74,3100c-1.57,0-3.15-.08-4.75-.24a46.65,46.65,0,0,1-41.72-51.12c4.2-41.51-14.39-94.68-51-145.89-34.55-48.34-82.65-90.66-125.52-110.46a46.66,46.66,0,0,1,39.14-84.72c57.55,26.59,118.22,79.27,162.29,140.92,50.35,70.42,74.47,144.84,67.92,209.54A46.68,46.68,0,0,1,2319.74,3100Z"/>
        <path class="cls-2" d="M2308.5,3191.69a54.91,54.91,0,0,1-44.08-87.65,54.89,54.89,0,0,1,82.82-63.82c22.68,15.67,33.64,42.36,30.06,73.25-3.1,26.72-17.74,62.24-48.78,74.42A54.75,54.75,0,0,1,2308.5,3191.69Z"/>
    </g>
</svg>
"""


def func(matches):
    return "{}{}".format(matches.group(1), int(matches.group(2))+11)


doc = parseString(xml)

style_as_string = doc.getElementsByTagName('style')[0].childNodes[0]

result = re.sub('(cls-)([0-9]+)', func, style_as_string.data)

style_as_string.data = result

print doc.toxml()
<?xml version="1.0" ?><svg viewBox="0 0 4000 4000" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <style>.cls-12{fill:#755841;}.cls-13{fill:#f7c394;}.cls-14{fill:#9b7657;}.cls-15{fill:#7f6145;}.cls-16{fill:#ffc9a1;}.cls-17{fill:#ffcd98;}.cls-18{fill:#3d0c0c;}.cls-19{fill:#fff;}.cls-20{fill:#313cc4;}</style>
    </defs>
    <title>expanded</title>
    <g id="expanded">
        <path class="cls-1" d="M1685.94,3103.49a46.66,46.66,0,0,1-46.27-41.15c-7.68-64.58,15.14-139.41,64.25-210.71,43-62.41,102.73-116.13,159.81-143.72a46.66,46.66,0,1,1,40.6,84c-42.51,20.54-89.86,63.7-123.56,112.64-35.71,51.83-53.36,105.32-48.44,146.75a46.65,46.65,0,0,1-40.82,51.84A47.68,47.68,0,0,1,1685.94,3103.49Z"/>
        <path class="cls-2" d="M1698.65,3195a54.69,54.69,0,0,1-19.13-3.46c-16.08-6-29-18.39-38.52-36.88a116,116,0,0,1-11.55-36.67c-4.12-30.82,6.37-57.71,28.78-73.76a54.89,54.89,0,0,1,83.92,62.36,54.91,54.91,0,0,1-43.5,88.41Z"/>
        <path class="cls-3" d="M2254,3205.38a158.15,158.15,0,0,1-22.69-1.75c-39-5.71-88.52-24.82-151.33-58.42a18.93,18.93,0,1,1,17.86-33.39c129.79,69.42,172.74,59.15,186.78,46.84,10.58-9.26,9.64-25.54,6.89-55.92-3.21-35.45-7.19-79.56,15.19-130.25,17-38.39,46.73-75,81.7-100.39,40.59-29.51,83.81-41.73,125-35.36,26.53,4.1,53.15,16,74.94,33.57,25,20.14,41.51,45.85,47.69,74.35,5.06,23.34,3.71,51.06-3.62,74.15-9.4,29.64-28.13,50.84-52.75,59.68a78.83,78.83,0,0,1-26.81,4.62c-37.89,0-69.36-25.67-73.53-29.22a18.94,18.94,0,0,1,24.56-28.84c.33.28,33.72,28.3,63,17.8,17-6.1,25.39-22.67,29.45-35.49,5.33-16.81,6.37-37.76,2.7-54.68-5.46-25.19-21.73-42.65-34.42-52.86-16.45-13.25-37.22-22.6-57-25.65-67.11-10.39-138.54,50.89-166.24,113.62-18.43,41.73-15.08,78.81-12.12,111.54,3,33.64,5.92,65.43-19.65,87.82C2295.74,3199.28,2277.1,3205.38,2254,3205.38Z"/>
        <path class="cls-1" d="M1877.5,3396.06c-.93,0-1.85,0-2.79,0a68.61,68.61,0,0,1-65.82-71.3c3.54-88.69,25.64-150.48,41.77-195.59,4.87-13.62,9.07-25.38,12.13-35.94a68.61,68.61,0,1,1,131.83,38.09c-4.24,14.66-9.34,28.94-14.75,44.05-14.6,40.85-31.16,87.15-33.86,154.87A68.61,68.61,0,0,1,1877.5,3396.06Z"/>
        <path class="cls-2" d="M1761.27,3431A54.9,54.9,0,0,1,1739,3325.93c71.38-31.61,167.74-20.26,211.71-5.67a54.89,54.89,0,1,1-34.57,104.2c-26.37-8.75-93.26-15.61-132.68,1.85A54.74,54.74,0,0,1,1761.27,3431Z"/>
        <path class="cls-1" d="M2126.63,3397.47a68.62,68.62,0,0,1-68.44-64.68c-3.88-67.66-21.24-113.66-36.56-154.25-5.67-15-11-29.21-15.51-43.79a68.61,68.61,0,1,1,131.15-40.38c3.23,10.5,7.64,22.19,12.75,35.72,16.92,44.82,40.09,106.22,45.18,194.83a68.63,68.63,0,0,1-64.57,72.44C2129.29,3397.43,2128,3397.47,2126.63,3397.47Z"/>
        <path class="cls-2" d="M2243.71,3430.39a54.73,54.73,0,0,1-21.32-4.33c-39.72-16.77-106.49-8.74-132.7.47A54.89,54.89,0,0,1,2053.31,3323c43.71-15.35,139.86-28.39,211.78,2a54.9,54.9,0,0,1-21.38,105.47Z"/>
        <path class="cls-1" d="M2319.74,3100c-1.57,0-3.15-.08-4.75-.24a46.65,46.65,0,0,1-41.72-51.12c4.2-41.51-14.39-94.68-51-145.89-34.55-48.34-82.65-90.66-125.52-110.46a46.66,46.66,0,0,1,39.14-84.72c57.55,26.59,118.22,79.27,162.29,140.92,50.35,70.42,74.47,144.84,67.92,209.54A46.68,46.68,0,0,1,2319.74,3100Z"/>
        <path class="cls-2" d="M2308.5,3191.69a54.91,54.91,0,0,1-44.08-87.65,54.89,54.89,0,0,1,82.82-63.82c22.68,15.67,33.64,42.36,30.06,73.25-3.1,26.72-17.74,62.24-48.78,74.42A54.75,54.75,0,0,1,2308.5,3191.69Z"/>
    </g>
</svg>