1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
|
package xpath
import (
"bytes"
"fmt"
"math"
"sort"
"strings"
"testing"
)
var (
employee_example = createEmployeeExample()
book_example = createBookExample()
html_example = createHtmlExample()
empty_example = createNode("", RootNode)
mybook_example = createMyBookExample()
)
type testQuery string
func (t testQuery) Select(_ iterator) NodeNavigator {
panic("implement me")
}
func (t testQuery) Clone() query {
return t
}
func (t testQuery) Evaluate(_ iterator) interface{} {
return string(t)
}
func (t testQuery) ValueType() resultType {
return xpathResultType.Any
}
func (t testQuery) Properties() queryProp {
return queryProps.None
}
func test_xpath_elements(t *testing.T, root *TNode, expr string, expected ...int) {
result := selectNodes(root, expr)
assertEqual(t, len(expected), len(result))
for i := 0; i < len(expected); i++ {
assertEqual(t, expected[i], result[i].lines)
}
}
func test_xpath_values(t testing.TB, root *TNode, expr string, expected ...string) {
result := selectNodes(root, expr)
assertEqual(t, len(expected), len(result))
for i := 0; i < len(expected); i++ {
assertEqual(t, expected[i], result[i].Value())
}
}
func test_xpath_tags(t *testing.T, root *TNode, expr string, expected ...string) {
result := selectNodes(root, expr)
assertEqual(t, len(expected), len(result))
for i := 0; i < len(expected); i++ {
assertEqual(t, expected[i], result[i].Data)
}
}
func test_xpath_count(t *testing.T, root *TNode, expr string, expected int) {
result := selectNodes(root, expr)
assertEqual(t, expected, len(result))
}
func test_xpath_eval(t *testing.T, root *TNode, expr string, expected ...interface{}) {
e, err := Compile(expr)
assertNoErr(t, err)
v := e.Evaluate(createNavigator(root))
// if is a node-set
if iter, ok := v.(*NodeIterator); ok {
got := iterateNavs(iter)
assertEqual(t, len(expected), len(got))
for i := 0; i < len(expected); i++ {
assertEqual(t, expected[i], got[i])
}
return
}
assertEqual(t, expected[0], v)
}
func Test_Predicates_MultiParent(t *testing.T) {
// https://github.com/antchfx/xpath/issues/75
/*
<measCollecFile xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec">
<measData>
<measInfo>
<measType p="1">field1</measType>
<measType p="2">field2</measType>
<measValue>
<r p="1">31854</r>
<r p="2">159773</r>
</measValue>
</measInfo>
<measInfo measInfoId="metric_name2">
<measType p="1">field3</measType>
<measType p="2">field4</measType>
<measValue>
<r p="1">1234</r>
<r p="2">567</r>
</measValue>
</measInfo>
</measData>
</measCollecFile>
*/
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 j := 0; j < len(data); j++ {
d := data[j]
measInfo := measData.createChildNode("measInfo", ElementNode)
measType := measInfo.createChildNode("measType", ElementNode)
var keys []string
for k := range d.measType {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
measType.addAttribute("p", k)
measType.createChildNode(d.measType[k], TextNode)
}
measValue := measInfo.createChildNode("measValue", ElementNode)
keys = make([]string, 0)
for k := range d.measValue {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
r := measValue.createChildNode("r", ElementNode)
r.addAttribute("p", k)
r.createChildNode(d.measValue[k], 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")
assertNil(t, err)
_, err = Compile("//a[id=']/span")
assertErr(t, err)
_, err = Compile("//ul/li/@class")
assertNil(t, err)
_, err = Compile("/a/b/(c, .[not(c)])")
assertNil(t, err)
_, err = Compile("/pre:foo")
assertNil(t, err)
}
func TestInvalidXPath(t *testing.T) {
var err error
_, err = Compile("()")
assertErr(t, err)
_, err = Compile("(1,2,3)")
assertErr(t, err)
}
func TestCompileWithNS(t *testing.T) {
_, err := CompileWithNS("/foo", nil)
assertNil(t, err)
_, err = CompileWithNS("/foo", map[string]string{})
assertNil(t, err)
_, err = CompileWithNS("/foo", map[string]string{"a": "b"})
assertNil(t, err)
_, err = CompileWithNS("/a:foo", map[string]string{"a": "b"})
assertNil(t, err)
_, err = CompileWithNS("/u:foo", map[string]string{"a": "b"})
assertErr(t, err)
}
func TestNamespacePrefixQuery(t *testing.T) {
/*
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>book1</book>
<b:book xmlns:b="ns">book2</b:book>
<c:book xmlns:c="ns">book3</c:book>
</books>
*/
doc := createNode("", RootNode)
books := doc.createChildNode("books", ElementNode)
books.lines = 2
book1 := books.createChildNode("book", ElementNode)
book1.lines = 3
book1.createChildNode("book1", TextNode)
book2 := books.createChildNode("b:book", ElementNode)
book2.lines = 4
book2.addAttribute("xmlns:b", "ns")
book2.createChildNode("book2", TextNode)
book3 := books.createChildNode("c:book", ElementNode)
book3.lines = 5
book3.addAttribute("xmlns:c", "ns")
book3.createChildNode("book3", TextNode)
test_xpath_elements(t, doc, `//b:book`, 4) // expected [4 , 5]
// With namespace bindings:
exp, _ := CompileWithNS("//x:book", map[string]string{"x": "ns"})
nodes := iterateNodes(exp.Select(createNavigator(doc)))
assertEqual(t, 2, len(nodes))
assertEqual(t, "book2", nodes[0].Value())
assertEqual(t, "book3", nodes[1].Value())
}
func TestMustCompile(t *testing.T) {
expr := MustCompile("//")
assertTrue(t, expr != nil)
if wanted := (nopQuery{}); expr.q != wanted {
t.Fatalf("wanted nopQuery object but got %s", expr)
}
iter := expr.Select(createNavigator(html_example))
if iter.MoveNext() {
t.Fatal("should be an empty node list but got one")
}
}
func Test_plusFunc(t *testing.T) {
// 1+1
assertEqual(t, float64(2), plusFunc(nil, float64(1), float64(1)))
// string +
assertEqual(t, float64(2), plusFunc(nil, "1", "1"))
// invalid string
v := plusFunc(nil, "a", 1)
assertTrue(t, math.IsNaN(v.(float64)))
// Nodeset
// TODO
}
func Test_minusFunc(t *testing.T) {
// 1 - 1
assertEqual(t, float64(0), minusFunc(nil, float64(1), float64(1)))
// string
assertEqual(t, float64(0), minusFunc(nil, "1", "1"))
// invalid string
v := minusFunc(nil, "a", 1)
assertTrue(t, math.IsNaN(v.(float64)))
}
func TestNodeType(t *testing.T) {
tests := []struct {
expr string
expected NodeType
}{
{`//employee`, ElementNode},
{`//name[text()]`, ElementNode},
{`//name/text()`, TextNode},
{`//employee/@id`, AttributeNode},
}
for _, test := range tests {
v := selectNode(employee_example, test.expr)
assertTrue(t, v != nil)
assertEqual(t, test.expected, v.Type)
}
doc := createNode("", RootNode)
doc.createChildNode("<!-- This is a comment -->", CommentNode)
n := selectNode(doc, "//comment()")
assertTrue(t, n != nil)
assertEqual(t, CommentNode, n.Type)
}
func iterateNavs(t *NodeIterator) []*TNodeNavigator {
var nodes []*TNodeNavigator
for t.MoveNext() {
node := t.Current().(*TNodeNavigator)
nodes = append(nodes, node)
}
return nodes
}
func iterateNodes(t *NodeIterator) []*TNode {
var nodes []*TNode
for t.MoveNext() {
n := t.Current().(*TNodeNavigator)
if n.NodeType() == AttributeNode {
childNode := &TNode{
Type: TextNode,
Data: n.Value(),
}
nodes = append(nodes, &TNode{
Parent: n.curr,
Type: AttributeNode,
Data: n.LocalName(),
FirstChild: childNode,
LastChild: childNode,
})
} else {
nodes = append(nodes, n.curr)
}
}
return nodes
}
func selectNode(root *TNode, expr string) *TNode {
list := selectNodes(root, expr)
if len(list) == 0 {
return nil
}
return list[0]
}
func selectNodes(root *TNode, expr string) []*TNode {
t := Select(createNavigator(root), expr)
c := make(map[uint64]bool)
var list []*TNode
for _, n := range iterateNodes(t) {
m := getHashCode(createNavigator(n))
if _, ok := c[m]; ok {
continue
}
c[m] = true
list = append(list, n)
}
return list
}
func joinValues(nodes []*TNode) string {
s := make([]string, 0)
for _, n := range nodes {
s = append(s, n.Value())
}
return strings.Join(s, ",")
}
func createNavigator(n *TNode) *TNodeNavigator {
return &TNodeNavigator{curr: n, root: n, attr: -1}
}
type Attribute struct {
Key, Value string
}
type TNode struct {
Parent, FirstChild, LastChild, PrevSibling, NextSibling *TNode
Type NodeType
Data string
Attr []Attribute
NamespaceURL string
Prefix string
lines int
}
func (n *TNode) Value() string {
if n.Type == TextNode {
return n.Data
}
var buff bytes.Buffer
var output func(*TNode)
output = func(node *TNode) {
if node.Type == TextNode {
buff.WriteString(node.Data)
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
output(child)
}
}
output(n)
return buff.String()
}
// TNodeNavigator is for navigating TNode.
type TNodeNavigator struct {
curr, root *TNode
attr int
}
func (n *TNodeNavigator) NodeType() NodeType {
switch n.curr.Type {
case CommentNode:
return CommentNode
case TextNode:
return TextNode
case ElementNode:
if n.attr != -1 {
return AttributeNode
}
return ElementNode
}
return n.curr.Type
}
func (n *TNodeNavigator) LocalName() string {
if n.attr != -1 {
return n.curr.Attr[n.attr].Key
}
name := n.curr.Data
if strings.Contains(name, ":") {
return strings.Split(name, ":")[1]
}
return name
}
func (n *TNodeNavigator) Prefix() string {
if n.attr == -1 && strings.Contains(n.curr.Data, ":") {
return strings.Split(n.curr.Data, ":")[0]
}
return n.curr.Prefix
}
func (n *TNodeNavigator) NamespaceURL() string {
if n.Prefix() != "" {
for _, a := range n.curr.Attr {
if a.Key == "xmlns:"+n.Prefix() {
return a.Value
}
}
}
return n.curr.NamespaceURL
}
func (n *TNodeNavigator) Value() string {
switch n.curr.Type {
case CommentNode:
return n.curr.Data
case ElementNode:
if n.attr != -1 {
return n.curr.Attr[n.attr].Value
}
var buf bytes.Buffer
node := n.curr.FirstChild
for node != nil {
if node.Type == TextNode {
buf.WriteString(strings.TrimSpace(node.Data))
}
node = node.NextSibling
}
return buf.String()
case TextNode:
return n.curr.Data
}
return ""
}
func (n *TNodeNavigator) Copy() NodeNavigator {
n2 := *n
return &n2
}
func (n *TNodeNavigator) MoveToRoot() {
n.curr = n.root
}
func (n *TNodeNavigator) MoveToParent() bool {
if n.attr != -1 {
n.attr = -1
return true
} else if node := n.curr.Parent; node != nil {
n.curr = node
return true
}
return false
}
func (n *TNodeNavigator) MoveToNextAttribute() bool {
if n.attr >= len(n.curr.Attr)-1 {
return false
}
n.attr++
return true
}
func (n *TNodeNavigator) MoveToChild() bool {
if n.attr != -1 {
return false
}
if node := n.curr.FirstChild; node != nil {
n.curr = node
return true
}
return false
}
func (n *TNodeNavigator) MoveToFirst() bool {
if n.attr != -1 || n.curr.PrevSibling == nil {
return false
}
for {
node := n.curr.PrevSibling
if node == nil {
break
}
n.curr = node
}
return true
}
func (n *TNodeNavigator) String() string {
return n.Value()
}
func (n *TNodeNavigator) MoveToNext() bool {
if n.attr != -1 {
return false
}
if node := n.curr.NextSibling; node != nil {
n.curr = node
return true
}
return false
}
func (n *TNodeNavigator) MoveToPrevious() bool {
if n.attr != -1 {
return false
}
if node := n.curr.PrevSibling; node != nil {
n.curr = node
return true
}
return false
}
func (n *TNodeNavigator) MoveTo(other NodeNavigator) bool {
node, ok := other.(*TNodeNavigator)
if !ok || node.root != n.root {
return false
}
n.curr = node.curr
n.attr = node.attr
return true
}
func createNode(data string, typ NodeType) *TNode {
return &TNode{Data: data, Type: typ, Attr: make([]Attribute, 0)}
}
func (n *TNode) createChildNode(data string, typ NodeType) *TNode {
m := createNode(data, typ)
m.Parent = n
if n.FirstChild == nil {
n.FirstChild = m
} else {
n.LastChild.NextSibling = m
m.PrevSibling = n.LastChild
}
n.LastChild = m
return m
}
func (n *TNode) appendNode(data string, typ NodeType) *TNode {
m := createNode(data, typ)
m.Parent = n.Parent
n.NextSibling = m
m.PrevSibling = n
if n.Parent != nil {
n.Parent.LastChild = m
}
return m
}
func (n *TNode) addAttribute(k, v string) {
n.Attr = append(n.Attr, Attribute{k, v})
}
func (n *TNode) getAttribute(key string) string {
for i := 0; i < len(n.Attr); i++ {
if n.Attr[i].Key == key {
return n.Attr[i].Value
}
}
return ""
}
func createBookExample() *TNode {
/*
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
*/
type Element struct {
Data string
Attributes map[string]string
}
books := []struct {
category string
title Element
year int
price float64
authors []string
}{
{
category: "cooking",
title: Element{"Everyday Italian", map[string]string{"lang": "en"}},
year: 2005,
price: 30.00,
authors: []string{"Giada De Laurentiis"},
},
{
category: "children",
title: Element{"Harry Potter", map[string]string{"lang": "en"}},
year: 2005,
price: 29.99,
authors: []string{"J K. Rowling"},
},
{
category: "web",
title: Element{"XQuery Kick Start", map[string]string{"lang": "en"}},
year: 2003,
price: 49.99,
authors: []string{"James McGovern", "Per Bothner", "Kurt Cagle", "James Linn", "Vaidyanathan Nagarajan"},
},
{
category: "web",
title: Element{"Learning XML", map[string]string{"lang": "en"}},
year: 2003,
price: 39.95,
authors: []string{"Erik T. Ray"},
},
}
var lines = 0
doc := createNode("", RootNode)
lines++
bookstore := doc.createChildNode("bookstore", ElementNode)
lines++
bookstore.lines = lines
for i := 0; i < len(books); i++ {
v := books[i]
lines++
book := bookstore.createChildNode("book", ElementNode)
book.lines = lines
lines++
book.addAttribute("category", v.category)
// title
title := book.createChildNode("title", ElementNode)
title.lines = lines
lines++
for k, v := range v.title.Attributes {
title.addAttribute(k, v)
}
title.createChildNode(v.title.Data, TextNode)
// authors
for j := 0; j < len(v.authors); j++ {
author := book.createChildNode("author", ElementNode)
author.lines = lines
lines++
author.createChildNode(v.authors[j], TextNode)
}
// year
year := book.createChildNode("year", ElementNode)
year.lines = lines
lines++
year.createChildNode(fmt.Sprintf("%d", v.year), TextNode)
// price
price := book.createChildNode("price", ElementNode)
price.lines = lines
lines++
price.createChildNode(fmt.Sprintf("%.2f", v.price), TextNode)
}
return doc
}
// The example document from https://way2tutorial.com/xml/xpath-node-test.php
func createEmployeeExample() *TNode {
/*
<?xml version="1.0" standalone="yes"?>
<empinfo>
<employee id="1">
<name>Opal Kole</name>
<designation discipline="web" experience="3 year">Senior Engineer</designation>
<email>OpalKole@myemail.com</email>
</employee>
<employee id="2">
<name from="CA">Max Miller</name>
<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
<email>maxmiller@email.com</email>
</employee>
<employee id="3">
<name>Beccaa Moss</name>
<designation discipline="appdev">Application Developer</designation>
<email>beccaamoss@email.com</email>
</employee>
</empinfo>
*/
type Element struct {
Data string
Attributes map[string]string
}
var lines = 0
doc := createNode("", RootNode)
lines++ // 1
empinfo := doc.createChildNode("empinfo", ElementNode)
lines++
empinfo.lines = lines
var employees = []struct {
name Element
designation Element
email Element
}{
{
name: Element{Data: "Opal Kole"},
designation: Element{Data: "Senior Engineer", Attributes: map[string]string{
"discipline": "web",
"experience": "3 year",
}},
email: Element{Data: "OpalKole@myemail.com"},
},
{
name: Element{Data: "Max Miller", Attributes: map[string]string{"from": "CA"}},
designation: Element{Data: "DBA Engineer", Attributes: map[string]string{
"discipline": "DBA",
"experience": "2 year",
}},
email: Element{Data: "maxmiller@email.com"},
},
{
name: Element{Data: "Beccaa Moss"},
designation: Element{Data: "Application Developer", Attributes: map[string]string{
"discipline": "appdev",
}},
email: Element{Data: "beccaamoss@email.com"},
},
}
for i := 0; i < len(employees); i++ {
v := employees[i]
lines++
// employee
employee := empinfo.createChildNode("employee", ElementNode)
employee.addAttribute("id", fmt.Sprintf("%d", i+1))
employee.lines = lines
lines++
// name
name := employee.createChildNode("name", ElementNode)
name.createChildNode(v.name.Data, TextNode)
for k, n := range v.name.Attributes {
name.addAttribute(k, n)
}
name.lines = lines
lines++
// designation
designation := employee.createChildNode("designation", ElementNode)
designation.createChildNode(v.designation.Data, TextNode)
for k, n := range v.designation.Attributes {
designation.addAttribute(k, n)
}
designation.lines = lines
lines++
// email
email := employee.createChildNode("email", ElementNode)
email.createChildNode(v.email.Data, TextNode)
for k, n := range v.email.Attributes {
email.addAttribute(k, n)
}
email.lines = lines
// skiping closed tag
lines++
}
return doc
}
func createHtmlExample() *TNode {
/*
<html lang="en">
<head>
<title>My page</title>
<meta name="language" content="en" />
</head>
<body>
<h2>Welcome to my page</h2>
<ul>
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/about">About</a>
</li>
<li>
<a href="/account">Login</a>
</li>
<li></li>
</ul>
<p>This is the first paragraph.</p>
<!-- this is the end -->
</body>
</html>
*/
lines := 0
doc := createNode("", RootNode)
lines++
xhtml := doc.createChildNode("html", ElementNode)
xhtml.lines = lines
xhtml.addAttribute("lang", "en")
lines++
// head container
head := xhtml.createChildNode("head", ElementNode)
head.lines = lines
lines++
title := head.createChildNode("title", ElementNode)
title.lines = lines
title.createChildNode("My page", TextNode)
lines++
meta := head.createChildNode("meta", ElementNode)
meta.lines = lines
meta.addAttribute("name", "language")
meta.addAttribute("content", "en")
// skip the head
lines++
lines++
body := xhtml.createChildNode("body", ElementNode)
body.lines = lines
lines++
h2 := body.createChildNode("h2", ElementNode)
h2.lines = lines
h2.createChildNode("Welcome to my page", TextNode)
lines++
links := []struct {
text string
href string
}{
{text: "Home", href: "/"},
{text: "About", href: "/About"},
{text: "Login", href: "/account"},
}
ul := body.createChildNode("ul", ElementNode)
ul.lines = lines
lines++
for i := 0; i < len(links); i++ {
link := links[i]
li := ul.createChildNode("li", ElementNode)
li.lines = lines
lines++
a := li.createChildNode("a", ElementNode)
a.lines = lines
a.addAttribute("href", link.href)
a.createChildNode(link.text, TextNode)
lines++
// skip the <li>
lines++
}
// skip the last ul
lines++
p := body.createChildNode("p", ElementNode)
p.lines = lines
lines++
p.createChildNode("This is the first paragraph.", TextNode)
lines++
comment := body.createChildNode("<!-- this is the end -->", CommentNode)
comment.lines = lines
lines++
return doc
}
func createMyBookExample() *TNode {
/*
<?xml version="1.0" encoding="utf-8"?>
<books xmlns:mybook="http://www.contoso.com/books">
<mybook:book id="bk101">
<title>XML Developer's Guide</title>
<author>Gambardella, Matthew</author>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</mybook:book>
<mybook:book id="bk102">
<title>Midnight Rain</title>
<author>Ralls, Kim</author>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
</mybook:book>
</books>
*/
var (
prefix string = "mybook"
namespaceURL string = "http://www.contoso.com/books"
)
lines := 1
doc := createNode("", RootNode)
doc.lines = lines
lines++
books := doc.createChildNode("books", ElementNode)
books.addAttribute("xmlns:mybook", namespaceURL)
books.lines = lines
lines++
data := []struct {
id string
title string
author string
price float64
publish string
}{
{id: "bk101", title: "XML Developer's Guide", author: "Gambardella, Matthew", price: 44.95, publish: "2000-10-01"},
{id: "bk102", title: "Midnight Rain", author: "Ralls, Kim", price: 5.95, publish: "2000-12-16"},
}
for i := 0; i < len(data); i++ {
v := data[i]
book := books.createChildNode("book", ElementNode)
book.addAttribute("id", v.id)
book.Prefix = prefix
book.NamespaceURL = namespaceURL
book.lines = lines
lines++
title := book.createChildNode("title", ElementNode)
title.createChildNode(v.title, TextNode)
title.lines = lines
lines++
author := book.createChildNode("author", ElementNode)
author.createChildNode(v.author, TextNode)
author.lines = lines
lines++
price := book.createChildNode("price", ElementNode)
price.createChildNode(fmt.Sprintf("%.2f", v.price), TextNode)
price.lines = lines
lines++
publish_date := book.createChildNode("publish_date", ElementNode)
publish_date.createChildNode(v.publish, TextNode)
publish_date.lines = lines
lines++
// skip the last of book element
lines++
}
return doc
}
|