ruby – 如何使用Nokogiri和Builder从父元素继承命名空间?
首先 – 看看这列火车的代码残骸:
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 hackery – 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> 我有这个带有安全元素的头部分需要带有命名空间的格式,但是一旦我们点击了Request部分(以及任何后续部分,或任何其他与这个特定API做不同事情的NodeSet ……)文档调用对于非命名空间元素. 简单的问题是:如何生成嵌套在具有命名空间定义的父元素内的NodeSet,而不继承父命名空间(没有我放在一起的恶心黑客)? 我正在使用常见的: builder = Nokogiri::XML::Builder.new do |xml| 而我真正感兴趣的是我如何能够“建造者”并做一些类似的事情: el = builder.at_xpath('//Body') newEl = Nokogiri::XML::Node.new do |node| ...my node stuff here... end el.add_child(newEl) 这样我就可以将这个标题部分(所有消息都需要)抽象到它自己的方法中,并针对通过API公开的功能在不同的主体部分上缝合. 请帮忙! 解决方法
您可以使用其他构建器来完成此操作.
xml['soapenv'].Body do xml << Nokogiri::XML::Builder.new do |request_xml| xml.Request do request_xml.version "1.1" request_xml.name @admin_name.name request_xml.source_version "1.0" request_xml.downloadmarked "0" request_xml.from @dateFrom request_xml.time_from "0000" request_xml.to @dateTo request_xml.time_to "2359" request_xml.limit "100" end end.doc.root.to_xml end 将导致: <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> 使用<<运算符将原始字符串附加到文档.另请注意#doc.root的使用,如果您只使用#to_xml,您将获得<?xml version =“1.0”?>在字符串的开头. 但是,如果要求请求命名空间而不是它的子项,则这种方法不是理想的,因为您必须为每个子项使用构建器(构建器只能有1个根).多个“根”的解决方案是使用DocumentFragment. xml['soapenv'].Body do request = Nokogiri::XML::DocumentFragment.parse "" Nokogiri::XML::Builder.with(request) do |request_xml| request_xml.version "1.1" request_xml.name @admin_name.name request_xml.source_version "1.0" request_xml.downloadmarked "0" request_xml.from @dateFrom request_xml.time_from "0000" request_xml.to @dateTo request_xml.time_to "2359" request_xml.limit "100" end xml.Request << request.to_xml end 将导致: <soapenv:Body> <soapenv: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> </soapenv:Request> </soapenv:Body> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |