469,582 Members | 2,425 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,582 developers. It's quick & easy.

XPath expression help

I'd like to write an XPath expression that gets all nodes with a given
local-name, say 'Customer', within a given namespace. From a prior response
I got the following to get nodes with a specific local-name:

//*[local-name() = 'thenamehere']

But how would I modify this to look only within a given namespace (although
not likely, I could have 'Customer' defined in multiple namespaces)?

Any help would be great!

Paul
Nov 18 '05 #1
2 1148


Paul wrote:
I'd like to write an XPath expression that gets all nodes with a given
local-name, say 'Customer', within a given namespace.


Well XPath has namespace support, in the XPath expression you use a
prefix bound to a namespace URI where that binding happens outside of
the XPath expression language. Thus
//pf:Customer
with the prefix pf bound to the proper namespace URI selects all
Customer elements in that namespace.
How you bind that prefix to the namespace URI depends on where/how you
use XPath, within an XSLT stylesheet you would simply declare that e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pf="http://example.com/ns1" version="1.0">

If you use some API to evaluate XPath expressions you need to check the
documentation, for MSXML see this post
<http://groups.google.com/group/microsoft.public.xml/msg/78260972bdb86ccf>

For .NET you need to use an XmlNamespaceManager, bind the prefix you
want to use to the namespace URI and pass the namespace manager as the
second argument to SelectNodes/SelectSingleNode.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 18 '05 #2
Hi Paul,

Have you updated the namespace in the namespace manager?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Anna | last post: by
3 posts views Thread by gimme_this_gimme_that | last post: by
2 posts views Thread by gimme_this_gimme_that | last post: by
4 posts views Thread by syed.akhlaq | last post: by
2 posts views Thread by arunairs | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.