Re: [xml] How could I understand the slightly difference of the parsing process of E1/E2[E3] and E1[E2][E3]?

Ming Chen wrote:
Hi Experts,
Recently a colleague and I have disagreed on the parsing process of the E1/E2[E3], where E3 has a numeric type. Here is the example XML file:
<?xml version="1.0" encoding="UTF-8"?>
<rec id="1">
<para type="error" position="11"/>
<para type="warning" position="12"/>
<para type="warning" position="13"/>
<rec id="2">
<para type="warning" position="21"/>
<para type="warning" position="22"/>
<para type="warning" position="23"/>
<rec id="3">
<para type="info" position="31"/>
<para type="warning" position="32"/>
<para type="warning" position="33"/>
For XPath expression "//rec/para[1]", xmllint.exe outputs:
<para type="error" position="11"/><para type="warning" position="21"/><para type="info" position="31"/> While my colleague said that the output should be: <para type="error" position="11"/>

Your colleague is is interpreting the xpath expression as ( //rec/para ) [1]
rather than
//rec / (para[1])

See section 2.1 of the XPath (1.0) spec; The predicate,
in this case [1], is part of the "step".
So //rec selects a set of <rec> nodes, then for each, the
next step "para[1]" is applied and the union is formed.
Not that the predicate [1] is applied to the union.


So, what's the nice distinction between E1/E2[E3] and E1[E2][E3].

The first xpath has 2 steps, where the 2nd step has one predicate.
The second xpath has 1 step which has 2 predicates.
When there are multiple predicates, they are applied
left to right as if they were successive filters.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]