Copied to clipboard

Flag this post as spam?

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


  • Phong Nguyen Dinh 26 posts 197 karma points
    Nov 02, 2016 @ 04:37
    Phong Nguyen Dinh
    0

    Expression must evaluate to a node-set while using XPath

    I am getting error while trying to get nodes by XPath. Any help is appreciated.

    var xpathQuery = string.Format("//{0}/[@nodeName='{1}']/{2}//[@nodeTypeAlias='{3}' or @nodeTypeAlias='{4}']",
                    DocumentTypes.Kempinski,
                    language.ToLower(),
                    DocumentTypes.CorporatePressReleasesLandingPage,
                    DocumentTypes.TemplateAContentWithTextOnly,
                    DocumentTypes.TemplateBContentWithFeaturedImage);
    

    I use glass mapper for DocumentTypes. Here is my umbraco backend structure:

    <Kempinski>
        <en name="en">
            <CorporatePressReleasesLandingPage>
    
                <TemplateAContentWithTextOnly>
                </TemplateAContentWithTextOnly>
    
                <TemplateBContentWithFeaturedImage>
                </TemplateBContentWithFeaturedImage>
    
            </CorporatePressReleasesLandingPage>
        </en>
    </Kempinski>
    

    I want to take all Nodes with Document Types have alias "TemplateAContentWithTextOnly" and "TemplateBContentWithFeaturedImage"

    enter image description here

    And i get this error. Anyone have any idea?

    Regard,

    Phong.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Nov 02, 2016 @ 07:12
    Dave Woestenborghs
    0

    Hi Phong,

    Don't immediatly see what is wrong with the query, but I'm no xpath expert.

    What I would do is simplify the query and bits.

    So I would start getting the Kempinskie part first. Than the language node...and so on.

    That way you can find the part of the query that breaks.

    Dave

  • Phong Nguyen Dinh 26 posts 197 karma points
    Nov 02, 2016 @ 07:53
    Phong Nguyen Dinh
    0

    Hi Dave,

    Thank for your advice.

    I did exactly what you said to me but every single time i tried to get a node, it was always null, even the first one: Kempinski.

    And i get more confused about this. Do you have any idea Dave?

    Phong.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Nov 02, 2016 @ 08:26
    Dave Woestenborghs
    0

    Hi Phong,

    The xml you posted is from the umbraco.config file in the App_Data folder ?

    I tested this query and this works on the supplied xml fragment :

    //Kempinski/*[@nodeName='en']/CorporatePressReleasesLandingPage/*[self::TemplateAContentWithTextOnly or self::TemplateBContentWithFeaturedImage]
    

    Dave

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 02, 2016 @ 08:29
    Chriztian Steinmeier
    1

    Hi Phong,

    There's a couple of tiny bugs in the XPath - try this instead:

    string.Format("//{0}[@nodeName='{1}']/{2}//*[@nodeTypeAlias='{3}' or @nodeTypeAlias='{4}']"
    

    Notice that there needs to be a "path step" (e.g. *, elementname or similar) before a predicate (thing in square brackets).

    Another version could be this:

    string.Format("//{0}[@nodeName='{1}']/{2}//*[self::{3} or self::{4}]"
    

    (I like that better, but it may not be as obvious if you're not familiar with XPath)

    Hope that helps,

    /Chriztian

  • Phong Nguyen Dinh 26 posts 197 karma points
    Nov 02, 2016 @ 10:34
    Phong Nguyen Dinh
    100

    Thank you so much Dave and Chriztian for your help, i found a solution.

    I change my string format to:

    string.Format("//{0}/*[@nodeName='{1}']/{2}/*[local-name()='{3}' or local-name()='{4}']"
    

    I also tried your ideas but it didn't work, so sorry for my late reply.

    It took me a lot of time but i think i get better with XPath now.

    Thank you guys again for your kindness. Love this comunity.

    Regard, Phong.

Please Sign in or register to post replies

Write your reply to:

Draft