From: "Andrew S. Townley" Date: 2009-03-27T21:22:43+09:00 Subject: Re: [ANN] tagz-5.0.0 Apologies for the typo earlier. I think I have it mostly working now, but it would need a couple of changes in Tagz to do what I want with the ns declarations and to fix the empty element handling. Here's the examples: cat /tmp/a.rb require 'rubygems' require 'tagz' class Namespace def initialize hash, &block if hash.is_a? Hash @prefix, @uri = hash.inject([]) { |a, (k, v)| a << k << v } else @prefix = nil @uri = hash end @context = block @self = eval 'super()', block end instance_methods.each{|m| undef_method if m[%r/__/]} def emit_ns if !@prefix.nil? return { "xmlns:#{ @prefix }".to_sym => @uri } else return { :xmlns => @uri } end end def method_missing m, *a, &b if m.to_s[%r/^_|_$/] && !@prefix.nil? @self.send("#{ @prefix }:#{ m }", *a, &b) else if @prefix.nil? "#{ super }" else "#{ @prefix }:#{ m }" end end end end include Tagz.globally xhtml = Namespace.new(:xhtml => "http://www.w3.org/1999/xhtml") xf = Namespace.new(:xf => "http://www.w3.org/2002/xforms") default = Namespace.new("http://www.w3.org/1999/xhtml") puts xhtml.emit_ns.inspect puts xf.emit_ns.inspect puts default.emit_ns.inspect xml = default.html_(default.emit_ns) { a_ "link", :href => "uri" } puts "\nDefault namespace example" puts xml puts # BUG!! puts empty_element_ xml = xhtml.html_{ xhtml.foo_(xhtml.bar => 'bar') { 'content' } xf.bar_(xf.foo => 'foo') { 'content' } } puts puts xml nsdecls = xhtml.emit_ns.merge xf.emit_ns puts nsdecls.inspect a = Namespace.new(:a => "uri_a") simple = doc = # want to do this: # xhtml.html_(xhtml.emit_ns, xf.emit_ns) { xhtml.html_(nsdecls) { xhtml.head_{ xhtml.title_ "Important Document" xf.model_{ xf.instance_{ person_{ fname_ lname_ } } } xf.submission(:id => "form1", :method => "get", :action => "doit.jsp") } xhtml.body_{ "..." } } puts "\nXForms example" puts doc excalibur$ ruby /tmp/a.rb {:"xmlns:xhtml"=>"http://www.w3.org/1999/xhtml"} {:"xmlns:xf"=>"http://www.w3.org/2002/xforms"} {:xmlns=>"http://www.w3.org/1999/xhtml"} Default namespace example link contentcontent {:"xmlns:xhtml"=>"http://www.w3.org/1999/xhtml", :"xmlns:xf"=>"http://www.w3.org/2002/xforms"} XForms example Important Document... -- Andrew S. Townley http://atownley.org