Rails 3、XML 格式和生成器

发布于 2024-11-15 06:38:26 字数 457 浏览 3 评论 0原文

我有一个 xml 标签,需要像这样格式化:

<AddDealRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

我似乎无法使用 builder 使其正常工作。我正在尝试在构建器中执行以下代码:

xml.AddDealRequest(:xmlns:xsi => "http://www.w3.org/2001/XMLSchema-instance", :xmlns:xsd => "http://www.w3.org/2001/XMLSchema" ) do

但显然第二个冒号正在丢弃该符号。有什么办法可以逃脱第二个符号吗?或者这个声明是完全必要的吗?

谢谢!

I have an xml tag that needs to be formatted like so:

<AddDealRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

I can't seem to get this to work properly, using builder. I am attempting the following code in builder:

xml.AddDealRequest(:xmlns:xsi => "http://www.w3.org/2001/XMLSchema-instance", :xmlns:xsd => "http://www.w3.org/2001/XMLSchema" ) do

but obviously that second colon is throwing off the symbol. Is there any way to escape that second symbol? Or is this declaration entirely necessary?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2024-11-22 06:38:26

尝试引用您的符号:

xml.AddDealRequest(
    :'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
    :'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)

您也可以尝试使用字符串而不是符号,

xml.AddDealRequest(
    'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
    'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)

但我不知道构建器是否对此感到满意,但 文档包含类似这样的内容

xm.target("name"=>"compile", "option"=>"fast")
# => <target option="fast" name="compile"\>

因此属性名称的字符串应该有效。

在 irb 中花一点时间可能有助于澄清一些事情:

>> 'where_is:pancakes_house'.to_sym
=> :"where_is:pancakes_house"

>> :'xmlns:xsi'.to_s
=> "xmlns:xsi"

Try quoting your symbols:

xml.AddDealRequest(
    :'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
    :'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)

You could also try using strings instead of symbols

xml.AddDealRequest(
    'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
    'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)

but I don't know if builder will be happy with that but the documentation includes things like this:

xm.target("name"=>"compile", "option"=>"fast")
# => <target option="fast" name="compile"\>

so strings for the attribute names should work.

A bit of time in irb might be help clarify things:

>> 'where_is:pancakes_house'.to_sym
=> :"where_is:pancakes_house"

>> :'xmlns:xsi'.to_s
=> "xmlns:xsi"
℡Ms空城旧梦 2024-11-22 06:38:26

我不会期望任何人阅读最早答案中的所有评论,而是将结果发布在这里:

Firefox 不显示 xmlns 属性(至少不显示)当它与默认值匹配时)。如果您查看源代码 (Ctrl+U) 或使用 Chrome 作为浏览器,您将看到缺少的属性出现在 xml 输出中。

Rather than expect anyone to read through all the comments in the earliest answer, I'll just post the outcome here:

Firefox doesn't display the xmlns attribute (at least not when it matches a default). If you view the source (Ctrl+U) or use Chrome as your browser, you'll see that the missing attributes are appearing in the xml output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文