XPATH / PHP problem with latest ()

I tried to read various textbooks on the Internet, but I cannot succeed in solving this problem, which is easy to describe, but I can not imagine a solution.

Here is an example XML:

<AAA>
    <BBB>
        <CCC>1</CCC>
        <CCC>2</CCC>
    </BBB>
    <BBB>
        <CCC>3</CCC>
        <CCC>4</CCC>
    </BBB>
</AAA>

I want to be able to select the last one <CCC>in each container ... in other words, I want to be able to pull <CCC>2</CCC>and <CCC>4</CCC>. I tried countless expressions to achieve this, but the best thing I can do is get the last one <CCC>(which contains 4).

It looks like // BBB / CCC [last ()] should work, but apparently it selects all CCCs that are children of BBB (total 4), and only then processes the last () predicate - and only for that returns one element <CCC>4</CCC>

! - ? !

+3
1
/AAA/BBB/CCC[position() = last()]

//BBB/CCC[position() = last()] //CCC[position() = last()] . xpath : http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm

+5

Source: https://habr.com/ru/post/1722296/


All Articles