diff options
author | Nicolas Piganeau <nicolas.piganeau@ndp-systemes.fr> | 2017-08-04 12:49:03 +0200 |
---|---|---|
committer | Brett Vickers <beevik@users.noreply.github.com> | 2017-08-09 16:35:59 -0700 |
commit | 49a60cf39919cd8765e8c34e366dcbb914c8499e (patch) | |
tree | 576c4d8fa582318648ef4a88d9d34ba6286860ae | |
parent | 09746331a38f3da8c023a85a65c8c152070de725 (diff) | |
download | go-etree-49a60cf39919cd8765e8c34e366dcbb914c8499e.tar.gz |
Added [text()] syntax to retrieve all elements with non empty text
-rw-r--r-- | path.go | 18 | ||||
-rw-r--r-- | path_test.go | 6 |
2 files changed, 22 insertions, 2 deletions
@@ -26,6 +26,7 @@ only the following limited syntax is supported: [tag] Selects all elements with a child element named tag [tag='val'] Selects all elements with a child element named tag and text matching val + [text()] Selects all elements with non empty text [text()='val'] Selects all elements whose text matches val Examples: @@ -278,10 +279,12 @@ func (c *compiler) parseFilter(path string) filter { } } - // Filter contains [@attr], [N] or [tag] + // Filter contains [@attr], [N], [tag] or [text()] switch { case path[0] == '@': return newFilterAttr(path[1:]) + case strings.HasPrefix(path, "text()"): + return newFilterTextAll() case isInteger(path): pos, _ := strconv.Atoi(path) switch { @@ -432,14 +435,25 @@ func (f *filterAttrVal) apply(p *pather) { // text equal to the specified value. type filterTextVal struct { val string + all bool } func newFilterTextVal(value string) *filterTextVal { - return &filterTextVal{value} + return &filterTextVal{val: value} +} + +func newFilterTextAll() *filterTextVal { + return &filterTextVal{all: true} } func (f *filterTextVal) apply(p *pather) { for _, c := range p.candidates { + if f.all { + if c.Text() != "" { + p.scratch = append(p.scratch, c) + } + continue + } if c.Text() == f.val { p.scratch = append(p.scratch, c) } diff --git a/path_test.go b/path_test.go index 12a4e21..ce6338c 100644 --- a/path_test.go +++ b/path_test.go @@ -17,6 +17,7 @@ var testXML = ` <author>Giada De Laurentiis</author> <year>2005</year> <p:price>30.00</p:price> + <editor>Clarkson Potter</editor> </book> <book category="CHILDREN"> @@ -24,6 +25,8 @@ var testXML = ` <author>J K. Rowling</author> <year>2005</year> <p:price>29.99</p:price> + <editor></editor> + <editor/> </book> <book category="WEB"> @@ -35,6 +38,8 @@ var testXML = ` <author>Vaidyanathan Nagarajan</author> <year>2003</year> <p:price>49.99</p:price> + <editor> + </editor> </book> <!-- Final book --> @@ -95,6 +100,7 @@ var tests = []test{ {"//book[price='29.99']/title", "Harry Potter"}, {"//book/price[text()='29.99']", "29.99"}, {"//book/author[text()='Kurt Cagle']", "Kurt Cagle"}, + {"//book/editor[text()]", []string{"Clarkson Potter", "\n\t\t"}}, // attribute queries {"./bookstore/book[@category='WEB']/title", []string{"XQuery Kick Start", "Learning XML"}}, |