Question

I want to create a compilestring to use it with an XPathExpression. I already navigated to a subset and have created an iterator. Based on the current Position of that iterator i create a new currentNode. Now i want to create the exact expression wich leads to that, and only that node, so i can then create an iterator wich selects only these children.

    public XPathNodeIterator extractSubChildIterator(XPathNavigator currentNode)
    {
        XPathNavigator nav = currentNode.Clone();
        string myXPathString = "/"+ nav.LocalName + "["+ HOWDOIGETTHISNUMBER(nav) +"]";
        while (nav.MoveToParent())
        {
            if (!(nav.Name == ""))
                myXPathString = "/" + nav.LocalName + "[" + HOWDOIGETTHISNUMBER(nav) + "]" + myXPathString;
        }
        myXPathString += "/*";

        XPathExpression expr = nav.Compile(myXPathString);
        return currentNode.Select(expr);
    }

The function HOWDOIGETTHISNUMBER() is the placeholder for the thing i dont quite get. I base my expression String on the examplelist on This Page - "/catalog/cd[1] selects the first cd child of catalog"

Was it helpful?

Solution 2

The Tip to Evaluate an XPathExpression was the right one. Various Expressions can be found here. Problem solved using this query:

    private string HOWDOIGETTHISNUMBER(XPathNavigator theNode)
    {
        XPathExpression query = theNode.Compile("count(preceding-sibling::"+theNode.LocalName+")+1");
        return theNode.Evaluate(query).ToString();
    }

OTHER TIPS

    string myXPathString = "/"+ nav.LocalName + "["+ HOWDOIGETTHISNUMBER() +"]"; 

You want to construct and evaluate this XPath expression:

"count(preceding-sibling::*[name()=''" + nav.LocalName +"]"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top