Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#to_xml: Child element with removed namespace will still have parent namespace #43

Open
burtlo opened this issue Oct 12, 2013 · 1 comment
Labels

Comments

@burtlo
Copy link
Collaborator

burtlo commented Oct 12, 2013

The following happymapper model defines a default namespace for the top level element and removes the namespace in the child elements.

 class Address
    include HappyMapper

    namespace :prefix
    tag :address

    has_many :streets, String, tag: 'street', namespace: nil

    has_one :housenumber, String, namespace: nil
    has_one :postcode, String, namespace: nil
    has_one :city, String, namespace: nil
  end

This model will parse XML that looks like the following:

<prefix:address location='home' xmlns:prefix="http://www.unicornland.com/prefix">
  <street>Milchstrasse</street>
  <street>Another Street</street>
  <housenumber>23</housenumber>
  <postcode>26131</postcode>
  <city>Oldenburg</city>
</prefix:address>

However, this model will not persist the xml correctly when #to_xml is used. It instead generates xml of the following format:

<prefix:address location='home' xmlns:prefix="http://www.unicornland.com/prefix">
  <prefix:street>Milchstrasse</street>
  <prefix:street>Another Street</street>
  <prefix:housenumber>23</housenumber>
  <prefix:postcode>26131</postcode>
  <prefix:city>Oldenburg</city>
</prefix:address>

When persisting the default namespace is being applied.

@sshaw
Copy link

sshaw commented Jul 5, 2014

I think that it would be good to clarify in the docs how HappyMapper deals with this in the context of qualified vs unqualified namespaces in XML Schema (if XML Schema conformance is the goal, which seems so). Both of these require mappings that are not obvious, particularly if you want to parse instance docs in a prefix independent manner. See this gist for an example. (Note that these were generated by the jaxb2ruby HappyMapper template).

There, as in @burtlo's example above, :namespace must be set to nil to parse an unqualified instance doc else parsing will fail as the XPath lookup will include the parent's namespace prefix. Not sure if this is a feature or bug 😕.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants