473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1153
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.