This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Microsoft XML > May 2007 > xpath help





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author xpath help
Craig Buchanan

2007-05-01, 6:18 pm

I'm having difficulty with an Xpath on an XML document (snippet below) that
contains a default namespace.

If I remove the default namespace, this Xpath produces the desired result:
//li[a[@href[contains(.,'aplushomecare.org')]]]. Essentially, i want a 'li'
element if its contains an 'a' element with a href attribute that contains
the desired text.

I tried
//*[local-name()='li'][local-name()='a'][@href[contains(.,'aplushomecare.org')]],
but it returns no nodes.

Any assistance would be appreciated.

Thanks,

Craig Buchanan

Xml:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head/>
<body>
<ul class="padbot">
<li>
<a name="a" />
<strong>A+ Home Care, Inc.</strong><br />
8932 Old Cedar Ave., S.<br />
Bloomington, MN 55425<br />
800-603-7760<br />
<a href="http://www.aplushomecare.org/">www.aplushomecare.org</a><br />
</li>
...
</ul>
</body>
</html>



Martin Honnen

2007-05-01, 6:18 pm

Craig Buchanan wrote:
> I'm having difficulty with an Xpath on an XML document (snippet below) that
> contains a default namespace.
>
> If I remove the default namespace, this Xpath produces the desired result:
> //li[a[@href[contains(.,'aplushomecare.org')]]]. Essentially, i want a 'li'
> element if its contains an 'a' element with a href attribute that contains
> the desired text.


You where shown how to use e.g.
xmlDoc.setProperty('SelectionNamespace',
'xmlns:xhtml="http://www.w3.org/1999/xhtml"');
to bind a prefix to the namespace URI so that you can then write XPath
expressions like
//xhtml:li[xhtml:a[contains(@href, 'aplushomecare.org')]]

If you don't want that then use the cumbersome local-name() approach but
you need to keep the structure e.g.
//*[local-name() = 'li'][*[local-name() = 'a'][contains(@href,
'aplushomecare.org')]]
so that way you are looking for elements at all levels having the local
name 'li' which have a child element having the local name 'a' where the
attribute 'href' contains the string value.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Bjoern Hoehrmann

2007-05-01, 6:18 pm

* Craig Buchanan wrote in microsoft.public.xml:
>I'm having difficulty with an Xpath on an XML document (snippet below) that
>contains a default namespace.
>
>If I remove the default namespace, this Xpath produces the desired result:
>//li[a[@href[contains(.,'aplushomecare.org')]]]. Essentially, i want a 'li'
>element if its contains an 'a' element with a href attribute that contains
>the desired text.
>
>I tried
>//*[local-name()='li'][local-name()='a'][@href[contains(.,'aplushomecare.org')]],
>but it returns no nodes.
>
>Any assistance would be appreciated.


You need to setup a prefix for the default namespace and use that prefix
in the expression. How to do that depends on what XML toolkit you use.
The expression you are looking for is e.g.

//xhtml:li[ xhtml:a[ contains(@href, 'aplus...') ] ]

Matching only on the local-name() (and ideally also the namespace-uri())
would also work, but the latter expression above matches any element
with a local-name() that is both 'li' and 'a', which is impossible.
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews