[ANN] XmlStruct-1.0.0 released

N

NeilW

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 Wilson, Aldur Systems Ltd
 
J

Justin Bailey

------=_Part_5958_7421222.1143061694870
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D> "http ..." { |xml|
xml.EnvelopeVersion "2.0"
xml.Header {
xml.MessageDetails {
etc, etc
}
}
}

Check it out http://rubyforge.org/projects/builder/

XmlStruct
=3D=3D=3D=3D=3D=3D=3D=3D=3D

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

Documentation
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

http://xmlstruct.rubyforge.org/

About
=3D=3D=3D=3D=3D

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
=3D=3D=3D=3D=3D

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

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

outputs

<GovTalkMessage xmlns=3D"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=3D"VendorNumber">275687</Key>
<Key Type=3D"MainCPH">14/02/0327</Key>
</Keys>
<Body>
</Body>
</GovTalkDetails>


Enjoy
=3D=3D=3D
Neil Wilson, Aldur Systems Ltd

------=_Part_5958_7421222.1143061694870--
 
Z

zdennis

Justin said:
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/

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
 
T

Trans

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

T.
 
J

Jim Weirich

zdennis said:
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>
 
N

NeilW

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top