?? 07 - substituting xml entities.rb
字號:
require 'rexml/document'str = %{<?xml version="1.0"?><!DOCTYPE doc [ <!ENTITY product 'Stargaze'> <!ENTITY version '2.3'>]><doc> &product; v&version; is the most advanced astronomy product on the market.</doc>}doc = REXML::Document.new strdoc.root.children[0].valuedoc.root.text# => "\n Stargaze v2.3 is the most advanced astronomy product on the market.\n"doc.root.children[0].to_s# => "\n &product; v&version; is the most advanced astronomy product on the market.\n"doc.root.write# <doc># &product; v&version; is the most advanced astronomy program on the market.# </doc>#---require 'delegate'require 'rexml/text'class EntitySubstituter < DelegateClass(IO) def initialize(io, document, filter=nil) @document = document @filter = filter super(io) end def <<(s) super(REXML::Text::unnormalize(s, @document.doctype, @filter)) endendoutput = EntitySubstituter.new($stdout, doc)doc.write(output)# <?xml version='1.0'?><!DOCTYPE doc [# <!ENTITY product "Stargaze"># <!ENTITY version "2.3"># ]># <doc># Stargaze v2.3 is the most advanced astronomy product on the market.# </doc>#---text_node = doc.root.children[0]text_node.value = "&product; v&version; has a catalogue of 2.3 " + "million celestial objects."doc.write# <?xml version='1.0'?><!DOCTYPE doc [# <!ENTITY product "Stargaze"># <!ENTITY version "2.3"># <doc>&product; v&version; has a catalogue of &version; million celestial objects.</doc>#---text_node.raw = truedoc.write# <?xml version='1.0'?><!DOCTYPE doc [# <!ENTITY product "Stargaze"># <!ENTITY version "2.3"># ]># <doc>&product; v&version; has a catalogue of 2.3 million celestial objects.</doc>text_node.value# => "Stargaze v2.3 has a catalogue of 2.3 million celestial objects."text_node.to_s# => "&product; v&version; has a catalogue of 2.3 million celestial objects."#---str = %{ <!DOCTYPE doc [ <!ENTITY year '2006'> ]> <doc>© &year; Komodo Dragon & Bob Productions</doc>}doc = REXML::Document.new strtext_node = doc.root.children[0]text_node.value# => "© 2006 Komodo Dragon & Bob Productions"text_node.to_s# => "© &year; Komodo Dragon & Bob Productions"#---
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -