Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 713 posts 954 karma points
    Jul 19, 2011 @ 06:04
    Tom
    0

    Razor Xpath statement

    Hi Guys I have the following:

    Collection

          Release 1

                Item 1

                Item 2

          Release 2

               Item 1

     

    When on the collection page

    I wanted to find the first release that is not hidden (and published of course) that has child products

     

    so in my collection template I've written the following

    <umbraco:Macro runat="server" language="cshtml">
        @inherits umbraco.MacroEngines.DynamicNodeContext
        @{
          var activeCollection = Model.Children.Where("Active").XPath("//currentRelease [count(./productItem) > 0]").FirstOrDefault();
          if(activeCollection != null && activeCollection.Items.Count() > 0)
          {
            var products = activeCollection.Take(15);
            
            <div id="collection-items-wrapper" class="clearfix">
            <ul class="list-none">
            @foreach(var product in products)
            {
              <li>@product.Name</li>
            }
            </ul>
            </div>
          }
        }
      </umbraco:Macro>

     

    I'm not getting any results.. just wondering how the XPath query should be formulated and where the "root" of the xpath begins?

     

    Cheers,

    Tom

  • Alex 78 posts 136 karma points
    Jul 19, 2011 @ 09:45
    Alex
    1

    You should be able to do this without using xPath, how about something like this:

    dynamic activeCollection = Model.Descendants("releaseDocType").Where("ChildrenAsList.Count > 0").FirstOrDefault();

    Obviously changing "releaseDoctype" to whatever you DocType alias is.

  • Marc Burton 5 posts 28 karma points
    Jan 10, 2012 @ 12:54
    Marc Burton
    1

    The XPath method gives you access to the whole xml cache so those preceding // will start your query from the root node of the cache ie the root node of your site. Try rewriting it to:

     XPath("currentRelease [count(./productItem) > 0]")

    That should give you the query from your current node - well it worked for me anyhow...

Please Sign in or register to post replies

Write your reply to:

Draft