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
Public Instance methods
attlist!( *args )

ATTLIST DTD declaration.

# File lib/facets/more/xmlhelper.rb, line 115
  def attlist!( *args )
    '<!ATTLIST'
  end
cdata( data )

CData.

# File lib/facets/more/xmlhelper.rb, line 96
  def cdata( data )
    raise ArgumentError, "CDATA contains ']]>'" if data.index(']]>')
    "<![CDATA[#{data}]]>"
  end
comment( remark )

Comment.

# File lib/facets/more/xmlhelper.rb, line 73
  def comment( remark )
    "<!-- #{remark} -->"
  end
doctype!( *args )

DOCTYPE DTD declaration.

# File lib/facets/more/xmlhelper.rb, line 103
  def doctype!( *args )
    "<!DOCTYPE"
  end
document( options )

Empty XML document.

This method is also aliased as new
# 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( tag, body=nil, atts={} )

Element.

# 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!( *args )

ELEMENT DTD declaration.

# File lib/facets/more/xmlhelper.rb, line 109
  def element!( *args )
    '<!ELEMENT'
  end
entity!( *args )

ENTITY DTD declaration.

# File lib/facets/more/xmlhelper.rb, line 121
  def entity!( *args )
    '<!ENTITY'
  end
instruct( name, *args )

Proccessing instruction.

This method is also aliased as instruction pi
# 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
instruction( name, *args )

Alias for instruct

new( options )

Alias for document

notation!( *args )

NOTATION DTD declaration.

# File lib/facets/more/xmlhelper.rb, line 127
  def notation!( *args )
    '<!NOTATION'
  end
pi( name, *args )

Alias for instruct

text( str )

Text.

# File lib/facets/more/xmlhelper.rb, line 90
  def text( str )
    str
  end
xml( version=nil, encoding=nil )

XML declaration.

# 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