Skip to content

Commit

Permalink
fix attribute output
Browse files Browse the repository at this point in the history
  • Loading branch information
spali committed Jan 28, 2021
1 parent 5873934 commit 6e111d7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
11 changes: 8 additions & 3 deletions xq.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,14 @@ func query(reader io.ReadCloser, xpath string) error {
return fmt.Errorf("%w: %s", ErrXMLQuery, err)
}
for _, elem := range list {
// prevent output of pseudo tags if root element was selected
isRoot := elem.Parent != nil
fmt.Printf("%s\n", elem.OutputXML(isRoot))
if elem.Type == xmlquery.AttributeNode {
// do not nest attribute node in a tag
fmt.Printf("%s\n", elem.InnerText())
} else {
// prevent output of pseudo tags if root element was selected
isRoot := elem.Parent != nil
fmt.Printf("%s\n", elem.OutputXML(isRoot))
}
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions xq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func Test_query(t *testing.T) {
{"test root element specifically queried by name", args{newStringReader("<abc></abc>"), "/abc"}, "<abc></abc>", nil},
{"test root element specifically queried", args{newStringReader("<abc></abc>"), "/"}, "<?xml?><abc></abc>", nil},
{"test root element queried", args{newStringReader("<abc></abc>"), "/*"}, "<abc></abc>", nil},
{"test attribute node", args{newStringReader("<abc id=\"test\"></abc>"), "/abc/@id"}, "test", nil},
}

for _, tt := range tests {
Expand Down

0 comments on commit 6e111d7

Please sign in to comment.