my code looks like this: xml.instruct! xml.county(name: @jurisdiction.name, id: @jurisdiction.jurisdictionID) do xml.officials do @officials.each do |official| xml.official do xml.firstname official.firstname xml.lastname official.lastname xml.eotypeID eotypeID: official.eotypeID xml.eotype EoType.find(official.eotypeID).name xml.sourceID sourceID: official.sourceID xml.source SourceType.find(official.sourceID).name end end end end which produces this: <county name="Some Town" id="9999999999"> <officials> <official> <firstname>Hector</firstname> <lastname>Someone</lastname> <eotypeID eotypeID="1"/> <eotype>Primary</eotype> <sourceID sourceID="1"/> <source>State Web Site</source> </official> <official> <firstname>Lucy</firstname> <lastname>Someone-Else</lastname> <eotypeID eotypeID="3"/> <eotype>Registration</eotype> <sourceID sourceID="6"/> <source>History</source> </official> </officials> </county> I would like to change it to produce this: <county name="Some Town" id="9999999999"> <officials> <official> <firstname>Hector</firstname> <lastname>Someone</lastname> <eotypeID eotypeID="1"/> <eotype>Primary</eotype> <sourceID sourceID="1"/> <source>Web Site</source> </official> <official> <firstname>Lucy</firstname> <lastname>Someone-Else</lastname> <eotype eotypeID="3">Registration</eotype> <source sourceID="6">History</source> </official> </officials> </county> I cannot find any documentation or examples on the point. Can someone offer a helpful hint or point to some useful documentation that covers the subject, where the primary point is this: Using XML Builder, or some other facility, how does one emit an element instance that has both attributes and non-empty content? There are losts of ways to do it with brute force; just trying to find an "elegant" solution. Thanks for any suggestions -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/nV3s1ulAMxUJ. For more options, visit https://groups.google.com/groups/opt_out.
My previous post on this question had an error in the resired target description. Here is it aagain; apologies for any confusion: my code looks like this: xml.instruct! xml.county(name: @jurisdiction.name, id: @jurisdiction.jurisdictionID) do xml.officials do @officials.each do |official| xml.official do xml.firstname official.firstname xml.lastname official.lastname xml.eotypeID eotypeID: official.eotypeID xml.eotype EoType.find(official.eotypeID).name xml.sourceID sourceID: official.sourceID xml.source SourceType.find(official.sourceID).name end end end end which produces this: <county name="Some Town" id="9999999999"> <officials> <official> <firstname>Hector</firstname> <lastname>Someone</lastname> <eotypeID eotypeID="1"/> <eotype>Primary</eotype> <sourceID sourceID="1"/> <source>State Web Site</source> </official> <official> <firstname>Lucy</firstname> <lastname>Someone-Else</lastname> <eotypeID eotypeID="3"/> <eotype>Registration</eotype> <sourceID sourceID="6"/> <source>History</source> </official> </officials> </county> I would like to change it to produce this: <county name="Some Town" id="9999999999"> <officials> <official> <firstname>Hector</firstname> <lastname>Someone</lastname> <eotype eotypeID="1">Primary</eotype> <source sourceID="1">Web Site</source> </official> <official> <firstname>Lucy</firstname> <lastname>Someone-Else</lastname> <eotype eotypeID="3">Registration</eotype> <source sourceID="6">History</source> </official> </officials> </county> I cannot find any documentation or examples on the point. Can someone offer a helpful hint or point to some useful documentation that covers the subject, where the primary point is this: Using XML Builder, or some other facility, how does one emit an element instance that has both attributes and non-empty content? There are losts of ways to do it with brute force; just trying to find an "elegant" solution. Thanks for any suggestions On Thursday, October 25, 2012 6:26:46 PM UTC-4, bwb wrote:> > my code looks like this: > xml.instruct! > xml.county(name: @jurisdiction.name, id: @jurisdiction.jurisdictionID) do > xml.officials do > @officials.each do |official| > xml.official do > xml.firstname official.firstname > xml.lastname official.lastname > xml.eotypeID eotypeID: official.eotypeID > xml.eotype EoType.find(official.eotypeID).name > xml.sourceID sourceID: official.sourceID > xml.source SourceType.find(official.sourceID).name > end > end > end > end > > which produces this: > > <county name="Some Town" id="9999999999"> > <officials> > <official> > <firstname>Hector</firstname> > <lastname>Someone</lastname> > <eotypeID eotypeID="1"/> > <eotype>Primary</eotype> > <sourceID sourceID="1"/> > <source>State Web Site</source> > </official> > <official> > <firstname>Lucy</firstname> > <lastname>Someone-Else</lastname> > <eotypeID eotypeID="3"/> > <eotype>Registration</eotype> > <sourceID sourceID="6"/> > <source>History</source> > </official> > </officials> > </county> > > I would like to change it to produce this: > > <county name="Some Town" id="9999999999"> > <officials> > <official> > <firstname>Hector</firstname> > <lastname>Someone</lastname> > <eotypeID eotypeID="1"/> > <eotype>Primary</eotype> > <sourceID sourceID="1"/> > <source>Web Site</source> > </official> > <official> > <firstname>Lucy</firstname> > <lastname>Someone-Else</lastname> > <eotype eotypeID="3">Registration</eotype> > <source sourceID="6">History</source> > </official> > </officials> > </county> > > I cannot find any documentation or examples on the point. Can someone > offer a helpful hint or point to some useful documentation that covers the > subject, where the primary point is this: Using XML Builder, or some other > facility, how does one emit an element instance that has both attributes > and non-empty content? There are losts of ways to do it with brute force; > just trying to find an "elegant" solution. > > Thanks for any suggestions >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/hUmakDHUs20J. For more options, visit https://groups.google.com/groups/opt_out.
On Thu, Oct 25, 2012 at 10:26 PM, bwb <bwbasheer-QnWlmZY7faaEP6I3uJRHLFaTQe2KTcn/@public.gmane.org> wrote:> Using XML Builder, or some other facility, how does one emit an > element instance that has both attributes and non-empty content?Long story short: pass it the content, and then the attributes. (Or the other way around, but then I think you''d have to put the attributes in braces (since it''s a hash and you''ll have stuff after it), and put parens on the call (so Ruby doesn''t think the hash is a block).) In your case, if I correctly spotted the difference between the current and desired outputs, what I think you want would be: xml.eotype EoType.find(official.eotypeID).name, eotypeID: official.eotypeID xml.source SourceType.find(official.sourceID).name, sourceID: official.sourceID Or, to reduce unclear redundancy: eo_id = official.eotypeID xml.eotype EoType.find(eo_id).name, eotypeID: eo_id src_id = official.sourceID xml.source SourceType.find(src_id).name, sourceID: src_id Or, with the attributes hash first: eo_id = official.eotypeID xml.eotype({ eotypeID: eo_id }, EoType.find(eo_id).name) src_id = official.sourceID xml.source({ sourceID: src_id }, SourceType.find(src_id).name) Try any of these (I''d favor #2) and let me know if it gets you what you want. -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Dave, You nailed it. Either way works fine. I could not find a reference with the grammar explained as you do below, at least not in the specific XML Builder context. Thanks. Page -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Dave Aronson Sent: Thursday, October 25, 2012 8:02 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: Re: [Rails] XML Builder On Thu, Oct 25, 2012 at 10:26 PM, bwb <bwbasheer-QnWlmZY7faaEP6I3uJRHLFaTQe2KTcn/@public.gmane.org> wrote:> Using XML Builder, or some other facility, how does one emit an > element instance that has both attributes and non-empty content?Long story short: pass it the content, and then the attributes. (Or the other way around, but then I think you''d have to put the attributes in braces (since it''s a hash and you''ll have stuff after it), and put parens on the call (so Ruby doesn''t think the hash is a block).) In your case, if I correctly spotted the difference between the current and desired outputs, what I think you want would be: xml.eotype EoType.find(official.eotypeID).name, eotypeID: official.eotypeID xml.source SourceType.find(official.sourceID).name, sourceID: official.sourceID Or, to reduce unclear redundancy: eo_id = official.eotypeID xml.eotype EoType.find(eo_id).name, eotypeID: eo_id src_id = official.sourceID xml.source SourceType.find(src_id).name, sourceID: src_id Or, with the attributes hash first: eo_id = official.eotypeID xml.eotype({ eotypeID: eo_id }, EoType.find(eo_id).name) src_id = official.sourceID xml.source({ sourceID: src_id }, SourceType.find(src_id).name) Try any of these (I''d favor #2) and let me know if it gets you what you want. -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.