Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: Cannot query all nodes using indices #104

Closed
srebhan opened this issue Oct 1, 2024 · 1 comment
Closed

Regression: Cannot query all nodes using indices #104

srebhan opened this issue Oct 1, 2024 · 1 comment
Labels

Comments

@srebhan
Copy link
Contributor

srebhan commented Oct 1, 2024

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
@srebhan
Copy link
Contributor Author

srebhan commented Oct 7, 2024

Thanks @zhengchun! I can confirm that this solves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants