When using a query such as //phones[1]/number it is expected to return the first element of each finding of the phones node in subtrees (see xpath playground).
With the following code
package main
import (
"fmt"
"strings"
"github.com/antchfx/xmlquery"
)
const xml string = `
<?xml version="1.0"?>
<root>
<people>
<age>42</age>
<email>[email protected]</email>
<id>101</id>
<name>John Doe</name>
</people>
<people>
<age>40</age>
<id>102</id>
<name>Jane Doe</name>
</people>
<people>
<age>12</age>
<email>[email protected]</email>
<id>201</id>
<name>Jack Doe</name>
<phones>
<number>555-555-5555</number>
<type>2</type>
</phones>
</people>
<people>
<age>19</age>
<email>[email protected]</email>
<id>301</id>
<name>Jack Buck</name>
<phones>
<number>555-555-0000</number>
<type>1</type>
</phones>
<phones>
<number>555-555-0001</number>
</phones>
<phones>
<number>555-555-0002</number>
<type>2</type>
</phones>
</people>
<people>
<age>16</age>
<email>[email protected]</email>
<id>1001</id>
<name>Janet Doe</name>
<phones>
<number>555-777-0000</number>
</phones>
<phones>
<number>555-777-0001</number>
<type>1</type>
</phones>
</people>
<tags>home</tags>
<tags>private</tags>
<tags>friends</tags>
</root>
`
func main() {
doc, err := xmlquery.Parse(strings.NewReader(xml))
if err != nil {
panic(err)
}
nodes, err := xmlquery.QueryAll(doc, "//phones[1]/number")
if err != nil {
panic(err)
}
for i, n := range nodes {
fmt.Printf("node %d: %s=%v\n", i, n.Data, n.InnerText())
}
}
we see
node 0: number=555-555-5555
node 1: number=555-555-0000
node 2: number=555-777-0000
up to version v1.2.5 of this package. However, commit ed2f6ee changes the behavior and now the code only produces
node 0: number=555-555-5555
When using a query such as
//phones[1]/numberit is expected to return the first element of each finding of thephonesnode in subtrees (see xpath playground).With the following code
we see
up to version v1.2.5 of this package. However, commit ed2f6ee changes the behavior and now the code only produces