Forum: Ruby XmlStruct-1.0.0 released

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A84d51d60791f8e0fb6d27a8a8b2a96e?d=identicon&s=25 Neil Wilson (neilw)
on 2006-03-22 21:54
(Received via mailing list)
XmlStruct
=========

http://rubyforge.org/projects/xmlstruct/

Documentation
=============

http://xmlstruct.rubyforge.org/

About
=====

XML Markup the simple way.

I've uploaded a small library to Rubyforge that allows you
to markup data using XML tags using direct Ruby assignment
following the example of OpenStruct.

The library supports attributes and simple text elements -
all of which are XML escaped before output. The interface is
designed to be as simple as possible, with the least amount
of syntactic sugar. The general idea is that you mark up your
flat data and output a string containing the markup.

The system works best when you have a small amount of data,
and a large amount of irritating XML structure to impose upon
it. The class came about to allow me to quickly draw up XML
messages to send to the UK Government Gateway. Boy can you
tell that that schema was designed by committee.

I hope that it finds some use somewhere.

Usage
=====

I hope the interface is intuitive. Very simply you use assignment
commands, e.g

    require 'rubygems'
    require 'xml_struct'
    @xmldoc = XmlStruct.new
    @xmldoc.GovTalkMessage={:xmlns =>
"http://www.govtalk.gov.uk/CM/envelope"}
    @xmldoc.GovTalkMessage.EnvelopeVersion="2.0"
    md=@xmldoc.GovTalkMessage.Header.MessageDetails
    md.Class="MAFF-IACS-AAPS2001"
    md.Qualifier="request"
    md.Function="submit"
    md.CorrelationID
    md.Transformation="XML"
    md.GatewayTimeStamp
    @xmldoc.GovTalkDetails.Keys.Key[0]={:Type => "VendorNumber"}
    @xmldoc.GovTalkDetails.Keys.Key[0]=275687
    @xmldoc.GovTalkDetails.Keys.Key[1]={:Type => "MainCPH"}
    @xmldoc.GovTalkDetails.Keys.Key[1]="14/02/0327"
    @xmldoc.GovTalkDetails.Body
    print @xmldoc.to_s

outputs

    <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
    <EnvelopeVersion>2.0</EnvelopeVersion>
    <Header>
    <MessageDetails>
    <Class>MAFF-IACS-AAPS2001</Class>
    <Qualifier>request</Qualifier>
    <Function>submit</Function>
    <CorrelationID>
    </CorrelationID>
    <Transformation>XML</Transformation>
    <GatewayTimeStamp>
    </GatewayTimeStamp>
    </MessageDetails>
    </Header>
    </GovTalkMessage>
    <GovTalkDetails>
    <Keys>
    <Key Type="VendorNumber">275687</Key>
    <Key Type="MainCPH">14/02/0327</Key>
    </Keys>
    <Body>
    </Body>
    </GovTalkDetails>


Enjoy
===
Neil Wilson, Aldur Systems Ltd
573b9499030e1ccb867ef80f0ff1ac49?d=identicon&s=25 Justin Bailey (Guest)
on 2006-03-22 22:08
(Received via mailing list)
Have you seen the xml builder project? It's along the same lines but
gives a
much cleaner syntax. Your example could be done along these lines:

xml.GovTalkMessage :xmlns => "http ..." { |xml|
  xml.EnvelopeVersion "2.0"
  xml.Header {
   xml.MessageDetails {
   etc, etc
   }
  }
}

Check it out http://rubyforge.org/projects/builder/
F0223b1193ecc3a935ce41a1edd72e42?d=identicon&s=25 zdennis (Guest)
on 2006-03-23 01:45
(Received via mailing list)
Justin Bailey wrote:
> }
>
> Check it out http://rubyforge.org/projects/builder/

I don't like the reuse of xml inside of each of the blocks. IIRC it is
why I dislike the CGI library. Visually it looks like
everything gets appended right to xml, although thinking twice I know
that the block scope is actually where the xml addition
takes place.

  xml.MyTag :xmlns+."..." { |xml|
    xml.ATag "2.0"
    xml.BTag {
      xml.CTag {
         xml.DTAG { ....etc.... }
      }
    }
  }

The above makes me think I am getting:
  <MyTag>
    <ATag />
    <BTag />
    <CTag />
    <DTag />
  </MyTag>

My brain thinks this at first because of the reuse of "xml". Now
thinking twice I realize that the scope takes precedence, and my
brain conforms.

It seems like the following would be more approriate, although it would
be more typing:

  xml.MyTag :xmlns+."..." { |xml|
    xml.ATag "2.0"
    xml.BTag { |btag|
      btag.CTag { |ctag|
         ctag.DTAG { |dtag| ....etc.... }
      }
    }
  }

This way I can easily visually tell that what the hiearchy looks like in
my xml document w/o having to count tabs or curly braces.

Zach
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2006-03-23 03:08
(Received via mailing list)
I'll add that there's a partial E4X implementation on RubyForge that
could use some TLC too.

T.
10d4acbfdaccb4eee687a428ca00a5d8?d=identicon&s=25 Jim Weirich (weirich)
on 2006-03-23 06:13
zdennis wrote:
> It seems like the following would be more approriate, although it would
> be more typing:
>
>   xml.MyTag :xmlns+."..." { |xml|
>     xml.ATag "2.0"
>     xml.BTag { |btag|
>       btag.CTag { |ctag|
>          ctag.DTAG { |dtag| ....etc.... }
>       }
>     }
>   }
>
> This way I can easily visually tell that what the hiearchy looks like in
> my xml document w/o having to count tabs or curly braces.

If you really want to, that style works fine with XML builder too.

  require 'builder/xmlmarkup'

  xml = Builder::XmlMarkup.new(:indent=>2)

  xml.MyTag { |xml|
    xml.ATag "2.0"
    xml.BTag { |btag|
      btag.CTag { |ctag|
        ctag.DTAG { |dtag|  }
      }
    }
  }
  puts xml.target!

Produces:

  <MyTag>
    <ATag>2.0</ATag>
    <BTag>
      <CTag>
        <DTAG>
        </DTAG>
      </CTag>
    </BTag>
  </MyTag>

--
-- Jim Weirich
A84d51d60791f8e0fb6d27a8a8b2a96e?d=identicon&s=25 Neil Wilson (neilw)
on 2006-03-23 09:09
(Received via mailing list)
One man's meat is another man's poison.

I don't like the nested braces syntax and my system is not interested
in the XML layout. The straight assignment just seems simpler to me,
and I find it useful when I'm doing assignments throughout a program
rather than in just one place.

Builder is much more powerful. I didn't need the power, but I needed
the simplicity. Hence why I built XmlStruct. (That and learning a bit
about reflection and meta-programming).

NeilW
This topic is locked and can not be replied to.