File: simple.coffee

package info (click to toggle)
node-xml2js 0.4.23%2B~cs15.4.0%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,380 kB
  • sloc: xml: 117; javascript: 7; makefile: 2
file content (61 lines) | stat: -rw-r--r-- 2,138 bytes parent folder | download | duplicates (2)
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
suite 'XPath:', ->
  test 'Evaluate', ->
    doc = xml('book').ele('title').txt('Harry Potter').doc()
    nodes = xpath.evaluate('//title', doc, null, xpath.XPathResult.ANY_TYPE, null).nodes

    eq(nodes[0].localName, "title")
    eq(nodes[0].firstChild.data, "Harry Potter")
    eq(nodes[0].toString(), "<title>Harry Potter</title>")

  test 'Select', ->
    doc = builder.begin()
      .ins('book', 'title="Harry Potter"')
      .ins('series', 'title="Harry Potter"')
      .ins('series', 'books="7"')
      .ele('root')
      .com('This is a great book')
      .ele('title')
      .txt('Harry Potter')
      .document()

    nodes = xpath.select('//title', doc)
    nodes2 = xpath.select('//node()', doc)
    pis = xpath.select("/processing-instruction('series')", doc)

    eq(nodes[0].localName, 'title')
    eq(nodes[0].firstChild.data, 'Harry Potter')
    eq(nodes[0].toString(), '<title>Harry Potter</title>')
    
    eq(nodes2.length, 7)
    
    eq(pis.length, 2)
    eq(pis[1].data, 'books="7"')

  test 'Select single node', ->
    doc = xml('book').ele('title').txt('Harry Potter').doc()
    eq(xpath.select('//title[1]', doc)[0].localName, 'title')

  test 'Select text node', ->
    doc = xml('book').ele('title').txt('Harry').up().ele('title').txt('Potter').doc()
    eq(xpath.select('local-name(/book)', doc), 'book')
    eq(xpath.select('//title/text()', doc).toString(), 'Harry,Potter')

  test 'Select number value', ->
    doc = xml('book').ele('title').txt('Harry').up().ele('title').txt('Potter').doc()
    eq(xpath.select('count(//title)', doc), 2)

  test 'Select attribute', -> 
    doc = xml('author').att('name', 'J. K. Rowling').doc()
    author = xpath.select1('/author/@name', doc).value
    eq(author, 'J. K. Rowling');

  test 'Select with multiple predicates', ->
    doc = xml('characters')
      .ele('character', { name: "Snape", sex: "M", age: "50" }).up()
      .ele('character', { name: "McGonnagal", sex: "F", age: "65" }).up()
      .doc()

    characters = xpath.select('/*/character[@sex = "M"][@age > 40]/@name', doc)

    eq(characters.length, 1)
    eq(characters[0].textContent, 'Snape')