Sitecore Core Development

Friday, April 28, 2006

Sitecore XPath

The Sitecore XPath query language is a custom-implemented subset of XPath. The main difference is that instead of this:

"/item[@key='sitecore']/item[@key='content']/item[@key='home']"

you can write:

"/sitecore/content/home"

which is much easier.

Sitecore XPath can be used from the API using the Database.SelectItems(...) or Database.SelectSingleItem(...) methods. In the Sitecore Client you can use the it in the sources for Lookup, ValueLookup and MultiList fields. e.g.

"query:/sitecore/content/home/*"

which yields the subitems of home.


You can also use Sitecore XPath in the new XPath Builder in the Developer Center of 5.3.

The performance in .NET 2.0 is probably not as good as real XPath (.NET 1.0 and 1.1 is another story), but for some tasks you prefer simplicity over speed - rarely, but it happends.

(Update: I have added a bit more explanation)

The reason for this post is really that we made a small addition to the syntax. You can now quote identifiers with hashs (#). Suppose you have an item with the name "News - Corporate". Before you could not address this item as the dash (-) would be interpreted as an operator and not a part of the name. To solve this you can now write:

"/sitecore/content/Home/#News - Corporate#"

Everything between the hashs is considered a name or an identifier.

32 Comments:

Post a Comment

<< Home