From c848e4dd9e8062dd294ff11ae3e0cf63c12e8ce3 Mon Sep 17 00:00:00 2001 From: beevik Date: Sun, 1 Dec 2013 12:09:37 -0800 Subject: [PATCH] More documentation. --- README.md | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4eef7ee..9bc221c 100644 --- a/README.md +++ b/README.md @@ -26,18 +26,18 @@ doc.Indent(2) doc.WriteTo(os.Stdout) ``` -Here is the output of the program: -``` - - - - - - - +Output +```xml + + + + + + + ``` -###Sample document source +###Document used by remaining examples: For the remaining examples, we will be using the following `bookstore.xml` document as the source. ```xml @@ -90,10 +90,10 @@ if err := doc.ReadFromFile("test.xml"); err != nil { root := doc.SelectElement("bookstore") fmt.Println("ROOT element:", root.Tag) -for _, c := range root.ChildElements() { - fmt.Println("CHILD element:", c.Tag) - for _, a := range c.Attr { - fmt.Printf(" ATTR: %s=%s\n", a.Key, a.Value) +for _, book := range root.SelectElements("book") { + fmt.Println("CHILD element:", book.Tag) + for _, attr := range book.Attr { + fmt.Printf(" ATTR: %s=%s\n", attr.Key, attr.Value) } } ``` @@ -108,4 +108,23 @@ CHILD element: book ATTR: category=WEB CHILD element: book ATTR: category=WEB -``` \ No newline at end of file +``` + +###Example: Path queries + +This example processes the bookstore XML document using Path queries: +```go +doc := etree.NewDocument() +if err := doc.ReadFromFile("bookstore.xml"); err != nil { + panic(err) +} +for _, t := range doc.FindElements("//book[@category='WEB']/title") { + fmt.Println("Title:", t.Text()) +} +``` + +Output: +``` +Title: XQuery Kick Start +Title: Learning XML +```