1

I read the word context very often in the documents about XPath. I would like to get a conceptual idea about it and compare that with what I understand of it or its general meaning.

Currently, by context I think of a set of nodes creating a scope in the hierarchical structure of XML.

for example

<doc>
   <header>
   </header>
   <content>
      <section1>
           it is a good boy
      </section1>
      <section2>
          this good is for export
      </section2>
   </content>
   <footer>
   </footer>
</doc>

In my own terms, for example //content/section1//* is a context in which the meaning of "good" differs with its meaning in the context of //content/section2//*

I don't know how much it correspond to its meaning in the documents of XPath or XSLT,...

Ahmad
  • 8,811
  • 11
  • 76
  • 141
  • 1
    As long as you want to get a conceptual idea about the word "context" you should not impose any meaning on the word "context" yourself. – Tomalak Jun 16 '15 at 14:24
  • 1
    @Tomalak one can ignore my meaning of it, I brought it just for comparison – Ahmad Jun 16 '15 at 14:29
  • Then explicitly specify it as comparison (or as your initial understanding you'd like confirmed or corrected). – Charles Duffy Jun 16 '15 at 15:24
  • @CharlesDuffy you mean to change the question title or body? I used "concept" in the title to avoid technical details – Ahmad Jun 16 '15 at 15:31
  • Yes, but then inside the question you say "context is X". You don't say "I currently believe context to be X", but you claim that it *is* something, ie. that you already know the answer. That makes your question unclear: If you already know "context" to have a meaning, then what are you asking for? – Charles Duffy Jun 16 '15 at 15:32
  • @CharlesDuffy Thank you, better now? – Ahmad Jun 16 '15 at 15:38

2 Answers2

1

Per the docs:

Expression evaluation occurs with respect to a context. XSLT and XPointer specify how the context is determined for XPath expressions used in XSLT and XPointer respectively. The context consists of:

  • a node (the context node)
  • a pair of non-zero positive integers (the context position and the context size)
  • a set of variable bindings
  • a function library
  • the set of namespace declarations in scope for the expression

The context position is always less than or equal to the context size.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

It's very simple. Context is what predicates and location steps implicitly refer to.

  • Predicates are the expressions square brackets.

  • Location steps are, sloppily speaking, the bits that are separated by forward slashes.

  • Here's a more thorough explanation of XPath terms: https://stackoverflow.com/a/2990317/18771

So, for example:

  • //a selects any <a> element.

  • //a[@name = 'foo'] selects any <a> element whose @name attribute has a value of 'foo'.

    • Context for the location step in this expression is again the root node.
    • Context for the predicate is any selected <a>.
  • Et cetera. Context is different for every sub-expression.

//a[@name = 'foo'] is equivalent to //a[./@name = 'foo'], where context is set explicitly via .. Implicit context allows you to skip writing ./ every time you want to refer to that node you mean.

.//a selects all nodes beneath the current node. The current node is a concept that exists in environments that host XPath, for example XSLT or any DOM API that supports XPath. The current node can be used as context for the initial step of the path.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    Thank you, it was close to my understanding and its general meaning, you mean each step provide a context for the rest of the path? – Ahmad Jun 16 '15 at 14:39
  • 1
    Not exactly - context is not "for the rest of the path", it is only for the immediately following single bit of the path. The selected nodes in every step provide context for the immediately following step. – Tomalak Jun 16 '15 at 14:40
  • Can we say a context specify a domain which the next step can choose a node or a new context from? – Ahmad Jun 16 '15 at 14:55
  • That would be another good definition. If you want to avoid the word "domain" - context is always exactly a single node. If a location step selects 10 nodes and a predicate is applied to those nodes, every time the predicate runs, one of those 10 nodes is the context. – Tomalak Jun 16 '15 at 15:00
  • I see, what you point seems technical and how evaluation occurs. it choose each of the 10 nodes and match the predicate against it to filter some nodes. right? – Ahmad Jun 16 '15 at 15:18
  • 1
    @Ahmad, that's an implementation detail. Sometimes an implementation will have an index it can use for hash-based joins to do more efficient filtering, as opposed to looping over a source sequence as such. However, the *effect* will be the same as if the predicate were evaluated with each possibility as the context node. – Charles Duffy Jun 16 '15 at 15:21
  • I don't think the first two "Context for ..." bullet points are technically correct. But it may not matter to the OP. Keep in mind, e.g. that `//a` represents multiple location steps: `/descendant-or-self::node()/a`. The context for the first `/` is *whatever the context node currently is*; then `/` uses that context node to determine what document to start at the root of. The context for `descendant-or-self::node()` is the root of the document containing the (original) context node. – LarsH Jun 16 '15 at 20:22
  • @LarsH No, the context for an XPath expression starting with `/` is the root node. (I left off the `/descendant-or-self::node()/a` for being irrelevant in the situation.) – Tomalak Jun 17 '15 at 03:46
  • Can you define, then, what you mean by "the context for an XPath expression"? The root note is not (in general) the context node when the `/...` expression begins to be evaluated, but it is the context node after the initial `/` is evaluated. – LarsH Jun 17 '15 at 09:29
  • @LarsH Exactly, "the context for an XPath expression starting with `/` is the root node" – i.e., after the initial `/` is evaluated, the context ist the root node. :) – Tomalak Jun 17 '15 at 09:34
  • OK... I would call that "When an XPath expression starts with `/`, the context for **the location step after the `/`** is the root node." It seems misleading to say that the context for `a/b/c` is `a`. To me, the context for `a/b/c` is what ever the context was *before* the first location step. The context for `b` in that expression is `a`. – LarsH Jun 17 '15 at 09:35
  • But it would be hard to find sources for the semantics here. I would guess the concept "the context for XPath expression X" is not something used much, since the context is independent of what XPath expressions are evaluated in it. – LarsH Jun 17 '15 at 09:43
  • @LarsH Yes. I think we've agreed all along; I've been a bit more nonchalant with my explanation in an attempt to make the general concept clearer to the OP. – Tomalak Jun 17 '15 at 09:43
  • 1
    OK, point taken. I will think about and improve the wording in my answer. I attempted to make that difference with my last paragraph, obviously I failed. – Tomalak Jun 17 '15 at 09:44
  • @LarsH After some back and forth thinking I don't believe I can come up with a concise way to express the technicality of the matter that does not end up as a de-facto copy of the comments. So I've just explicitly linked to the comments instead. – Tomalak Jun 18 '15 at 15:48