title: XPathNavigator select as if current node was root, or validate that an expression is relative to current node
body: <p>I am trying to write a class, that contains a private <code>XPathExpression</code> with functions where users can pass in a <code>XPathNavigator</code> or <code>HtmlNodeNavigator</code>, which returns also <code>XPathNavigator</code> or <code>HtmlNodeNavigator</code>.</p>
  
  <pre><code>class Demo
  {
      private XPathExpression expr;
      public Demo(String xpath)
      {
          expr = XPathExpression.Compile(xpath);
      }
  
      XPathNavigator GetFirst(XPathNavigator current)
      {
          return current.SelectSingleNode(expr);
      }
  }
  </code></pre>
  
  <p>The goal is that these can be used in sequence, so for example</p>
  
  <pre><code>String content = "&lt;x&gt;&lt;x&gt;abc&lt;/x&gt;&lt;/x&gt;";
  var doc = new HtmlDocument(content);
  var demo = new Demo("//x");
  XPathNavigator root = doc.DocumentNode.CreateNavigator();
  XPathNavigator outer = demo.GetFirst(root);
  XPathNavigator inner = demo.GetFirst(outer);
  </code></pre>
  
  <p>Here I want that <code>inner.Value</code> should equal "abc". However, this doesn't work. The expression <code>//x</code> will always select from the root. To make it work, I could change the expression to <code>.//x</code>, so that it actually uses the current node.</p>
  
  <p>But is there a way to do this <strong>without modifying the expression</strong> (which is all similar questions use as their answer)? I'm looking for something like <code>current.AsRoot.SelectSingleNode(expr)</code>. This would be the best solution for me. Although I would prefer to do this without parsing an entirely new HtmlDocument for efficiency reasons.</p>
  
  <p>If that is not possible, as a backup plan, how could I validate that the expression is actually relative to give feedback to the user? Would simply checking that it starts with <code>.</code> cover all cases? I think there are some function calls that could complicate the validation.</p>