开发者

XPath where the requirements apply to deeper nodes than the ones in resulting set

In my answer to ano开发者_Go百科ther post in SO (here: SharePoint 2007, how to check if a folder exists in a document library) 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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜