XML Builder

24 views
Skip to first unread message

bwb

unread,
Oct 25, 2012, 6:26:46 PM10/25/12
to rubyonra...@googlegroups.com
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

bwb

unread,
Oct 25, 2012, 6:30:34 PM10/25/12
to rubyonra...@googlegroups.com
My previous post on this question had an error in the resired target description.  Here is it aagain; apologies for any confusion:

      <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

Dave Aronson

unread,
Oct 25, 2012, 8:02:01 PM10/25/12
to rubyonra...@googlegroups.com
On Thu, Oct 25, 2012 at 10:26 PM, bwb <bwba...@retrievalsystems.com> 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/.

B.W. Basheer

unread,
Oct 26, 2012, 10:44:07 AM10/26/12
to rubyonra...@googlegroups.com


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
--
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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to
rubyonrails-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply all
Reply to author
Forward
0 new messages