如何在SVG文件中用Python根据'id'字段查找元素

3 投票
3 回答
6746 浏览
提问于 2025-04-15 19:53

下面是一个.svg文件的片段(其实是xml格式的):

   <text
       xml:space="preserve"
       style="font-size:14.19380379px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:125%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;font-family:DejaVu Sans Mono;-inkscape-font-specification:DejaVu Sans Mono"
       x="109.38555"
       y="407.02847"
       id="libcode-00"
       sodipodi:linespacing="125%"
       inkscape:label="#text4638"><tspan
         sodipodi:role="line"
         id="tspan4640"
         x="109.38555"
         y="407.02847">12345678</tspan></text>

我正在学习Python,但不知道怎么找到所有那些text元素,它们的id字段等于libcode-XX,其中XX是一个数字。

我用minidom的解析器加载了这个.svg文件,并尝试使用getElementById来查找元素。但是我得到了None的结果。

    svgTemplate = minidom.parse(svgFile)
    print svgTemplate
    print svgTemplate.getElementById('libcode-00')

根据其他StackOverflow的问题,我尝试在svgTemplate对象上使用setIdAttribute('id'),但没有成功。

总之:请给我一个聪明的方法来提取所有这些text元素,它们的id格式是libcode-XX。之后获取tspan文本并用生成的内容替换应该就没问题了。

3 个回答

0

在MattH的精彩例子基础上再补充一点,当你使用xpath并且知道命名空间时,你可以这样做:

pub_name = data.xpath('//dc:publisher/cc:Agent/dc:title',
                            namespaces=nsmap)[0].text

这样做可以直接获取你想要的元素标签的文本内容。

0

如果你把'id'换成'xml:id',这样行不行呢?

如果minidom不认识svg,它可能会把'id'这个属性当成普通属性来处理,而不是特定的ID类型。符合标准的svg实现会把svg内容中的'id'属性识别为ID类型,而一个能够加载外部DTD的xml实现也应该能正确识别,只要文件标记得当。加载外部DTD在XML中是可选的,所以解决这个问题的正确方法是让解析器能够识别svg。

关于SVG 1.1 DTD中'id'的定义:http://www.w3.org/TR/SVG11/svgdtd.html#DTD.1.4

11

抱歉,我对minidom不太熟悉。而且,我还得从一个示例的svg文档中找到命名空间声明,这样你的代码片段才能加载。

我个人使用的是lxml.etree。我建议你使用XPATH来处理你的XML文档中的部分内容。XPATH非常强大,如果你遇到困难,这里在StackOverflow上有很多帮助。

关于XPATH和etree,StackOverflow上有很多答案。我也写过几篇相关的内容。

from lxml import etree
data = """
 <svg
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:cc="http://web.resource.org/cc/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
    xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
    width="50"
    height="25"
    id="svg2"
    sodipodi:version="0.32"
    inkscape:version="0.45.1"
    version="1.0"
    sodipodi:docbase="/home/tcooksey/Projects/qt-4.4/demos/embedded/embeddedsvgviewer/files"
    sodipodi:docname="v-slider-handle.svg"
    inkscape:output_extension="org.inkscape.output.svg.inkscape">
    <text
       xml:space="preserve"
       style="font-size:14.19380379px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:125%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;font-family:DejaVu Sans Mono;-inkscape-font-specification:DejaVu Sans Mono"
       x="109.38555"
       y="407.02847"
       id="libcode-00"
       sodipodi:linespacing="125%"
       inkscape:label="#text4638"><tspan
         sodipodi:role="line"
         id="tspan4640"
         x="109.38555"
         y="407.02847">12345678</tspan></text>
    </svg>
"""

nsmap = {
    'sodipodi': 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
    'cc': 'http://web.resource.org/cc/',
    'svg': 'http://www.w3.org/2000/svg',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'xlink': 'http://www.w3.org/1999/xlink',
    'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'inkscape': 'http://www.inkscape.org/namespaces/inkscape'
    }


data = etree.XML(data)

# All svg text elements
>>> data.xpath('//svg:text',namespaces=nsmap)
[<Element {http://www.w3.org/2000/svg}text at b7cfc9dc>]
# All svg text elements with id="libcode-00"
>>> data.xpath('//svg:text[@id="libcode-00"]',namespaces=nsmap)
[<Element {http://www.w3.org/2000/svg}text at b7cfc9dc>]
# TSPAN child elements of text elements with id="libcode-00"
>>> data.xpath('//svg:text[@id="libcode-00"]/svg:tspan',namespaces=nsmap)
[<Element {http://www.w3.org/2000/svg}tspan at b7cfc964>]
# All text elements with id starting with "libcode"
>>> data.xpath('//svg:text[fn:startswith(@id,"libcode")]',namespaces=nsmap)
[<Element {http://www.w3.org/2000/svg}text at b7cfcc34>]
# Iterate text elements, access tspan child
>>> for elem in data.xpath('//svg:text[fn:startswith(@id,"libcode")]',namespaces=nsmap):
...     tp = elem.xpath('./svg:tspan',namespaces=nsmap)[0]
...     tp.text = "new text"

open("newfile.svg","w").write(etree.tostring(data))

撰写回答