XMLHelper
XMLHelper is a utility module which provides methods the ease the creation of XML markup.
Usage
x = XMLHelper puts x.element( :a, x.element( :b, "X" ) )
produces
<a><b>X</b></a>
Methods
- attlist!
- cdata
- comment
- doctype!
- document
- element
- element!
- entity!
- instruct
- instruction
- new
- notation!
- pi
- text
- xml
Public Instance methods
ATTLIST DTD declaration.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 115 def attlist!( *args ) '<!ATTLIST' end
CData.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 96 def cdata( data ) raise ArgumentError, "CDATA contains ']]>'" if data.index(']]>') "<![CDATA[#{data}]]>" end
Comment.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 73 def comment( remark ) "<!-- #{remark} -->" end
DOCTYPE DTD declaration.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 103 def doctype!( *args ) "<!DOCTYPE" end
Empty XML document.
This method is also aliased as
new
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 43 def document( options )x doc = '' if options[:version] doc << xml( options[:version], options[:encoding] ) end end
Element.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 62 def element( tag, body=nil, atts={} ) atts = atts.collect{ |k,v| %{ #{k}="#{v}"} }.join('') if body "<#{tag}#{atts}>#{body}</#{tag}>" else "<#{tag}#{atts} />" end end
ELEMENT DTD declaration.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 109 def element!( *args ) '<!ELEMENT' end
ENTITY DTD declaration.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 121 def entity!( *args ) '<!ENTITY' end
Proccessing instruction.
This method is also aliased as
instruction
pi
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 79 def instruct( name, *args ) atts = Hash === args.last ? args.pop : {} atts = atts.collect{ |k,v| %{ #{k}="#{v}"} }.join('') body = ' ' + args.join(' ') "<?#{tag}#{body}#{atts}?>" end
Alias for instruct
Alias for document
NOTATION DTD declaration.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 127 def notation!( *args ) '<!NOTATION' end
Alias for instruct
Text.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 90 def text( str ) str end
XML declaration.
[ show source ]
# File lib/facets/more/xmlhelper.rb, line 53 def xml( version=nil, encoding=nil ) atts = {} atts[:version] = version || '1.1' atts[:version] = encoding if encoding instruct( :xml, atts ) end