Getting ActiveRecord (Rails) to_xml to use xsi:nil and xsi:type instead of nil and type
Posted
by nbeyer
on Stack Overflow
See other posts from Stack Overflow
or by nbeyer
Published on 2010-04-14T18:16:22Z
Indexed on
2010/04/14
20:03 UTC
Read the original article
Hit count: 707
The default behavior of XML serialization (to_xml) for ActiveRecord objects will emit 'type' and 'nil' attributes that are similar to XML Schema Instance attributes, but aren't set in a XML Namespace.
For example, a model might produce an output like this:
<user>
<username nil="true" />
<first-name type="string">Name</first-name>
</user>
Is there anyway to get to_xml to utilize the XML Schema Instance namespace and prefix the attributes and the values?
Using the above example, I'd like to produce the following:
<user xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xs="http://www.w3.org/1999/XMLSchema">
<username xsi:nil="true" />
<first-name xsi:type="xs:string">Name</first-name>
</user>
© Stack Overflow or respective owner