Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 20, 2011 @ 15:30
    Jeroen Breuer
    0

    XPath get current node and children

    Hello,

    I'm no XPath expert so this might be easy, but I'm trying to create an XPath which returns the current node and it's childeren. I don't want the children of those children. Here's what I've currently got (in C#):

    var allDocs = xmlDocument.CreateNavigator();
    if (allDocs != null)
    {
        //Get all media items that have the allowed media type.
        var expr = !string.IsNullOrEmpty(allowedMediaTypes) ? 
            allDocs.Compile("//*[@nodeTypeAlias and contains('" + allowedMediaTypes + "',concat(',',@nodeType,','))]")
            : allDocs.Compile("//*[@nodeTypeAlias]");
    
        try
        {
            var iterator = allDocs.Select(expr);
            while (iterator.MoveNext())
            {
                //do stuff
            }
        }
        catch (Exception) { }
    }

    This returns the current node and all allowed descendants, but I only need the children. Any tips?

    Jeroen

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jul 20, 2011 @ 16:02
    Tom Fulton
    1

    Maybe try changing "//" to "/" and using the child selector?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 20, 2011 @ 16:03
    Jeroen Breuer
    0

    Wouldn't that just return the children and not the current node?

    Jeroen

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 20, 2011 @ 16:06
    Lee Kelleher
    0

    Hi Jeroen,

    The problem is those greedy double-slashes.  You could try something like this?

    "*[@nodeTypeAlias] | *[@nodeTypeAlias]/*[@nodeTypeAlias]"

    That would get the current node (if it has an "nodeTypeAlias" attribute), and all it's children that have that "nodeTypeAlias" attribute too.

    Hope it helps?

    Cheers, Lee.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jul 20, 2011 @ 16:17
    Jeroen Breuer
    0

    Hi Lee,

    Thanks that's what I was looking for. Working on another nice feature for DAMP 2.0 :-).

    Jeroen

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 20, 2011 @ 16:20
    Lee Kelleher
    0

    Cool, glad I could help.  Looking forward to DAMP 2.0!

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft