XmlStruct-1.0.0 released

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”
[email protected]
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 W., Aldur Systems Ltd

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/

Justin B. 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:





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

I’ll add that there’s a partial E4X implementation on RubyForge that
could use some TLC too.

T.

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:

2.0


– Jim W.

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