是否可以使用Haml生成普通的XML?

时间:2023-01-14 11:04:06

I've been working on a piece of software where I need to generate a custom XML file to send back to a client application. The current solutions on Ruby/Rails world for generating XML files are slow, at best. Using builder or event Nokogiri, while have a nice syntax and are maintainable solutions, they consume too much time and processing.

我一直在研究一个软件,我需要生成一个自定义XML文件以发送回客户端应用程序。 Ruby / Rails世界上用于生成XML文件的当前解决方案充其量是缓慢的。使用构建器或事件Nokogiri,虽然具有良好的语法和可维护的解决方案,但它们消耗了太多时间和处理。

I definetly could go to ERB, which provides a good speed at the expense of building the whole XML by hand.

我绝对可以去ERB,它以牺牲手工构建整个XML为代价提供了很好的速度。

HAML is a great tool, have a nice and straight-forward syntax and is fairly fast. But I'm struggling to build pure XML files using it. Which makes me wonder, is it possible at all?

HAML是一个很棒的工具,具有良好而直接的语法并且相当快。但我正在努力使用它来构建纯XML文件。这让我想知道,它有可能吗?

Does any one have some pointers to some code or docs showing how to do this, build a full, valid XML from HAML?

有没有人对某些代码或文档有一些指示,说明如何执行此操作,从HAML构建完整,有效的XML?

8 个解决方案

#1


36  

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

在HAML中执行XML非常简单,只需使用以下命令启动模板即可:

!!! XML

which produces

哪个产生

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

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

然后正如@beanish先前所说,你“编造自己的标签”:

%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_

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

#2


8  

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

run it through haml

通过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

HAML参考讨论了html标签并给出了一些例子。 HAML参考

#3


3  

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

这演示了一些可能对xml文档有用的东西:

!!! 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.

查看haml引用上的Haml :: Helpers链接,了解更多方法,如haml_tag。

If you want to use double-quotes for attributes,

如果要对属性使用双引号,

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

请参阅:https://*.com/a/967065/498594

Or outside of rails use:

或者在轨道之外使用:

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

#4


2  

Maybe I'm missing something, but what would be so different about using haml for xml as opposed to html?

也许我错过了什么,但是使用haml for xml而不是html会有什么不同?

You say you're struggling, but what are you struggling with? Could you post some sample code?

你说你在挣扎,但是你在苦苦挣扎?你能发一些示例代码吗?

#5


1  

Haml can produce XML just as easily as HTML (I've used it for FBML and XHTML). What problems are you having?

Haml可以像HTML一样轻松地生成XML(我将它用于FBML和XHTML)。你有什么问题?

#6


0  

I've not used HAML, but if you can't make it work another option is Builder.

我没有使用HAML,但如果你不能使它工作,另一种选择是Builder。

#7


-1  

It should be possible. After all you can create plain old XML with Notepad.

它应该是可能的。毕竟,您可以使用记事本创建普通的旧XML。

#8


-1  

what about creating the xml header, e.g. <?xml version="1.0" encoding="UTF-8"?> ?

如何创建xml标头,例如<?xml version =“1.0”encoding =“UTF-8”?>?

#1


36  

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

在HAML中执行XML非常简单,只需使用以下命令启动模板即可:

!!! XML

which produces

哪个产生

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

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

然后正如@beanish先前所说,你“编造自己的标签”:

%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_

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

#2


8  

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

run it through haml

通过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

HAML参考讨论了html标签并给出了一些例子。 HAML参考

#3


3  

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

这演示了一些可能对xml文档有用的东西:

!!! 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.

查看haml引用上的Haml :: Helpers链接,了解更多方法,如haml_tag。

If you want to use double-quotes for attributes,

如果要对属性使用双引号,

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

请参阅:https://*.com/a/967065/498594

Or outside of rails use:

或者在轨道之外使用:

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

#4


2  

Maybe I'm missing something, but what would be so different about using haml for xml as opposed to html?

也许我错过了什么,但是使用haml for xml而不是html会有什么不同?

You say you're struggling, but what are you struggling with? Could you post some sample code?

你说你在挣扎,但是你在苦苦挣扎?你能发一些示例代码吗?

#5


1  

Haml can produce XML just as easily as HTML (I've used it for FBML and XHTML). What problems are you having?

Haml可以像HTML一样轻松地生成XML(我将它用于FBML和XHTML)。你有什么问题?

#6


0  

I've not used HAML, but if you can't make it work another option is Builder.

我没有使用HAML,但如果你不能使它工作,另一种选择是Builder。

#7


-1  

It should be possible. After all you can create plain old XML with Notepad.

它应该是可能的。毕竟,您可以使用记事本创建普通的旧XML。

#8


-1  

what about creating the xml header, e.g. <?xml version="1.0" encoding="UTF-8"?> ?

如何创建xml标头,例如<?xml version =“1.0”encoding =“UTF-8”?>?