Fix a bug in path parsing.
Paths containing quoted text with a forward-slash in them were being
parsed improperly. This bug has been fixed.
diff --git a/path.go b/path.go
index a8271a0..126eb15 100644
--- a/path.go
+++ b/path.go
@@ -189,7 +189,7 @@
// Split path into segment objects
var segments []segment
- for _, s := range strings.Split(path, "/") {
+ for _, s := range splitPath(path) {
segments = append(segments, c.parseSegment(s))
if c.err != ErrPath("") {
break
@@ -198,6 +198,21 @@
return segments
}
+func splitPath(path string) []string {
+ pieces := make([]string, 0)
+ start := 0
+ inquote := false
+ for i := 0; i+1 <= len(path); i++ {
+ if path[i] == '\'' {
+ inquote = !inquote
+ } else if path[i] == '/' && !inquote {
+ pieces = append(pieces, path[start:i])
+ start = i + 1
+ }
+ }
+ return append(pieces, path[start:])
+}
+
// parseSegment parses a path segment between / characters.
func (c *compiler) parseSegment(path string) segment {
pieces := strings.Split(path, "[")
diff --git a/path_test.go b/path_test.go
index b46025f..60abf57 100644
--- a/path_test.go
+++ b/path_test.go
@@ -38,7 +38,7 @@
</book>
<!-- Final book -->
- <book category="WEB">
+ <book category="WEB" path="/books/xml">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
@@ -96,6 +96,7 @@
// attribute queries
{"./bookstore/book[@category='WEB']/title", []string{"XQuery Kick Start", "Learning XML"}},
+ {"./bookstore/book[@path='/books/xml']/title", []string{"Learning XML"}},
{"./bookstore/book[@category='COOKING']/title[@lang='en']", "Everyday Italian"},
{"./bookstore/book/title[@lang='en'][@sku='150']", "Harry Potter"},
{"./bookstore/book/title[@lang='fr']", nil},