...

Source file src/github.com/clbanning/mxj/v2/x2j-wrapper/x2jpath_test.go

Documentation: github.com/clbanning/mxj/v2/x2j-wrapper

     1  package x2j
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  // the basic demo/test case - a small bibliography with mixed element types
     9  func TestValuesFromTagPath(t *testing.T) {
    10  var doc = `
    11  <doc>
    12  	<books>
    13  		<book seq="1">
    14  			<author>William H. Gaddis</author>
    15  			<title>The Recognitions</title>
    16  			<review>One of the great seminal American novels of the 20th century.</review>
    17  		</book>
    18  		<book seq="2">
    19  			<author>Austin Tappan Wright</author>
    20  			<title>Islandia</title>
    21  			<review>An example of earlier 20th century American utopian fiction.</review>
    22  		</book>
    23  		<book seq="3">
    24  			<author>John Hawkes</author>
    25  			<title>The Beetle Leg</title>
    26  			<review>A lyrical novel about the construction of Ft. Peck Dam in Montana.</review>
    27  		</book>
    28  		<book seq="4">
    29  			<author>
    30  				<first_name>T.E.</first_name>
    31  				<last_name>Porter</last_name>
    32  			</author>
    33  			<title>King's Day</title>
    34  			<review>A magical novella.</review>
    35  		</book>
    36  	</books>
    37  </doc>
    38  `
    39  var doc2 = `
    40  <doc>
    41  	<books>
    42  		<book seq="1">
    43  			<author>William H. Gaddis</author>
    44  			<title>The Recognitions</title>
    45  			<review>One of the great seminal American novels of the 20th century.</review>
    46  		</book>
    47  	</books>
    48  </doc>
    49  `
    50  	fmt.Println("\nTestValuesFromTagPath()\n",doc)
    51  
    52  	m,_ := DocToMap(doc)
    53  	fmt.Println("map:",WriteMap(m))
    54  
    55  	v,_ := ValuesFromTagPath(doc,"doc.books")
    56  	fmt.Println("path == doc.books: len(v):",len(v))
    57  	for key,val := range v {
    58  		fmt.Println(key,":",val)
    59  	}
    60  
    61  	v,_ = ValuesFromTagPath(doc,"doc.books.*")
    62  	fmt.Println("path == doc.books.*: len(v):",len(v))
    63  	for key,val := range v {
    64  		fmt.Println(key,":",val)
    65  	}
    66  
    67  	v,_ = ValuesFromTagPath(doc,"doc.books.book")
    68  	fmt.Println("path == doc.books.book: len(v):",len(v))
    69  	for key,val := range v {
    70  		fmt.Println(key,":",val)
    71  	}
    72  
    73  	v,_ = ValuesFromTagPath(doc2,"doc.books.book")
    74  	fmt.Println("doc == doc2 / path == doc.books.book: len(v):",len(v))
    75  	for key,val := range v {
    76  		fmt.Println(key,":",val)
    77  	}
    78  
    79  	v,_ = ValuesFromTagPath(doc,"doc.books.book.*")
    80  	fmt.Println("path == doc.books.book.*: len(v):",len(v))
    81  	for key,val := range v {
    82  		fmt.Println(key,":",val)
    83  	}
    84  
    85  	v,_ = ValuesFromTagPath(doc2,"doc.books.book.*")
    86  	fmt.Println("doc == doc2 / path == doc.books.book.*: len(v):",len(v))
    87  	for key,val := range v {
    88  		fmt.Println(key,":",val)
    89  	}
    90  
    91  	v,_ = ValuesFromTagPath(doc,"doc.books.*.author")
    92  	fmt.Println("path == doc.books.*.author: len(v):",len(v))
    93  	for key,val := range v {
    94  		fmt.Println(key,":",val)
    95  	}
    96  
    97  	v,_ = ValuesFromTagPath(doc,"doc.*.*.author")
    98  	fmt.Println("path == doc.*.*.author: len(v):",len(v))
    99  	for key,val := range v {
   100  		fmt.Println(key,":",val)
   101  	}
   102  
   103  	v,_ = ValuesFromTagPath(doc,"doc.*.*.title")
   104  	fmt.Println("path == doc.*.*.title: len(v):",len(v))
   105  	for key,val := range v {
   106  		fmt.Println(key,":",val)
   107  	}
   108  
   109  	v,_ = ValuesFromTagPath(doc,"doc.*.*.*")
   110  	fmt.Println("path == doc.*.*.*: len(v):",len(v))
   111  	for key,val := range v {
   112  		fmt.Println(key,":",val)
   113  	}
   114  
   115  	v,_ = ValuesFromTagPath(doc,"doc.*.*.*.*")
   116  	fmt.Println("path == doc.*.*.*.*: len(v):",len(v))
   117  	for key,val := range v {
   118  		fmt.Println(key,":",val)
   119  	}
   120  }
   121  
   122  // demo how to compensate for irregular tag labels in data
   123  // "netid" vs. "idnet"
   124  func TestValuesFromTagPath2(t *testing.T) {
   125  var doc1 = `
   126  <?xml version="1.0" encoding="UTF-8"?>
   127  <data>
   128      <netid>
   129          <disable>no</disable>
   130          <text1>default:text</text1>
   131          <word1>default:word</word1>
   132      </netid>
   133  </data>
   134  `
   135  var doc2 = `
   136  <?xml version="1.0" encoding="UTF-8"?>
   137  <data>
   138      <idnet>
   139          <disable>yes</disable>
   140          <text1>default:text</text1>
   141          <word1>default:word</word1>
   142      </idnet>
   143  </data>
   144  `
   145  	var docs = []string{doc1,doc2}
   146  
   147  	for n,doc := range docs {
   148  		fmt.Println("\nTestValuesFromTagPath2(), iteration:",n,"\n",doc)
   149  
   150  		m,_ := DocToMap(doc)
   151  		fmt.Println("map:",WriteMap(m))
   152  
   153  		v,_ := ValuesFromTagPath(doc,"data.*")
   154  		fmt.Println("\npath == data.*: len(v):",len(v))
   155  		for key,val := range v {
   156  			fmt.Println(key,":",val)
   157  		}
   158  		for key,val := range v[0].(map[string]interface{}) {
   159  			fmt.Println("\t",key,":",val)
   160  		}
   161  
   162  		v,_ = ValuesFromTagPath(doc,"data.*.*")
   163  		fmt.Println("\npath == data.*.*: len(v):",len(v))
   164  		for key,val := range v {
   165  			fmt.Println(key,":",val)
   166  		}
   167  	}
   168  }
   169  
   170  

View as plain text