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
|
package goxpath
import (
"bytes"
"encoding/xml"
"runtime/debug"
"testing"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
)
func execPath(xp, x string, exp []string, ns map[string]string, t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("Panicked: from XPath expr: '" + xp)
t.Error(r)
t.Error(string(debug.Stack()))
}
}()
res := MustParse(xp).MustExec(xmltree.MustParseXML(bytes.NewBufferString(x)), func(o *Opts) { o.NS = ns }).(tree.NodeSet)
if len(res) != len(exp) {
t.Error("Result length not valid in XPath expression '"+xp+"':", len(res), ", expecting", len(exp))
for i := range res {
t.Error(MarshalStr(res[i].(tree.Node)))
}
return
}
for i := range res {
r, err := MarshalStr(res[i].(tree.Node))
if err != nil {
t.Error(err.Error())
return
}
valid := false
for j := range exp {
if r == exp[j] {
valid = true
}
}
if !valid {
t.Error("Incorrect result in XPath expression '" + xp + "':" + r)
t.Error("Expecting one of:")
for j := range exp {
t.Error(exp[j])
}
return
}
}
}
func TestRoot(t *testing.T) {
p := `/`
x := `<?xml version="1.0" encoding="UTF-8"?><test><path/></test>`
exp := []string{"<test><path></path></test>"}
execPath(p, x, exp, nil, t)
}
func TestRoot2(t *testing.T) {
x := xmltree.MustParseXML(bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?><test><path/></test>`))
x = MustParse("/test/path").MustExec(x).(tree.NodeSet)[0]
for _, i := range []string{"/", "//test"} {
res, err := MarshalStr(MustParse(i).MustExec(x).(tree.NodeSet)[0])
if err != nil {
t.Error(err)
}
if res != "<test><path></path></test>" {
t.Error("Incorrect result", res)
}
}
}
func TestAbsPath(t *testing.T) {
p := ` / test / path`
x := `<test><path/></test>`
exp := []string{"<path></path>"}
execPath(p, x, exp, nil, t)
}
func TestNonAbsPath(t *testing.T) {
p := ` test `
x := `<?xml version="1.0" encoding="UTF-8"?><test><path/></test>`
exp := []string{"<test><path></path></test>"}
execPath(p, x, exp, nil, t)
}
func TestRelPath(t *testing.T) {
p := ` // path `
x := `<?xml version="1.0" encoding="UTF-8"?><test><path/></test>`
exp := []string{"<path></path>"}
execPath(p, x, exp, nil, t)
}
func TestRelPath2(t *testing.T) {
p := ` // path `
x := `<?xml version="1.0" encoding="UTF-8"?><path><test><path>test</path></test></path>`
exp := []string{"<path><test><path>test</path></test></path>", `<path>test</path>`}
execPath(p, x, exp, nil, t)
}
func TestRelNonRootPath(t *testing.T) {
p := ` / test // path `
x := `<?xml version="1.0" encoding="UTF-8"?><test><p1><p2><path/></p2></p1></test>`
exp := []string{"<path></path>"}
execPath(p, x, exp, nil, t)
}
func TestParent(t *testing.T) {
p := `/test/path/ parent:: test`
x := `<?xml version="1.0" encoding="UTF-8"?><test><path/></test>`
exp := []string{"<test><path></path></test>"}
execPath(p, x, exp, nil, t)
}
func TestAncestor(t *testing.T) {
p := `/p1/p2/p3/p1/ancestor::p1`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><p3><p1></p1></p3></p2></p1>`
exp := []string{`<p1><p2><p3><p1></p1></p3></p2></p1>`}
execPath(p, x, exp, nil, t)
}
func TestAncestorOrSelf(t *testing.T) {
p := `/p1/p2/p3/p1/ancestor-or-self::p1`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><p3><p1></p1></p3></p2></p1>`
exp := []string{`<p1></p1>`, `<p1><p2><p3><p1></p1></p3></p2></p1>`}
execPath(p, x, exp, nil, t)
}
func TestDescendant(t *testing.T) {
p := `/p1/descendant::p1`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><p1/></p2></p1>`
exp := []string{`<p1></p1>`}
execPath(p, x, exp, nil, t)
}
func TestDescendantOrSelf(t *testing.T) {
p := `/p1/descendant-or-self::p1`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><p1/></p2></p1>`
exp := []string{`<p1><p2><p1></p1></p2></p1>`, `<p1></p1>`}
execPath(p, x, exp, nil, t)
}
func TestAttribute(t *testing.T) {
p := `/p1/attribute::test`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"></p1>`
exp := []string{`<?attribute test="foo"?>`}
execPath(p, x, exp, nil, t)
}
func TestAttributeSelf(t *testing.T) {
p := `/p1/attribute::test/self::node()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"></p1>`
exp := []string{`<?attribute test="foo"?>`}
execPath(p, x, exp, nil, t)
}
func TestAttributeAbbr(t *testing.T) {
p := `/p1/ @ test`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"></p1>`
exp := []string{`<?attribute test="foo"?>`}
execPath(p, x, exp, nil, t)
}
func TestAttributeNoChild(t *testing.T) {
p := `/p1/attribute::test/p2`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"><p2/></p1>`
exp := []string{}
execPath(p, x, exp, nil, t)
}
func TestAttributeParent(t *testing.T) {
p := `/p1/attribute::test/ ..`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"><p2/></p1>`
exp := []string{`<p1 test="foo"><p2></p2></p1>`}
execPath(p, x, exp, nil, t)
}
func TestNodeTypeNode(t *testing.T) {
p := `/p1/child:: node( ) `
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"><p2/></p1>`
exp := []string{`<p2></p2>`}
execPath(p, x, exp, nil, t)
}
func TestNodeTypeNodeAbbr(t *testing.T) {
p := `/p1/ .`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"><p2/></p1>`
exp := []string{`<p1 test="foo"><p2></p2></p1>`}
execPath(p, x, exp, nil, t)
}
func TestNodeTypeParent(t *testing.T) {
p := `/p1/p2/parent::node()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"><p2/></p1>`
exp := []string{`<p1 test="foo"><p2></p2></p1>`}
execPath(p, x, exp, nil, t)
}
func TestNodeTypeParentAbbr(t *testing.T) {
p := `/p1/p2/..`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 test="foo"><p2/></p1>`
exp := []string{`<p1 test="foo"><p2></p2></p1>`}
execPath(p, x, exp, nil, t)
}
func TestFollowing(t *testing.T) {
p := `//p3/following::node()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><p31/><p3/><p4/></p2><p5><p6/></p5></p1>`
exp := []string{`<p4></p4>`, `<p5><p6></p6></p5>`, `<p6></p6>`}
execPath(p, x, exp, nil, t)
}
func TestPreceding(t *testing.T) {
p := `//p6/preceding::node()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><p3/><p4/></p2><p5><p6/><p7/></p5></p1>`
exp := []string{`<p2><p3></p3><p4></p4></p2>`, `<p3></p3>`, `<p4></p4>`}
execPath(p, x, exp, nil, t)
}
func TestPrecedingSibling(t *testing.T) {
p := `//p4/preceding-sibling::node()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><p3><p31/></p3><p4/><p51/></p2><p5><p6/></p5></p1>`
exp := []string{`<p3><p31></p31></p3>`}
execPath(p, x, exp, nil, t)
}
func TestPrecedingSibling2(t *testing.T) {
p := `/preceding-sibling::node()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1></p1>`
exp := []string{}
execPath(p, x, exp, nil, t)
}
func TestFollowingSibling(t *testing.T) {
p := `//p2/following-sibling::node()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p21/><p2><p3/><p4/></p2><p5><p6/></p5></p1>`
exp := []string{`<p5><p6></p6></p5>`}
execPath(p, x, exp, nil, t)
}
func TestFollowingSibling2(t *testing.T) {
p := `/following-sibling::node()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1></p1>`
exp := []string{}
execPath(p, x, exp, nil, t)
}
func TestComment(t *testing.T) {
p := `// comment()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><!-- comment --></p1>`
exp := []string{`<!-- comment -->`}
execPath(p, x, exp, nil, t)
}
func TestCommentInvPath(t *testing.T) {
p := `//comment()/ self :: processing-instruction ( ) `
x := `<?xml version="1.0" encoding="UTF-8"?><p1><!-- comment --></p1>`
exp := []string{}
execPath(p, x, exp, nil, t)
}
func TestCommentParent(t *testing.T) {
p := `//comment()/..`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><!-- comment --></p1>`
exp := []string{`<p1><!-- comment --></p1>`}
execPath(p, x, exp, nil, t)
}
func TestText(t *testing.T) {
p := `//text()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1>text</p1>`
exp := []string{`text`}
execPath(p, x, exp, nil, t)
}
func TestTextParent(t *testing.T) {
p := `//text()/..`
x := `<?xml version="1.0" encoding="UTF-8"?><p1>text</p1>`
exp := []string{`<p1>text</p1>`}
execPath(p, x, exp, nil, t)
}
func TestProcInst(t *testing.T) {
p := `//processing-instruction()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><?proc?></p1>`
exp := []string{`<?proc?>`}
execPath(p, x, exp, nil, t)
}
func TestProcInstParent(t *testing.T) {
p := `//processing-instruction()/..`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><?proc?></p1>`
exp := []string{`<p1><?proc?></p1>`}
execPath(p, x, exp, nil, t)
}
func TestProcInstInvPath(t *testing.T) {
p := `//processing-instruction()/self::comment()`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><?proc?></p1>`
exp := []string{}
execPath(p, x, exp, nil, t)
}
func TestProcInst2(t *testing.T) {
p := `//processing-instruction( 'proc2' ) `
x := `<?xml version="1.0" encoding="UTF-8"?><p1><?proc1?><?proc2?><?proc3?></p1>`
exp := []string{`<?proc2?>`}
execPath(p, x, exp, nil, t)
}
func TestNamespace(t *testing.T) {
p := `/*:p1/*:p2/namespace::*`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://foo.bar"><p2 xmlns:foo="http://test"></p2></p1>`
exp := []string{`<?namespace http://foo.bar?>`, `<?namespace http://test?>`, `<?namespace http://www.w3.org/XML/1998/namespace?>`}
execPath(p, x, exp, nil, t)
}
func TestRootNamespace(t *testing.T) {
p := `/namespace::*`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://foo.bar"><p2 xmlns:foo="http://test"></p2></p1>`
exp := []string{}
execPath(p, x, exp, nil, t)
}
func TestNamespaceXml(t *testing.T) {
p := `/*:p1/ * : p2 /namespace::xml`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://foo.bar"><p2 xmlns:foo="http://test"></p2></p1>`
exp := []string{`<?namespace http://www.w3.org/XML/1998/namespace?>`}
execPath(p, x, exp, nil, t)
}
func TestNamespaceNodeType(t *testing.T) {
p := `/*:p1/*:p2/namespace::foo/ self :: node ( ) `
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://foo.bar"><p2 xmlns:foo="http://test"></p2></p1>`
exp := []string{`<?namespace http://test?>`}
execPath(p, x, exp, nil, t)
}
func TestNamespaceNodeWildcard(t *testing.T) {
p := `/*:p1/*:p2/namespace::*:foo`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://foo.bar"><p2 xmlns:foo="http://test"></p2></p1>`
exp := []string{`<?namespace http://test?>`}
execPath(p, x, exp, nil, t)
p = `/*:p1/*:p2/namespace::invalid:foo`
exp = []string{}
execPath(p, x, exp, nil, t)
}
func TestNamespaceChild(t *testing.T) {
p := `/*:p1/namespace::*/ * : p2`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://foo.bar"><p2 xmlns:foo="http://test"></p2></p1>`
exp := []string{}
execPath(p, x, exp, nil, t)
}
func TestNamespaceParent(t *testing.T) {
p := `/*:p1/*:p2/namespace::foo/..`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://foo.bar"><p2 xmlns:foo="http://test"></p2></p1>`
exp := []string{`<p2 xmlns="http://foo.bar"></p2>`}
execPath(p, x, exp, nil, t)
}
func TestNamespace2(t *testing.T) {
p := `//namespace::test`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns:foo="http://test"><p2 xmlns:test="http://foo.bar"></p2></p1>`
exp := []string{`<?namespace http://foo.bar?>`}
execPath(p, x, exp, nil, t)
}
func TestNamespace3(t *testing.T) {
p := `/p1/p2/p3/namespace :: foo`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns:foo="http://test"><p2 xmlns:foo="http://foo.bar"><p3/></p2></p1>`
exp := []string{`<?namespace http://foo.bar?>`}
execPath(p, x, exp, nil, t)
}
func TestNamespace4(t *testing.T) {
p := `/test:p1/p2/p3/namespace::*`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test"><p2 xmlns=""><p3/></p2></p1>`
exp := []string{`<?namespace http://www.w3.org/XML/1998/namespace?>`}
execPath(p, x, exp, map[string]string{"test": "http://test"}, t)
}
func TestNodeNamespace(t *testing.T) {
p := `/ test : p1 `
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test"/>`
exp := []string{`<p1 xmlns="http://test"></p1>`}
execPath(p, x, exp, map[string]string{"test": "http://test"}, t)
}
func TestNodeNamespace2(t *testing.T) {
p := `/test:p1/test2:p2`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test"><p2 xmlns="http://test2"></p2></p1>`
exp := []string{`<p2 xmlns="http://test2"></p2>`}
execPath(p, x, exp, map[string]string{"test": "http://test", "test2": "http://test2"}, t)
}
func TestNodeNamespace3(t *testing.T) {
p := `/test:p1/foo:p2`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test" xmlns:foo="http://foo.bar"><foo:p2></foo:p2></p1>`
exp := []string{`<p2 xmlns="http://foo.bar"></p2>`}
execPath(p, x, exp, map[string]string{"test": "http://test", "foo": "http://foo.bar"}, t)
}
func TestAttrNamespace(t *testing.T) {
p := `/test:p1/@foo:test`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test" xmlns:foo="http://foo.bar" foo:test="foo"></p1>`
exp := []string{`<?attribute test="foo" xmlns="http://foo.bar"?>`}
execPath(p, x, exp, map[string]string{"test": "http://test", "foo": "http://foo.bar"}, t)
p = `/test:p1/@test:test`
exp = []string{}
execPath(p, x, exp, map[string]string{"test": "http://test", "foo": "http://foo.bar"}, t)
}
func TestWildcard(t *testing.T) {
p := `/p1/*`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns:test="http://test" xmlns:foo="http://foo.bar"><p1/><test:p2/><foo:p3/></p1>`
exp := []string{`<p1></p1>`, `<p2 xmlns="http://test"></p2>`, `<p3 xmlns="http://foo.bar"></p3>`}
execPath(p, x, exp, nil, t)
}
func TestWildcardNS(t *testing.T) {
p := `//*:p1`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test" xmlns:foo="http://foo.bar"><foo:p1/></p1>`
exp := []string{`<p1 xmlns="http://test"><p1 xmlns="http://foo.bar"></p1></p1>`, `<p1 xmlns="http://foo.bar"></p1>`}
execPath(p, x, exp, map[string]string{"test": "http://test", "foo": "http://foo.bar"}, t)
}
func TestWildcardLocal(t *testing.T) {
p := `//foo:*`
x := `<?xml version="1.0" encoding="UTF-8"?><p3 xmlns="http://test" xmlns:foo="http://foo.bar"><foo:p1/><foo:p2/></p3>`
exp := []string{`<p1 xmlns="http://foo.bar"></p1>`, `<p2 xmlns="http://foo.bar"></p2>`}
execPath(p, x, exp, map[string]string{"test": "http://test", "foo": "http://foo.bar"}, t)
}
func TestWildcardNSAttr(t *testing.T) {
p := `/p1/@*:attr`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns:foo="http://foo.bar" foo:attr="test"/>`
exp := []string{`<?attribute attr="test" xmlns="http://foo.bar"?>`}
execPath(p, x, exp, map[string]string{"test": "http://test", "foo": "http://foo.bar"}, t)
}
func TestWildcardLocalAttr(t *testing.T) {
p := `/p1/@foo:*`
x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns:foo="http://foo.bar" foo:attr="test" foo:bar="foobar"/>`
exp := []string{`<?attribute attr="test" xmlns="http://foo.bar"?>`, `<?attribute bar="foobar" xmlns="http://foo.bar"?>`}
execPath(p, x, exp, map[string]string{"test": "http://test", "foo": "http://foo.bar"}, t)
}
func TestPredicate1(t *testing.T) {
p := `/p1/p2 [ p3 ] `
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p2><p3/></p2></p1>`
exp := []string{`<p2><p3></p3></p2>`}
execPath(p, x, exp, nil, t)
}
func TestPredicate2(t *testing.T) {
p := `/p1/p2 [ 2 ] `
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p2><p3/></p2><p2><p4/></p2></p1>`
exp := []string{`<p2><p3></p3></p2>`}
execPath(p, x, exp, nil, t)
p = `/p1/p2 [ position() = 2 ] `
execPath(p, x, exp, nil, t)
}
func TestPredicate3(t *testing.T) {
p := `/p1/p2 [ last() ] /p3`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2><p3/></p2><p2><p3/></p2><p2><p3 foo="bar"/></p2></p1>`
exp := []string{`<p3 foo="bar"></p3>`}
execPath(p, x, exp, nil, t)
}
func TestPredicate4(t *testing.T) {
p := `/p1/p2[not(@test)]`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p2 test="foo"/><p2/></p1>`
exp := []string{`<p2></p2>`, `<p2></p2>`}
execPath(p, x, exp, nil, t)
}
func TestPredicate5(t *testing.T) {
p := `/p1/p2[.//p1]`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p2 test="t"/><p2><p1>lkj</p1></p2></p1>`
exp := []string{`<p2><p1>lkj</p1></p2>`}
execPath(p, x, exp, nil, t)
}
func TestPredicate6(t *testing.T) {
p := `/p1/p2[//p1]`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p2 test="t"/><p2><p1>lkj</p1></p2></p1>`
exp := []string{`<p2></p2>`, `<p2 test="t"></p2>`, `<p2><p1>lkj</p1></p2>`}
execPath(p, x, exp, nil, t)
}
func TestPredicate7(t *testing.T) {
p := `/p3[//p1]`
x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/><p2 test="t"/><p2><p1>lkj</p1></p2></p1>`
exp := []string{}
execPath(p, x, exp, nil, t)
}
func TestPredicate8(t *testing.T) {
p := `/test[@attr1='a' and @attr2='b']/a`
x := `<?xml version="1.0" encoding="UTF-8"?><test attr1="a" attr2="b"><a>aaaa</a><b>bbb</b></test>`
exp := []string{`<a>aaaa</a>`}
execPath(p, x, exp, nil, t)
}
func TestPredicate9(t *testing.T) {
p := `/root/text[@attr="2"][2]`
x := xml.Header + `<root>
<text attr="1">This is some text 1.1.</text>
<text attr="1">This is some text 1.2.</text>
<text attr="2">This is some text. 2.1</text>
<text attr="2">This is some text. 2.2</text>
<text attr="2">This is some text. 2.3</text>
</root>`
exp := []string{`<text attr="2">This is some text. 2.2</text>`}
execPath(p, x, exp, nil, t)
}
func TestPredicate10(t *testing.T) {
p := `/root/text[@attr="2"][last()]`
x := xml.Header + `<root>
<text attr="1">This is some text 1.1.</text>
<text attr="1">This is some text 1.2.</text>
<text attr="2">This is some text. 2.1</text>
<text attr="2">This is some text. 2.2</text>
<text attr="2">This is some text. 2.3</text>
</root>`
exp := []string{`<text attr="2">This is some text. 2.3</text>`}
execPath(p, x, exp, nil, t)
}
func TestUnion(t *testing.T) {
p := `/test/test2 | /test/test3`
x := `<?xml version="1.0" encoding="UTF-8"?><test><test2>foobar</test2><test3>hamneggs</test3></test>`
exp := []string{"<test2>foobar</test2>", "<test3>hamneggs</test3>"}
execPath(p, x, exp, nil, t)
}
|