【问题标题】:SVG (Python) xPath get Elements by AttributeSVG (Python) xPath 按属性获取元素
【发布时间】:2016-09-27 21:16:06
【问题描述】:

您好,我正在制作一个将 SVG 文件导入我的文档的程序。 (LXML)

    if so.funktion != "0":
        if so.funktion=="1":
            searchStr = '//svg:g[@svg-creator="popup1"]'
            selectStr = template.xpath(searchStr, namespaces=inkex.NSS)
            if selectStr != []:
                selectStr[0].set("id","Popup "+so.name)
                self.current_layer.append(selectStr[0])
            else:
                None

我的 SVG 文件:

<g
 id="p1"
 svg-creator="popup1"
 transform="translate(140,-160)">
<rect
   y="900"
   x="0"
   height="180"
   width="200"
   id="rect6309"
   style="opacity:1;fill:#000000;fill-opacity:0.7372549;stroke:none;stroke-width:0.5;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
<g
   id="b1234"
   svg-creator="button1">
  <rect
     style="opacity:1;fill:#6c6c6c;fill-opacity:1;stroke:none;stroke-width:0.5;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
     id="rect6588"
     width="126"
     height="34"
     x="59"
     y="1023" />
  <text
     sodipodi:linespacing="125%"
     id="text6770"
     y="1045.8575"
     x="91.91333"
     style="font-style:normal;font-weight:normal;font-size:50px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
     xml:space="preserve"><tspan
       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:17.5px;font-family:'Franklin Gothic Medium';-inkscape-font-specification:'Franklin Gothic Medium, ';fill:#ffffff;fill-opacity:1"
       y="1045.8575"
       x="91.91333"
       id="tspan6772"
       sodipodi:role="line">button1</tspan></text>
</g>
<g
   id="b83217"
   transform="translate(0,-50)"
   svg-creator="button2">
  <rect
     style="opacity:1;fill:#6c6c6c;fill-opacity:1;stroke:none;stroke-width:0.5;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
     id="rect6588-2"
     width="126"
     height="34"
     x="59"
     y="1023" />
  <text
     sodipodi:linespacing="125%"
     id="text6770-7"
     y="1045.8575"
     x="91.91333"
     style="font-style:normal;font-weight:normal;font-size:50px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
     xml:space="preserve"><tspan
       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:17.5px;font-family:'Franklin Gothic Medium';-inkscape-font-specification:'Franklin Gothic Medium, ';fill:#ffffff;fill-opacity:1"
       y="1045.8575"
       x="91.91333"
       id="tspan6772-6"
       sodipodi:role="line">button2</tspan></text>
</g>
<text
   xml:space="preserve"
   style="font-style:normal;font-weight:normal;font-size:50px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
   x="75.460815"
   y="941.51672"
   id="text421312"
   sodipodi:linespacing="125%"
   svg-creator="title"><tspan
     sodipodi:role="line"
     id="tspan8274"
     x="75.460815"
     y="941.51672"
     style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:27.5px;font-family:'Franklin Gothic Medium';-inkscape-font-specification:'Franklin Gothic Medium, ';fill:#ffffff;fill-opacity:1">Title</tspan></text>

到目前为止,我可以更改和设置我的 svg:g 的属性,但我需要访问/操作该 selectStr 中的另一个 svg:g!

如何访问和操作该 xpath 的 button1 和 button2? (我有多个具有相同属性的弹出窗口,所以我的实际 xPath 包含 popup1)

【问题讨论】:

  • 通过在当前 XPath 返回的元素对象上应用另一个 XPath。恐怕无法提供更具体的解释,除非您还提供更具体的信息,即包含当前选定元素和实际目标元素的示例 XML。谢谢
  • 当然是代码:
  • 你想改变什么?

标签: python xpath svg lxml


【解决方案1】:

您可以使用简单的 XPath 来查找子元素,例如 selectStr 中的“button1”:

btn1 = selectStr.find('svg:g[@svg-creator="button1"]', inkex.NSS)

如果您愿意,也可以使用xpath() 方法:

btn1 = selectStr.xpath('svg:g[@svg-creator="button1"]', namespaces=inkex.NSS)[0]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2012-12-24
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多