In-place modification of .xpath() results (doesn't work!!1)
Hello, This wonderful Sunday morning, I decided to write small hack tool, just to be frustrated by the following code not working: target = tree.xpath("target[@name='-package-resources']")[0] aapt = target.xpath("//aapt")[0] aapt.set("manifestpackage", "foo.bar") print lxml.etree.tostring(target) I checked that element in aapt is modified as expected, but the changes are not mirrored in target. Digging further with print target.getchildren()[1].getchildren() print aapt I see that original element and one returned by .xpath() are different: [<Element aapt at 0x957134c>] <Element aapt at 0x9571374> Moreover, the original XML element contains comments, while one returned by .xpath() does not. Why all this doesn't work as intuitively expected? What's more strange is that I used pattern as above many times previously, and everything worked (the documents on which it worked definitely didn't have comments though). For reference, culprit XML is Android ant build.xml excerpt: <target name="-package-resources" depends="-crunch"> <!-- only package resources if *not* a library project --> <do-only-if-not-library elseText="Library project: do not package resources..."> <aapt executable="${aapt}" command="package" versioncode="${version.code}" versionname="${version.name}" debug="${build.is.packaging.debug}" manifest="${out.manifest.abs.file}" assets="${asset.absolute.dir}" androidjar="${project.target.android.jar}" apkfolder="${out.absolute.dir}" nocrunch="${build.packaging.nocrunch}" resourcefilename="${resource.package.file.name}" resourcefilter="${aapt.resource.filter}" libraryResFolderPathRefid="project.library.res.folder.path" libraryPackagesRefid="project.library.packages" libraryRFileRefid="project.library.bin.r.file.path" previousBuildType="${build.last.target}" buildType="${build.target}" ignoreAssets="${aapt.ignore.assets}"> <res path="${out.res.absolute.dir}"/> <res path="${resource.absolute.dir}"/> <!-- <nocompress /> forces no compression on any files in assets or res/raw --> <!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw --> </aapt> </do-only-if-not-library> </target> -- Best regards, Paul mailto:pmiscml@gmail.com
HI Paul,
target = tree.xpath("target[@name='-package-resources']")[0] aapt = target.xpath("//aapt")[0]
Maybe you want to search './/aapt' here? //aapt searches the document.
x = etree.XML('<a><b><c/></b><d><c/></d></a>') d = x.xpath('d')
d[0].xpath('.//c') [<Element c at 0x107c295f0>] d[0].xpath('//c') [<Element c at 0x107c294b0>, <Element c at 0x107c295f0>]
Hello, On Sun, 15 Sep 2013 12:51:27 +0200 Jens Quade <jq@qdevelop.de> wrote:
HI Paul,
target = tree.xpath("target[@name='-package-resources']")[0] aapt = target.xpath("//aapt")[0]
Maybe you want to search './/aapt' here? //aapt searches the document.
Weird, indeed there was another <aapt> element in the xml doc, and I kinda did diff with my eyes to make sure selected one is what I needed (modulo comments). Apparently I was wrong. And I always thought that searching against element will use subtree rooted at that element, but http://lxml.de/xpathxslt.html#the-xpath-method explicitly says that if absolute-pathed query is used on element, it will be executed against the root tree. Live and learn. Thanks for your response! -- Best regards, Paul mailto:pmiscml@gmail.com
participants (2)
-
Jens Quade
-
Paul Sokolovsky