浏览 8436 次
锁定老帖子 主题:使用XPath计算节点的数量
精华帖 (0) :: 良好帖 (0) :: 新手帖 (5) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-23
最后修改:2009-12-29
本例主要涉及三点: 3、使用sum
首先,我们来看下使用XPath计算XML中各个节点的数量。
include REXML def index xml = <<XML <root> <a> <name>ayz</name> <b> <c>g234</c> </b> </a> <a> <name>aerd</name> <b> <c>g554</c> <c>g564</c> <c>g664</c> </b> <b> <c>g5544</c> </b> </a> <a> <name>cyz</name> <b> <c>g734</c> </b> </a> </root> XML doc = Document.new(xml) #输出a节点的数量 a_num = XPath.first(doc.root, 'count(//a)') #输出b节点的数量 b_num = XPath.first(doc.root, 'count(//b)') #输出c节点的数量 c_num = XPath.first(doc.root, 'count(//c)') #输出d节点的数量 d_num = XPath.first(doc.root, 'count(//d)') puts a_num # => 3 puts b_num # => 4 puts c_num # => 6 puts d_num # => 0 end
接着,来看下使用XPath输出符合特定条件的XML。
#输出所有a节点下b节点的数量小于2的b节点及其子节点 b_node = XPath.match(doc.root, '//a[count(b) < 2]/b') #输出所有b节点下c节点的数量小于3的c节点 c_node = XPath.match(doc.root, '//a[count(b//c) < 3]/b/c') puts b_node # => <b> # <c>g234</c> # </b> # <b> # <c>g734</c> # </b> puts c_node # => <c>g234</c> # <c>g734</c>
xml = <<XML <root> <item>1</item> <item>2</item> <item>3</item> <item>4</item> <item>5</item> <item>6</item> <item>7</item> </root> XML doc = Document.new(xml) #输出位置为3和4的节点 node = XPath.match(doc.root,'item[position() > 2 and position() < 5]') puts node # => <item>3</item> # <item>4</item>
xml = <<XML <root> <summary><task>clean bathroom</task></summary> <records> <x> <summary><task>clean bath</task></summary> <records> <x><summary><task>rinse surfaces with water</task></summary><records/></x> <x><summary><task>apply pine fresh flash cleaner</task></summary><records/></x> </records> </x> <x> <summary><task>clean sink</task></summary> <records> <x><summary><task>remove items around the sink</task></summary><records/></x> <x><summary><task>using a sponge apply warm water to the sink surfaces</task></summary><records/></x> </records> </x> <x><summary><task>mop the floor</task></summary>records/></x> </records> </root> XML document = Document.new(xml) titles = [] document.root.each_recursive do |elem| #输出所有task节点的内容 titles << elem.text.to_s if elem.name == "task" end puts titles #clean bathroom #clean bath #rinse surfaces with water #apply pine fresh flash cleaner #clean sink #remove items around the sink #using a sponge apply warm water to the sink surfaces #mop the floor
xml = <<XML <list> <link>www.linux.com</link> <link>http://www.rubyforge.org</link> <link>www.learningruby.com</link> <link>http://snippets.dzone.com</link> </list> XML Document.new(xml).root.elements.each("link[starts-with(., 'http')]") do |node| puts node.text # => http://www.rubyforge.org # http://snippets.dzone.com end
doc = Document.new("<a1><b1>3</b1><b1>5</b1><b1>1</b1><b1>7</b1><b1>9</b1></a1>") a = doc.root.elements.to_a.sort {|x, y| x.text.to_s <=> y.text.to_s} a.each {|x| puts x} # => <b1>1</b1> # <b1>3</b1> # <b1>5</b1> # <b1>7</b1> # <b1>9</b1>
xml = <<XML <task> <summary><tags>reth, etrert, rtj64f, sitto</tags></summary> <records> <task> <summary><tags>retert, etert, rtu4f, sitto</tags></summary> <records> <task> <summary> <name>abcdef</name> <tags>jjy, rf57, lgk78</tags> </summary> <records> </records> </task> </records> </task> </records> </task> XML doc = Document.new(xml) doc.root.elements.each('//tags') {|element| element.parent.delete(element)} puts doc #<task> # <summary/> # <records> # <task> # <summary/> # <records> # <task> # <summary> # <name>abcdef</name> # </summary> # <records> # </records> # </task> # </records> # </task> # </records> #</task>
xml = <<XML <entries> <entry id='93391' catid='0701'></entry> <entry id='93394' catid='0701'></entry> <entry id='93397' catid='0304'></entry> <entry id='93400' catid='0506'></entry> <entry id='94396' catid='0701'></entry> <entry id='95398' catid='0701'></entry> <entry id='95610' catid='0506'></entry> </entries> XML document = Document.new(xml) puts document.root.elements['entry[last()]'] # => <entry id='95610' catid='0506'/> p document.root.elements.each('entry/@catid').collect {|d| d.value}.uniq # => ["0701", "0304", "0506"]
xml = "<a><records><bun id='1'/><bun id='2'/><bun id='3'/><bun id='4'/></records></a>" doc = Document.new(xml) node_cake = Element.new("cake") node_bun = doc.root.elements['records/*[1]'] node_bun.parent.insert_before(node_bun, node_cake) puts doc #<a><records><cake/><bun id='1'/><bun id='2'/><bun id='3'/><bun id='4'/></records></a>
doc = Document.new("<a><b>format's<c>just fine</c></b><b3><c/></b3></a>") c = doc.root.elements.each('//c'){} puts c #<c>just fine</c> #<c/>
最后,我们来看下关于sum的例子。
items = <<ITEMS <items> <item>2</item> <item>4</item> </items> ITEMS puts Document.new(items).root.elements['sum(item)'] # => 6.0
items = <<ITEMS <items> <item1>2</item1><item2>10</item2> <item1>4</item1><item2>20</item2> </items> ITEMS puts Document.new(items).root.elements['sum(item1 + item2)'] # => 12.0 puts Document.new(items).root.elements['sum(item1[1] + item2[1])'] # => 12.0 puts Document.new(items).root.elements['sum(item1[2] + item2[2])'] # => 24.0 puts Document.new(items).root.elements['sum(sum(item1) + sum(item2))'] # => 36.0
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-02-02
很实用啊。。
|
|
返回顶楼 | |