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

Chained xpaths are searching from root level #237

Open
anivaros opened this issue Apr 12, 2020 · 1 comment
Open

Chained xpaths are searching from root level #237

anivaros opened this issue Apr 12, 2020 · 1 comment
Labels
Milestone

Comments

@anivaros
Copy link
Contributor

Description:

All node xpaths are calling for root document level, not for node. For example, this test will fail.

func testInnerXpath() {
    let input = """
                <html>
                <head>
                    <title>test title</title>
                </head>
                <body>
                    <div id="1"><div><h1>test header 1</h1></div></div>
                    <div id="2"><div><h1>test header 2</h1></div></div>
                </body>
                </html>
                """
    do {
        let doc = try HTML(html: input, encoding: .utf8)
        //all this asserts will fail:
        XCTAssertNil(doc.at_xpath("//head")?.at_xpath("//h1")?.toHTML)
        XCTAssertNil(doc.at_xpath("//head")?.at_xpath("//body")?.toHTML)
        XCTAssertNil(doc.at_xpath("//body")?.at_xpath("//title")?.toHTML)
        XCTAssertEqual(doc.at_xpath("//body/div[@id='2']")?.at_xpath("//h1")?.text, "test header 2")
        //only this assert is ok, passes:
        XCTAssertEqual(doc.at_xpath("//body/div[@id='2']//h1")?.text, "test header 2")
    } catch {
        XCTFail("Abnormal test data")
    }
}

Is it bug or feature?

I've started implementing fix of this problem I'm casting xmlNodePtr to xmlDocPtr and initing xmlXPathNewContext with this casted object and then all xpaths starting work properly.

@tid-kijyun
Copy link
Owner

To define a relative path you have to use dot-notation(.//).

XCTAssertNil(doc.at_xpath("//head")?.at_xpath(".//h1")?.toHTML)
XCTAssertNil(doc.at_xpath("//head")?.at_xpath(".//body")?.toHTML)
XCTAssertNil(doc.at_xpath("//body")?.at_xpath(".//title")?.toHTML)
XCTAssertEqual(doc.at_xpath("//body/div[@id='2']")?.at_xpath(".//h1")?.text, "test header 2")

//only this assert is ok, passes:
XCTAssertEqual(doc.at_xpath("//body/div[@id='2']//h1")?.text, "test header 2")

I understand this is confusing. I plan to change it so that it works even if dot notation is omitted in a future release.

@tid-kijyun tid-kijyun added this to the 5.2.3 milestone Apr 12, 2020
anivaros added a commit to anivaros/Kanna that referenced this issue Apr 14, 2020
anivaros added a commit to anivaros/Kanna that referenced this issue Apr 15, 2020
anivaros added a commit to anivaros/Kanna that referenced this issue Apr 18, 2020
anivaros added a commit to anivaros/Kanna that referenced this issue Apr 18, 2020
anivaros added a commit to anivaros/Kanna that referenced this issue Apr 18, 2020
tid-kijyun added a commit that referenced this issue Apr 19, 2020
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