Is it possible to generate plain-old XML using Haml? Is it possible to generate plain-old XML using Haml? xml xml

Is it possible to generate plain-old XML using Haml?


Doing XML in HAML is easy, just start off your template with:

!!! XML

which produces

<?xml version='1.0' encoding='utf-8' ?>

Then as @beanish said earlier, you "make up your own tags":

%test  %test2 hello  %item{:name => "blah"}

to get

<test>  <test2>hello</test2>  <item name='blah'></item></test>

More:http://haml.info/docs/yardoc/file.REFERENCE.html#doctype_


%test  %test2 hello  %item{:name => "blah"}

run it through haml

haml hamltest.haml test.xml

open the file in a browser

<test>  <test2>hello</test2>  <item name='blah'></item></test>

The HAML reference talks about html tags and gives some examples.HAML reference


This demonstrates some things that could use useful for xml documents:

!!! XML%root{'xmlns:foo' => 'http://myns'}  -# Note: :dashed-attr is invalid syntax  %dashed-tag{'dashed-attr' => 'value'} Text  %underscore_tag Text  - ['apple', 'orange', 'pear'].each do |fruit|    - haml_tag(fruit, "Yummy #{fruit.capitalize}!", 'fruit-code' => fruit.upcase)  %foo:nstag{'foo:nsattr' => 'value'}

Output:

<?xml version='1.0' encoding='utf-8' ?><root xmlns:foo='http://myns'>  <dashed-tag dashed-attr='value'>Text</dashed-tag>  <underscore_tag>Text</underscore_tag>  <apple fruit-code='APPLE'>Yummy Apple!</apple>  <orange fruit-code='ORANGE'>Yummy Orange!</orange>  <pear fruit-code='PEAR'>Yummy Pear!</pear>  <foo:nstag foo:nsattr='value'></foo:nstag></root>

Look at the Haml::Helpers link on the haml reference for more methods like haml_tag.

If you want to use double-quotes for attributes,

See: https://stackoverflow.com/a/967065/498594

Or outside of rails use:

>> Haml::Engine.new("%tag{:name => 'value'}", :attr_wrapper => '"').to_html=> "<tag name=\"value\"></tag>\n"