From 5a154602e79d035e40b2adc22b007550aa97a613 Mon Sep 17 00:00:00 2001 From: zhengchun Date: Fri, 29 Mar 2024 20:04:43 +0800 Subject: [PATCH] #72 , keeping the duplicate elements on query with parent `../..` --- query.go | 11 +---------- xpath_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/query.go b/query.go index d5a58ca..7a0063c 100644 --- a/query.go +++ b/query.go @@ -680,16 +680,11 @@ func (p *precedingQuery) position() int { // parentQuery is an XPath parent node query.(parent::*) type parentQuery struct { - table map[uint64]bool Input query Predicate func(NodeNavigator) bool } func (p *parentQuery) Select(t iterator) NodeNavigator { - if p.table == nil { - p.table = make(map[uint64]bool) - } - for { node := p.Input.Select(t) if node == nil { @@ -697,11 +692,7 @@ func (p *parentQuery) Select(t iterator) NodeNavigator { } node = node.Copy() if node.MoveToParent() && p.Predicate(node) { - id := getHashCode(node.Copy()) - if _, ok := p.table[id]; !ok { - p.table[id] = true - return node - } + return node } } } diff --git a/xpath_test.go b/xpath_test.go index b481f88..138a639 100644 --- a/xpath_test.go +++ b/xpath_test.go @@ -63,6 +63,57 @@ func test_xpath_eval(t *testing.T, root *TNode, expr string, expected ...interfa assertEqual(t, expected[0], v) } +func Test_Predicates_MultiParent(t *testing.T) { + // https://github.com/antchfx/xpath/issues/75 + /* + + + + field1 + field2 + + 31854 + 159773 + + + + field3 + field4 + + 1234 + 567 + + + + + */ + doc := createNode("", RootNode) + measCollecFile := doc.createChildNode("measCollecFile", ElementNode) + measData := measCollecFile.createChildNode("measData", ElementNode) + data := []struct { + measType map[string]string + measValue map[string]string + }{ + {measType: map[string]string{"1": "field1", "2": "field2"}, measValue: map[string]string{"1": "31854", "2": "159773"}}, + {measType: map[string]string{"1": "field3", "2": "field4"}, measValue: map[string]string{"1": "1234", "2": "567"}}, + } + for _, d := range data { + measInfo := measData.createChildNode("measInfo", ElementNode) + measType := measInfo.createChildNode("measType", ElementNode) + + for key, value := range d.measType { + measType.addAttribute("p", key) + measType.createChildNode(value, TextNode) + } + measValue := measInfo.createChildNode("measValue", ElementNode) + for key, value := range d.measValue { + r := measValue.createChildNode("r", ElementNode) + r.addAttribute("p", key) + r.createChildNode(value, TextNode) + } + } + test_xpath_values(t, doc, `//r[@p=../../measType/@p]`, "31854", "159773", "1234", "567") +} func TestCompile(t *testing.T) { var err error _, err = Compile("//a")