Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type ancestorQuery struct {
name string
iterator func() NodeNavigator
table map[uint64]bool
pos int

Self bool
Input query
Expand All @@ -164,6 +165,8 @@ func (a *ancestorQuery) Select(t iterator) NodeNavigator {
if node == nil {
return nil
}
// Reset position for a new input context node
a.pos = 0
first := true
node = node.Copy()
a.iterator = func() NodeNavigator {
Expand All @@ -186,6 +189,8 @@ func (a *ancestorQuery) Select(t iterator) NodeNavigator {
node_id := getHashCode(node.Copy())
if _, ok := a.table[node_id]; !ok {
a.table[node_id] = true
// Increase position for each matched node in current input context
a.pos++
return node
}
}
Expand Down Expand Up @@ -215,6 +220,13 @@ func (a *ancestorQuery) Properties() queryProp {
return queryProps.Position | queryProps.Count | queryProps.Cached | queryProps.Merge | queryProps.Reverse
}

// position returns the ordinal of the current matched node within the axis
// traversal for the current input context node. This is required so numeric
// predicates like [1] or [2] on the ancestor axis resolve in axis order.
func (a *ancestorQuery) position() int {
return a.pos
}

// attributeQuery is an XPath attribute node query.(@*)
type attributeQuery struct {
name string
Expand Down
33 changes: 33 additions & 0 deletions xpath_axes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ func Test_ancestor(t *testing.T) {
//test_xpath_elements(t, employee_example, `//ancestor::name`, 4, 9, 14)
}

func Test_ancestor_predicate(t *testing.T) {
doc := createElement(0, "",
createElement(1, "html",
createElement(2, "body",
createElement(3, "h1"),
createElement(4, "section",
createElement(5, "div",
createElement(6, "section",
createElement(7, "div",
createElement(8, "span"),
),
),
),
),
createElement(9, "section",
createElement(10, "div",
createElement(11, "section",
createElement(12, "div",
createElement(13, "span"),
),
),
),
),
),
),
)

test_xpath_elements(t, doc, `//span/ancestor::*`, 7, 6, 5, 4, 2, 1, 12, 11, 10, 9)
test_xpath_elements(t, doc, `//span/ancestor::section`, 6, 4, 11, 9)
test_xpath_elements(t, doc, `//span/ancestor::section[1]`, 6, 11)
test_xpath_elements(t, doc, `//span/ancestor::section[2]`, 4, 9)
}

func Test_ancestor_or_self(t *testing.T) {
// Expected the value is [2, 3, 8, 13], but got [3, 2, 8, 13]
test_xpath_elements(t, employee_example, `//employee/ancestor-or-self::*`, 3, 2, 8, 13)
Expand Down
20 changes: 20 additions & 0 deletions xpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,26 @@ func (n *TNode) getAttribute(key string) string {
return ""
}

func createElement(line int, name string, children ...*TNode) *TNode {
nodeType := ElementNode
if name == "" {
nodeType = RootNode
}
n := createNode(name, nodeType)
n.lines = line
for _, c := range children {
c.Parent = n
c.PrevSibling = n.LastChild
if c.PrevSibling == nil {
n.FirstChild = c
} else {
c.PrevSibling.NextSibling = c
}
n.LastChild = c
}
return n
}

func createBookExample() *TNode {
/*
<?xml version="1.0" encoding="UTF-8"?>
Expand Down