I have an SVG image with Model and Group as ID attribute of tag g, like
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="1141.5200729370117" version="1.1" width="1726.9000701904297" id="temp" viewBox="0 0 1726.9000701904297 1141.5200729370117">
<defs/>
<g id="Model" class="Model A">
<g class="Group">
<g id="Group-1" class="Group A">
<g id="Line" fill="#000000" stroke="#000000" style="fill-opacity: 1; stroke-opacity: 1; stroke-width: 0.2;" class="Line External"><polygon points="118.00,21.56 1293.38,21.56 1277.38,37.56 133.97,37.56 "/> </g>
<g id="Box" fill="#FFFFFF" stroke="#FFFFFF" style="fill-opacity: 1; stroke-opacity: 1; stroke-width: 0.2;" class="Box External"><polygon points="118.00,21.56 1293.38,21.56 1277.38,37.56 133.97,37.56 "/> </g>
</g>
</g>
</g>
</svg>
I want to remove all g tags which have id other than Line. I read the SVG file using:
import xml.etree.ElementTree as ET
root = ET.parse('my file.svg')
SVG_NS = "http://www.w3.org/2000/svg"
Then I create a parent map
parent_map = {c:p for p in root.iter() for c in p}
And remove those other than Line
for node in root.findall('.//{%s}g' % SVG_NS):
name = node.get('id')
if "Line" in str(name):
print('n=', node)
else:
parent_map[node].remove(node)
How do I convert back the parent_map to an SVG and save it as a png file?
My main objective is to change stroke width of a given svg file, but I see no inbuilt method to do so.
The width's are unfortunately hardcoded in the svg file, and my initial thought was to buffer the data, and edit it in memory before changing the rendered svg file. But that is just additional processing overhead.
Next, I looked into converting the svg to a dom - tree like structure, but then I might have to save it back again before loading it in the QGraphicsSvgItem, which would be a bizarre intermediate step for changing line thickness on the fly.
Here is an example svg file,
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
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:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="3.2421772mm"
height="6.1269817mm"
viewBox="0 0 3.2421772 6.1269817"
version="1.1"
id="svg12403"
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
sodipodi:docname="Bag.svg">
<defs
id="defs12397" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.35"
inkscape:cx="-229.58734"
inkscape:cy="-219.85002"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="1366"
inkscape:window-height="705"
inkscape:window-x="-8"
inkscape:window-y="-8"
inkscape:window-maximized="1" />
<metadata
id="metadata12400">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(-43.736054,-84.5377)">
<g
id="use26359"
transform="matrix(0.26458333,0,0,0.26458333,-3.9887501,73.928193)">
<desc
id="desc12960">Bag</desc>
<title
id="title12962">Bag</title>
<path
d="m 180.61365,49.4094 v 13.609494 h 11.78107 V 49.4094 C 192.27,46.875236 189.67,44.872517 186.50494,44.872517 c -3.16454,0 -5.76448,2.002719 -5.89129,4.536883 z m 2.94488,-9.07316 2.94641,4.536277 2.9449,-4.536277 z"
style="fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:0.66862381"
id="path12964"
inkscape:connector-curvature="0" />
<g
style="stroke-width:1.31504369"
id="g12968"
transform="matrix(0.507302,0,0,0.50958516,89.699529,-958.6518)">
<path
d="m 179.211,1978.199 v 26.707 h 23.223 v -26.707 c -0.246,-4.972 -5.371,-8.902 -11.61,-8.902 -6.238,0 -11.363,3.93 -11.613,8.902 z m 5.805,-17.804 5.808,8.902 5.805,-8.902 z m 5.808,0 v 8.902"
style="fill:none;stroke:#000000;stroke-width:0.93203717;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
id="path12966"
inkscape:connector-curvature="0" />
</g>
</g>
</g>
</svg>
As you can see, there is a stroke width value in the style attribute for the path.
Is there a simple less obnoxious "hack" way of achieving this?
I'd like to read, modify, and save (override) my svg file with BeautifulSoup in Python.
Contents of bs-test.svg:
<g data-default-color="#FFFFFF" data-element-id="X123456">
<rect class="selection-box" fill="none" height="91" stroke="none" width="140" x="-30" y="-10"/>
<circle cx="40" cy="25" data-colored="true" fill="red" pointer-events="visible" r="25" stroke="black" stroke-width="3"/>
<text fill="black" font-family="Verdana" font-size="16" text-anchor="middle" x="40" y="55">
<tspan dy="16" x="40">Label Text</tspan>
</text>
</g>
The contents are actually a subset of a larger svg, where I find g elements based on user-provided data-element-id values.
I'd like to change the fill attribute of the circle element to "blue".
what I have so far:
from bs4 import BeautifulSoup as bs
with open("bs-test.svg", "r") as f:
contents = f.read()
soup = bs(contents, "xml")
# grab g tags with the required data-element-id
elem_ls = soup.find_all(attrs={"data-element-id" : "X123456"})
x = elem_ls[0]
x
Output
<g data-default-color="#FFFFFF" data-element-id="X123456">
<rect class="selection-box" fill="none" height="91" stroke="none" width="140" x="-30" y="-10"/>
<circle cx="40" cy="25" data-colored="true" fill="red" pointer-events="visible" r="25" stroke="black" stroke-width="3"/>
<text fill="black" font-family="Verdana" font-size="16" text-anchor="middle" x="40" y="55">
<tspan dy="16" x="40">Label Text</tspan>
</text>
</g>
I'm sure that this is just a syntax question that I can't quite find the answer to; how might I go about grabbing the fill attr of circle, replacing its value with "blue", then writing out?
You just have to set the attribute value of the bs4 element using key
CODE:
from bs4 import BeautifulSoup as bs
with open("bs-test.svg", "r") as f:
contents = f.read()
soup = bs(contents, "xml")
# grab g tags with the required data-element-id
elem_ls = soup.find_all(attrs={"data-element-id" : "X123456"})
for e in elem_ls:
circle = e.find('circle')
circle['fill'] = 'blue'
print(e)
RESULTS:
<g data-default-color="#FFFFFF" data-element-id="X123456">
<rect class="selection-box" fill="none" height="91" stroke="none" width="140" x="-30" y="-10"/>
<circle cx="40" cy="25" data-colored="true" fill="blue" pointer-events="visible" r="25" stroke="black" stroke-width="3"/>
<text fill="black" font-family="Verdana" font-size="16" text-anchor="middle" x="40" y="55">
<tspan dy="16" x="40">Label Text</tspan>
</text>
</g>
All I am trying to do is select the drop down & then select "Export Excel Spread Sheet".
Example of Drop Down
Code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url2 =["https://example.com/reports"]
driver = webdriver.Chrome()
driver.implicitly_wait(15)
driver.get("https://example.com")
for u in url2:
driver.implicitly_wait(15)
driver.get(u)
I have tried so many different Xpaths & ID's
#driver.find_element_by_xpath("//a[contains(#class,'dropdown__trigger header-export-menu--toggle-btn')]").click()
#driver.find_element_by_xpath("//li[contains(text(),'Export Excel Spread Sheet')]").click()
#act.click().perform()
#act.click(driver.find_element_by_xpath("//a[contains(#class,'dropdown__trigger header-export-menu--toggle-btn')]")).perform()
#act.move_to_element(driver.find_element_by_xpath("//a[contains(#class,'dropdown__trigger header-export-menu--toggle-btn')]")).perform()
#WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID, "Header-Dropdown-Menu")).click())
#driver.find_element_by_class_name("//div[contains(#class,'dropdown__content header-export-menu--content')]").click()
#driver.find_element_by_xpath('//div[#class="dropdown header-export-menu" and #class="dropdown dropdown--active header-export-menu"]')
#driver.quit()
HTML Code
Click Me For HTML Example1
<!-- Under React Empty: 32 -->
<div class = "dropdown header-export-menu">
Take a look at the name in Example 1 vs Example 2
Click Me for HTML Example2
You'll notice the HTML Code has change to
<!-- Under React Empty: 32 -->
<div class = "dropdown dropdown--active header-export-menu">
Which I think is part of the problem I am having. Pretty Stuck.
I have also tried to use ChroPath & XPath Helper to try and resolve the issue but no luck.
Thank you in Advance !
Update:
The Comments have asked for further detail of the HTML code & I have gathered the following block.
<div class="header-container">
<!-- react-empty: 429 -->
<div class="header-event-info" id="header-event-info">
<div class="">
<div>
<div class="single-event-info">
<div class="event-data">
<p class="data-dd">25</p>
<p class="data-mmyy">Jun 2017</p>
</div>
<div class="event-detail">
<p class="event-name">"A name of a musical"</p>
<p class="event-more-details">
<!-- react-text: 437 -->
"Tuesday, 7:00 pm, Some Theatre"
<!-- /react-text -->
<a class="popup" data-content="" data-icon="" data-position="bottom" data-width="350" data-height="auto" data-trigger="click" data-scrollable="false">
<span class="popup-icon">
<svg width="19px" height="19px" viewBox="0 0 19 19" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<desc>Created with sketchtool.</desc>
<defs></defs>
<g id="Totals-For-Today" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g class="svg-icon-path" id="01-Event-Audit-Narrow---No-TFT" transform="translate(-983.000000, -121.000000)" stroke="#919598">
<g id="Group" transform="translate(27.500000, 15.560000)">
<g id="iButton" transform="translate(956.000000, 106.000000)">
<path d="M9,17.4399996 C13.6944204,17.4399996 17.5,13.63442 17.5,8.93999958 C17.5,4.24557921 13.6944204,0.43999958 9,0.43999958 C4.30557963,0.43999958 0.5,4.24557921 0.5,8.93999958 C0.5,13.63442 4.30557963,17.4399996 9,17.4399996 Z" id="outline">
</path>
<path class="svg-icon-text" d="M10.4765625,13.3169527 L7.68164062,13.3169527 L7.68164062,12.930234 C7.77148482,12.9224214 7.86425733,12.914609 7.95996094,12.9067965 C8.05566454,12.8989839 8.13867152,12.8833591 8.20898438,12.8599215 C8.31835992,12.824765 8.3994138,12.7632422 8.45214844,12.6753511 C8.50488308,12.5874601 8.53125,12.4732034 8.53125,12.3325777 L8.53125,8.76421833 C8.53125,8.63921771 8.50292997,8.52496104 8.44628906,8.42144489 C8.38964815,8.31792875 8.31054738,8.23101556 8.20898438,8.16070271 C8.13476525,8.11382747 8.02734445,8.07378881 7.88671875,8.04058552 C7.74609305,8.00738223 7.61718809,7.98687462 7.5,7.97906208 L7.5,7.59820271 L9.5390625,7.46929646 L9.62109375,7.55132771 L9.62109375,12.2622652 C9.62109375,12.3989846 9.64746067,12.5122648 9.70019531,12.602109 C9.75292995,12.6919532 9.83593693,12.7583587 9.94921875,12.8013277 C10.0351567,12.8364841 10.1191402,12.8648042 10.2011719,12.8862886 C10.2832035,12.9077731 10.3749995,12.9224214 10.4765625,12.930234 L10.4765625,13.3169527 Z M9.73828125,5.18999958 C9.73828125,5.41265694 9.66503979,5.60699094 9.51855469,5.77300739 C9.37206958,5.93902385 9.19140732,6.02203083 8.9765625,6.02203083 C8.77734275,6.02203083 8.60449292,5.94293006 8.45800781,5.78472614 C8.31152271,5.62652223 8.23828125,5.44585997 8.23828125,5.24273396 C8.23828125,5.02788913 8.31152271,4.84039101 8.45800781,4.68023396 C8.60449292,4.5200769 8.77734275,4.43999958 8.9765625,4.43999958 C9.19921986,4.43999958 9.38183522,4.51519414 9.52441406,4.66558552 C9.6669929,4.81597689 9.73828125,4.99077983 9.73828125,5.18999958 L9.73828125,5.18999958 Z" id="i-2-copy-2" stroke-width="0.25" fill="#919598"></path>
</g>
</g>
</g>
</g>
</svg>
</span>
</a>
</p>
</div>
</div>
<div class="header-export">
<!-- react-empty: 32 -->
<div class="dropdown dropdown--active header-export-menu">
<a class="dropdown__trigger header-export-menu--toggle-btn">
<svg width="9px" height="5px" viewBox="0 0 9 5" version="1.1">
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard" transform="translate(-109.000000, -97.000000)" fill="#FFFFFF">
<g id="Header-Dropdown-Menu" transform="translate(96.000000, 82.000000)">
<path d="M18.1734867,19.6470682 C17.8014721,20.0543213 17.1922167,20.0476427 16.8263028,19.6470682 L13.2549246,15.7373969 C12.8829101,15.3301438 13.0295754,15 13.5787039,15 L21.4210856,15 C21.9719185,15 22.1107787,15.3368224 21.7448649,15.7373969 L18.1734867,19.6470682 Z" id="options-dropdown-menu-arrow"></path>
</g>
</g>
</g>
</svg>
</a>
<div class="dropdown__content header-export-menu--content">
<ul class="export-menu">
<li class="export-menu-item ">
<svg width="17px" height="14px" viewBox="0 0 17 15" version="1.1">
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Basic-Report-Template-SPECS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Basic-Report-Template---EXPORT-SPECS-OnClick" transform="translate(-522.000000, -180.000000)" stroke="#484B4D" stroke-width="2">
<g id="SPECS" transform="translate(487.000000, 14.000000)">
<g id="Download-Icon-Copy-2" transform="translate(43.500000, 172.000000) rotate(-180.000000) translate(-43.500000, -172.000000) translate(35.000000, 164.000000)">
<path d="M5.36902902,13.624518 L5.36902902,6.56257607 L12.430971,6.56257607" id="Rectangle-242-Copy-5" transform="translate(8.900000, 10.093547) rotate(-315.000000) translate(-8.900000, -10.093547) "></path>
<path d="M8.9,6.99999999 L8.9,12.9999999" id="Line-Copy-10" stroke-linecap="square"></path>
<path d="M16.9,0.0208873076 L16.9,4.02297419 C16.9,5.12639113 16.0054862,6.02088731 14.9059397,6.02088731 L2.89406028,6.02088731 C1.7927712,6.02088731 0.9,5.12262668 0.9,4.02297419 L0.9,0.0208873076" id="Rectangle-243-Copy-4" transform="translate(8.900000, 3.020887) rotate(-180.000000) translate(-8.900000, -3.020887) "></path>
</g>
</g>
</g>
</g>
</svg>
<!-- react-text: 55 -->
"Export PDF"
<!-- /react-text -->
</li>
<li class="export-menu-item">
<svg width="17px" height="14px" viewBox="0 0 17 15" version="1.1">
<desc>Created with Sketch.</desc>
<defs></defs><g id="Basic-Report-Template-SPECS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Basic-Report-Template---EXPORT-SPECS-OnClick" transform="translate(-522.000000, -180.000000)" stroke="#484B4D" stroke-width="2">
<g id="SPECS" transform="translate(487.000000, 14.000000)">
<g id="Download-Icon-Copy-2" transform="translate(43.500000, 172.000000) rotate(-180.000000) translate(-43.500000, -172.000000) translate(35.000000, 164.000000)">
<path d="M5.36902902,13.624518 L5.36902902,6.56257607 L12.430971,6.56257607" id="Rectangle-242-Copy-5" transform="translate(8.900000, 10.093547) rotate(-315.000000) translate(-8.900000, -10.093547) "></path>
<path d="M8.9,6.99999999 L8.9,12.9999999" id="Line-Copy-10" stroke-linecap="square">
</path>
<path d="M16.9,0.0208873076 L16.9,4.02297419 C16.9,5.12639113 16.0054862,6.02088731 14.9059397,6.02088731 L2.89406028,6.02088731 C1.7927712,6.02088731 0.9,5.12262668 0.9,4.02297419 L0.9,0.0208873076" id="Rectangle-243-Copy-4" transform="translate(8.900000, 3.020887) rotate(-180.000000) translate(-8.900000, -3.020887)
"></path>
</g>
</g>
</g>
</g>
</svg>
<!-- react-text: 79 -->
"Export Excel Spread Sheet"
<!-- /react-text -->
</li>
<li class="export-menu-item ">
<svg width="16px" height="12px" viewBox="0 0 16 13" version="1.1">
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="Basic-Report-Template-SPECS" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Basic-Report-Template---EXPORT-SPECS-OnClick" transform="translate(-554.000000, -182.000000)" stroke="#484B4D" stroke-width="2">
<g id="SPECS" transform="translate(487.000000, 14.000000)">
<g id="Email-Icon-Copy" transform="translate(67.000000, 168.000000)">
<rect id="Rectangle-40" x="0" y="0.669998169" width="16" height="11" rx="2"></rect>
<path d="M1.55761719,3.08300781 L8.07275391,7.10009766 L14.8974609,3.14355469" id="Path-41"></path>
</g>
</g>
</g>
</g>
</svg>
<!-- react-text: 90 -->
"Email/Schedule Report"
<!-- /react-text -->
</li>
</ul>
</div>
</div>
Update 2 (Solution)
Here's where I went wrong.
I didn't provide enough of the HTML code.
Just a few lines above there was an "iframe" which was not allowing me to enter the block of code.
After switching into the iframe, I was able to click into the button and complete the following task of exporting the excel report.
example of code (Generalized to your future endeavors)
#Finding the Frame
iframes = driver.find_element_by_id("IDofFrame")
#Switching to that frame
driver.switch_to.frame(iframes)
#Finding the dropdown button element
driver.find_element_by_xpath("XPathOfButton").click()
#delay on the export click
time.sleep(3)
#Export click
driver.find_element_by_xpath("XPathOfButtonToExport").click()
#If you need to switch out of the frame to go back to the original HTML block
driver.switch_to.default_content()
CHECK YOUR HTML CODE FOR FRAMES !!!!
good video to reference.
https://www.youtube.com/watch?v=NhRx99uFUNk
Actually your every attempt is incorrect.
driver.find_element_by_xpath("//li[contains(text(),'Export Excel
Spread Sheet')]").click()
Here you are using contains(text()) Which is incorrect actually if you pass the node set selected by text() to contains(), as you did, then it is converted to a string, by taking the string value of the first node in the node set while in your HTML I can see <svg> is the first inner node of the <li> element, I would suggest trying with dot . which will take all string value inside a node with explicit wait :
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.XPATH, "//li[contains(.,'Export Excel Spread Sheet')]")))
element.click()
Hope it helps
I'm trying to figure out how to access a specific element by id in an SVG file. I was using the python library of lxml to parse through the file, but it always comes up empty. Here is the python script I used to access the element:
#!/usr/bin/env python
from lxml import etree
XHTML_NAMESPACE = "http://www.w3.org/2000/svg"
XHTML = "{%s}" % XHTML_NAMESPACE
NSMAP = {None : XHTML_NAMESPACE}
root = etree.parse("temp.svg")
textid = "text1274"
path = ".//text[#id='" + textid + "']/title"
name = root.findtext(path=path, namespaces=NSMAP)
print name
The result is always an empty string ('None'), but no error. It believes it found what I was looking for, but what I wanted was the element text (which should have been "Wei, 771 - 661BCE."). Here is the incriminating SVG file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
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"
version="1.1"
xml:space="preserve"
viewBox="0 0 54001 32400"
id="svg2"
inkscape:version="0.91 r"
sodipodi:docname="china700BC.svg"><sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1366"
inkscape:window-height="692"
id="namedview2468"
showgrid="false"
inkscape:zoom="0.016419753"
inkscape:cx="17689.896"
inkscape:cy="17739.986"
inkscape:window-x="0"
inkscape:window-y="24"
inkscape:window-maximized="1"
inkscape:current-layer="svg2" />
<defs
id="defs4">
<filter
id="blur2">
<feGaussianBlur
id="feGaussianBlur7"
result="blur"
stdDeviation="2"
in="SourceGraphic" />
</filter>
<filter
id="blur4">
<feGaussianBlur
id="feGaussianBlur10"
result="blur"
stdDeviation="4"
in="SourceGraphic" />
</filter>
<filter
id="blur8">
<feGaussianBlur
id="feGaussianBlur13"
result="blur"
stdDeviation="8"
in="SourceGraphic" />
</filter>
<filter
id="blur16">
<feGaussianBlur
id="feGaussianBlur16"
result="blur"
stdDeviation="16"
in="SourceGraphic" />
</filter>
<filter
id="blur32">
<feGaussianBlur
id="feGaussianBlur19"
result="blur"
stdDeviation="32"
in="SourceGraphic" />
</filter>
<filter
id="blur64">
<feGaussianBlur
id="feGaussianBlur22"
result="blur"
stdDeviation="64"
in="SourceGraphic" />
</filter>
</defs>
>
<g
stroke-linecap="round"
stroke-linejoin="round"
stroke-miterlimit="7"
stroke-width="14"
fill="none"
filter="url(#blur2)"
id="fntr">
<ellipse
id="ellipse381"
fill="white"
stroke="white"
ry="1"
rx="1"
cy="0"
cx="0" />
<ellipse
id="ellipse383"
fill="white"
stroke="white"
ry="1"
rx="1"
cy="32400"
cx="54001" />
<ellipse
fill="#FEBADE"
ry="1"
rx="1"
cy="24759"
cx="48948"
id="295286-dummy" />
</g>
<g
text-anchor="middle"
id="regn">
</g>
<g
text-anchor="middle"
id="cultr">
</g>
<g
text-anchor="middle"
id="peopl">
</g>
<g
font-style="italic"
text-anchor="middle"
id="tribe">
</g>
<text
id="text455"
x="30542.088"
y="16248.173"
font-size="20"
style="font-weight:normal;font-size:233.01080322px;text-anchor:middle"><title
id="title457">Chen.</title>Chen</text>
<text
id="text1274"
x="28689.652"
y="12753.011"
font-size="28"
style="font-weight:normal;font-size:326.21511841px;text-anchor:middle"><title
id="title1276">Wei, 771 - 661BCE.</title>Wei</text>
<script
id="script2466">
function LoadHandler(event)
{
new Title(event.getTarget().getOwnerDocument(), 810);
}
</script>
</svg>
I discovered that I can eliminate the error by deleting the eighth line, beginning with "xmlns=..." (which is the namespace declaration). However, due to the nature of where I obtained this file I cannot permanently remove this line (and probably shouldn't). Is there some way (such as properly specifying the namespace) I can get the expected output without having to edit the XML at all?
Thanks a ton
Mapping default namespace to None prefix didn't work for me either. You can, however, map it to a normal string prefix and use that prefix in the xpath, the rest of your codes are working without any change :
from lxml import etree
XHTML_NAMESPACE = "http://www.w3.org/2000/svg"
XHTML = "{%s}" % XHTML_NAMESPACE
NSMAP = {'d' : XHTML_NAMESPACE} # map default namespace to prefix 'd:'
root = etree.parse("temp.svg")
textid = "text1274"
path = ".//d:text[#id='" + textid + "']/d:title" # use registered prefix in xpath
name = root.findtext(path=path, namespaces=NSMAP)
print name