Generate Ruby classes from XML Schema Definition (XSD) - ruby

What is the preferred way to build a Ruby API for writing XML files based on an existing XSD file?
I know there is xsd2ruby from soap4r, but the generated classes are not very useful, IMHO:
The generated initializer has lots of parameters, one for each attribute. I would like to have a Hash param instead
For restrictions (length, enumeration, patterns, ...) no code is generated
What do you use today? Having generated Ruby classes for a complex XSD file would be very helpful.

Related

.NET VisualStudio wsdl multiple xsd files, overlapping definitions

I'm writing an implementation for a mailing service and I'm having trouble with the code that VisualStudio autogenerates from the wsdl file. The API contains several versions of the same objects. The objects are defined in separate xsd files corresponding to a particular version:
Although each xsd file defines a specific namespace, the complex element names are the exact same in all xsd files:
This causes VisualStudio to assign arbitrary names to the classes representing the complex names to retain uniqueness:
If you look at the xsd files, OpenMailingGroup_13B returns an OpenMailingGroupResponse and OpenMailingGroup_15A also returns an OpenMailingGroupResponse, and IMHO that's ok from a definition point of view because they are defined in separate xsd files each having its own separate namespace. However, when VisualStudio generates the proxy classes, it names the return type of OpenMailingGroup_13B as OpenMailingGroupResponse2 and the return type of OpenMailingGroup_15A as OpenMailingGroupResponse4.
This becomes an issue when you update the web service reference and new versions are added and removed (e.g. a new OpenMailingGroup_17X now exists). The problem is that VisualStudio would now use a different naming for the OpenMailingGroupResponse. What was OpenMailingGroupResponse4 for OpenMailingGroup_15A could not be OpenMailingGroupResponse9. And that breaks our code.
We thought of a few possible solutions:
Writing adapter classes - but there are literally hundreds of classes and that would be too painful.
Using var in the variable definition in order not to hardcode the type, but there's lots of nesting involved (e.g. a complex type has another complex type which has another complex type, etc.).
Not update the wsdl...
So, my question is: can I tell VisualStudio to split the generated code into multiple .NET namespaces, each corresponding to its xsd file? Maybe a general question is: is there anything I can do about this?
Thanks in advance,
Tiberiu

How do I get CsvDozerBeanWriter to pull column headers from Dozer XML mapping files

I'm writing a feature to produce CSV snapshots of screen data.
I need this to be data-driven. Thus I need to avoid hard-coding each snapshot in Java, but rather load it from a data source such as an XML file or a database. The data is contained in Java beans.
I'm using SuperCSV with the Dozer extension both at 2.1.0.
This combination seems perfect since I can code the mappings from the beans to the columns in Dozer XML mapping files.
This works well for the data, but I have not found a way to specify the strings to use for the CSV's column headers other than to hard-code them in Java as is done in all of the examples and test cases I've looked at. That is not data-driven.
Is there a way for me to code the column headers in the mapper file. Or even to extract them from the mapper file, construct a List and pass them to the writerHeader() method?
I think it would be OK to just use the bean property names as the headers, although ideal situation is that I am provided some additional meta-data notation in the XML's <Field> tag that specifies the header.
I'd have posted this on SourceForge, but I'm getting a 500 error there.
I'm a Super CSV developer. You're the first person I've heard of who's using CsvDozerBeanWriter with their own DozerBeanMapper - great to hear that feature is useful :)
So what's the goal of being 'data driven'? It sounds like you want your code to be really generic, so you can alter the CSV just by changing the XML. Is that right? Of course, you can't configure the cell processors dynamically...or are you trying to do that too!!??
I'd take a look at the MappingMetadata API of Dozer, which you can access by calling getMappingMetadata() on the DozerBeanMapper. I've never used it, but it looks like you could derive the column names this way (though you'd probably be limited to the field names).
Otherwise, you'll have to parse the XML file yourself (I'd probably use XPath). You'd have to do it this way if you want to use some other metadata in the XML for the column name.

Is there XML binding library for Ruby (like JAXB)?

is there any tool for Ruby which can transform XML (SOAP) to objects and vice versa? And if possible, generate all the objects (models) from XML schema (XSD). I worked several times with JAXB tool (in Java) and I need something simmilar:
generate models from XML schema
easily create component for serializing and deserializing them
easily create component for storing the objects to database
if possible, generate database tables according to that schema
Do you know any tool for this? What approach would you recommend to complete such task?
Thanks for your answers.
Savon should cover SOAP part of it.
I haven't used it but there is a library called HappyMapper: http://happymapper.rubyforge.org/

xsd - validating values from external dictionary file

I would like to define a schema for a document like:
...
<car>
<make>ford</make>
<model>mondeo</model>
</car>
...
the problem is that I would like to constraint possible values (so ford/mondeo or audi/a4 would be valid values for make/model, but audi/mondeo would not) from external data dictionary. In case when new car models needs to be added only external data file would change, but xsd schema would remain the same.
Is this possible at all? I have looked at key/keyref constraint, I see I can use them within a single document, but this is not I'm looking for. I don't want to repeat full data dictionary with every document instance, I would prefer to have the data file rather constitute part of the schema.
That is not possible in XML Schema 1.0.
XML Schema 1.1 will add some support that will allow expressing this kind of constraints (although AFAIK not in external files) - but that is not yet a W3C recommendation.
It is possible to implement this now with Schematron, eventually embedded in XML Schema.
However, there was already work in this area with usable results. See OASIS Code Lists
http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=codelist
More details can be found here:
http://www.genericode.org/
This is used in the OASIS Universal Business Language (UBL)
http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=ubl
Best Regards,
George

Generate Ruby Classes from XSD

Is there a way to generate Ruby classes (maybe even ActiveResource classes) from an XSD so that they contain a way to serialize the classes to xml valid for the initial XSD?
I know that soap4r has xsd2ruby but it appears that the generated ruby classes cannot be easily serialized to xml.
Shameless self promotion (hope this is okay on stackoverflow) but I'm working on an open source project to do just that
Its still a work in progress (feel free to send patches) but the ultimate goal is to convert XSD to/from Ruby classes (which it does now) and convert XML conforming to that XSD to/from instances of those classes.
Though this was asked a while ago, I came across a solution and thought it might help folks in the future.
My need was similar. I have a .xsd from a colleague and would like to generate a class file from it. My hope is that I'll be able to easily marshall the object and pass it to his RESTful end-point, where his Java server will unmarshall the payload and dynamically build the object on his side with no additional effort.
The solution I found was to get the soap4r from https://github.com/rubyjedi/soap4r. I made the two *.rb files in the bin directory executable and then ran:
bin/xsd2ruby.rb --xsd <source>.xsd --classdef <filename_prefix>
This generated a new file with each of the xsd:complexType implemented as a class. All other complex type were also generated with the correct inheritance relationships and all xsd:element was defined as an instance variable and a class initializer also defined.
Running xsd2ruby.rb by itself yielded the options:
~/src/test/soap4r:bin/xsd2ruby.rb
Usage: bin/xsd2ruby.rb --xsd xsd_location [options]
xsd_location: filename or URL
Example:
bin/xsd2ruby.rb --xsd myapp.xsd --classdef foo
Options:
--xsd xsd_location
--classdef [filenameprefix]
--mapping_registry
--mapper
--module_path [Module::Path::Name]
--force
--quiet
For the sake of completeness, I extended my class with the following (this is a "Prospect" class):
class Prospect
include Enumerable
def each(&block)
self.instance_variables.collect{|v| (v.gsub /#/, '').to_sym }.each(&block)
end
end
This let me use it as the body of an Net::HTTP::Post request.
To the question of a free to_xml: I haven't found it. The ruby Object comes with a to_yaml and to_json out of the box, but I've not found any simple conversion to XML. So it came down to a roll my own "to_xml".
Hope this helps.
It appears that this might work.
require 'xsd/mapping'
XSD::Mapping.obj2xml(xsdBasedObject)

Resources