tags:

views:

56

answers:

3

In my answer to another post in SO (here: http://stackoverflow.com/questions/1948552/sharepoint-2007-how-to-check-if-a-folder-exists-in-a-document-library/2005855#2005855) I had to parse an XML document with structure:

D:multistatus
|--D:response
|----D:propstat
|-------D:prop
|----------D:displayname
|----------D:isFolder

Is it possible to construct an XPath statement that selects a set of such D:response elements that contain D:displayname equal to "someName" and D:isFolder is "t"?

I know how to do it if I select all D:response elements and then loop through the result set, but I believe XPath is powerful enough to do that in more delicate way.

+3  A: 
//D:response[D:propstat/D:prop/D:displayname="someName" and D:propstat/D:prop/D:isFolder="t"]
vtd-xml-author
Thank you! I knew it, XPath is powerful ;-) Any good resources where I could read more about general rules of constructing XPath statements?
naivists
webmonkey has xpath tutorials... w3c has full xpath spec, you can do it using web search
vtd-xml-author
Shorter and more efficient: `/*/D:response[D:propstat/D:prop[D:displayname='someName' and D:isFolder='t']]`
Tomalak
+1  A: 

If displayname and isFolder can appear anywhere within D:response, then this should work.

//D:response//[D:displayname="someName" and D:isFolder="t"]

// means the node can appear anywhere in the hierarchy and

[...] is a predicate used to filter elements matching the given criteria.

Anurag
I believe that the structure is more or less standard, since a lot of tools have been built that implement WebDAV. However, your version is shorter than the one posted by @Jimmy zhang
naivists
Didn't know it was WebDAV. There are countless ways to do this in Xpath, but Jimmys solution is definitely better as it enforces the hierarchy which can avoid a lot of headache later on :)
Anurag
A: 

A shorter and more efficient variant of @Jimmy Zhang's answer is

/*/D:response[D:propstat/D:prop[D:displayname='someName' and D:isFolder='t']]

It avoids the inefficient // operator (which needlessly checks the whole tree when the position of the target element is actually known). Also it uses a nested predicate to avoid redundancy.

Tomalak
What exactly does `/*/` mean? Do you know some good reference manual where these constructs are explained?
naivists
`*` matches **any element**. Check out w3schools reference on XPATH syntax: http://www.w3schools.com/XPath/xpath_syntax.asp
Mads Hansen
oh, finally got it- the first slash is the root, then you say "any element as a direct child of root node" and then you request "D:response" element. Thanks for the link!
naivists