XML Builder

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:

Hector

Someone

Primary

State Web Site

Lucy

Someone-Else

Registration

History

I would like to change it to produce this:

Hector

Someone

Primary

Web Site

Lucy

Someone-Else

Registration

History

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

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:

Hector

Someone

Primary

State Web Site

Lucy

Someone-Else

Registration

History

I would like to change it to produce this:

Hector

Someone

Primary

Web Site

Lucy

Someone-Else

Registration

History

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

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,

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