472,371 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

Trying to find a elements Xpath and store it as a attribute

Hi all,

I've been struggling with this for a while so I'm hoping that someone
could point me in the right direction. Here's my problem: I'm trying to
get the XPath for a given node in my document and then store that XPath
as an attribute of the element itself. If anyone has a recommendation
I'd be happy to hear it.

Thanks,

Provo

For instance, I would take this XML

###before

<?xml version="1.0"?>
<hello-world>
<greeter>An XSLT Programmer</greeter>
<greeting>Hello, World!</greeting>
</hello-world>

###after

<?xml version="1.0"?>
<hello-world>
<greeter xp="hello-word[1]/greeter[1]">An XSLT Programmer</greeter>
<greeting xp="hello-world[1]/greeting[1]">Hello, World!</greeting>
</hello-world>

###

import sets
import amara
from amara import binderytools

doc = amara.parse('hello.xml')
elems = {}

for e in doc.xml_xpath('//*'):

paths = elems.setdefault((e.namespaceURI, e.localName),
sets.Set())
path = u'/'.join([n.nodeName for n in
e.xml_xpath(u'ancestor::*')])
paths.add(u'/' + path)

for name in elems:

doc.name.km = elems[name]

Oct 2 '06 #1
1 1108
provowallis wrote:
Hi all,

I've been struggling with this for a while so I'm hoping that someone
could point me in the right direction. Here's my problem: I'm trying to
get the XPath for a given node in my document and then store that XPath
as an attribute of the element itself. If anyone has a recommendation
I'd be happy to hear it.

Sorry. I only check c.l.py once a week or so...
For instance, I would take this XML

###before

<?xml version="1.0"?>
<hello-world>
<greeter>An XSLT Programmer</greeter>
<greeting>Hello, World!</greeting>
</hello-world>

###after

<?xml version="1.0"?>
<hello-world>
<greeter xp="hello-word[1]/greeter[1]">An XSLT Programmer</greeter>
<greeting xp="hello-world[1]/greeting[1]">Hello, World!</greeting>
</hello-world>

###

import sets
import amara
from amara import binderytools

doc = amara.parse('hello.xml')
elems = {}

for e in doc.xml_xpath('//*'):

paths = elems.setdefault((e.namespaceURI, e.localName),
sets.Set())
path = u'/'.join([n.nodeName for n in
e.xml_xpath(u'ancestor::*')])
paths.add(u'/' + path)

for name in elems:

doc.name.km = elems[name]
It's a tougher problem than you may think :-)

Luckily it's a problem I've worked on. For discussion see:

http://www.xml.com/pub/a/2004/11/24/py-xml.html

For an updated solution see abs_path in Amara domtools. In most cases
you can safely call that on an Amara bindery node.
--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Oct 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Don Bate | last post by:
I'm having a problem with the "unique" feature of XML Schema. I'm relatively new to using this feature so it may be that I'm misunderstanding something. Let's assume that I have the following...
2
by: monsterpot | last post by:
Hello, I have a document like this: <parameters> <param ptype="init">somedata</param> <param ptype="process">moredata</param> <param ptype="kill">alsodata</param> </parameters> <param> has...
1
by: Victor Hadianto | last post by:
Hi, I have a simple XSD for example like this: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">...
3
by: R.Schuetze | last post by:
Hallo, I'm working on an XSD schema for an 3D Point. The XML struktur of the point is the following: <Point name="6" target="Retro" type="TP" unit="m"> <param name="X" value="0.000"/> <param...
2
by: vjethava | last post by:
Hi, I'm relatively new to xml. I want to have a xml element of the form: <!ELEMENT CAR (MAKE, COMPANY)> <!ATTLIST CAR TYPE PCDATA PRICE PCADATA> <!ELEMENT COMPANY (HQ, COMPANY_TYPE)>
11
by: Rob | last post by:
I know, I know, don't use frames. Well, I'm stuck with these frames and I'm trying to add functionality without a complete redsign. You can look at this as a nostalgic journey. Anyway, I've got...
2
by: smachin1000 | last post by:
Hi All, In the sample schema & document below, I'd like the attribute "name" to be unique for all function elements under function_list. The tools I'm using (XML Spy and xmllint) all validate...
16
by: TT (Tom Tempelaere) | last post by:
Hi all, I created an XSD to define the structure of an XML file for my project. I made an XML file linked to the XSD using XmlSpy. The problem is that if I read the file using .NET XmlDocument...
5
by: Ouray Viney | last post by:
Hi All: I am looking at writing a python script that will let me parse a TestSuite xml file that contains n number of TestCases. My goal is to be able to count the <TestCaseelements base on a...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.