【发布时间】:2014-02-17 03:05:34
【问题描述】:
首先 - 看看这段代码的火车残骸:
xml['soapenv'].Body {
xml.Request {
xml.version ("1.1") {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.name (@admin_name.name) {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.source_version ("1.0") {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.downloadmarked ("0") {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.from (@dateFrom) {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.time_from ("0000") {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.to (@dateTo) {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.time_to ("2359") {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.limit ("100") {
xml.parent.namespace = xml.parent.namespace_definitions.first
}
xml.parent.namespace = xml.parent.namespace_definitions.first
}
}
这样创建 XML:
<soapenv:Body>
<Request>
<version>1.1</version>
<name>COMPANY NAME HERE</name>
<source_version>1.0</source_version>
<downloadmarked>0</downloadmarked>
<from>20140125</from>
<time_from>0000</time_from>
<to>20140125</to>
<time_to>2359</time_to>
<limit>100</limit>
</Request>
</soapenv:Body>
没有我所有的 namespace_definitions 黑客——XML 会像这样出来:
<soapenv:Body>
<soapenv:Request>
<soapenv:version>1.1</soapenv:version>
<soapenv:name>COMPANY NAME HERE</soapenv:name>
<soapenv:source_version>1.0</soapenv:source_version>
<soapenv:downloadmarked>0</soapenv:downloadmarked>
<soapenv:from>20140125</soapenv:from>
<soapenv:time_from>0000</soapenv:time_from>
<soapenv:to>20140125</soapenv:to>
<soapenv:time_to>2359</soapenv:time_to>
<soapenv:limit>100</soapenv:limit>
</soapenv:Request>
</soapenv:Body>
我有这个带有安全元素的标头部分,需要使用命名空间的格式,但是一旦我们点击请求部分(以及任何后续部分,或任何其他使用此特定 API 执行不同操作的 NodeSet...)文档要求使用非命名空间元素。
简单的问题是:如何生成嵌套在具有命名空间定义的父元素内的 NodeSet,而不继承父元素的命名空间(没有我放在一起的令人作呕的 hack)?
我用的是普通的:
builder = Nokogiri::XML::Builder.new do |xml|
而我真正感兴趣的是如何使用“builder”并执行以下操作:
el = builder.at_xpath('//Body')
newEl = Nokogiri::XML::Node.new do |node|
...my node stuff here...
end
el.add_child(newEl)
这样我就可以将这个标头部分(所有消息都需要)抽象到它自己的方法中,并缝合不同的主体部分以实现通过 API 公开的功能。
请帮忙!
【问题讨论】:
标签: ruby xml ruby-on-rails-3 xmlhttprequest nokogiri