Skip to content

Commit

Permalink
readme: added ordered: true
Browse files Browse the repository at this point in the history
  • Loading branch information
HassanAkbar authored and ronaldtse committed Jan 31, 2025
1 parent 86f9ea2 commit c1eae1d
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,99 @@ end
// TODO: How to create mixed content from `#new`?


[[ordered-content]]
==== Ordered content

`ordered: true` maintains the order of **XML Elements**, while `mixed: true` preserves the order of **XML Elements and Content**.

NOTE: When both options are used, `mixed: true` takes precedence.

To specify ordered content, the `ordered: true` option needs to be set at the
`xml` block's `root` method.

Syntax:

[source,ruby]
----
xml do
root 'xml_element_name', ordered: true
end
----

.Applying `ordered` to treat root as ordered content
[example]
====
[source,ruby]
----
class RootOrderedContent < Lutaml::Model::Serializable
attribute :bold, :string
attribute :italic, :string
attribute :underline, :string
xml do
root "RootOrderedContent", ordered: true
map_element :bold, to: :bold
map_element :italic, to: :italic
map_element :underline, to: :underline
end
end
----
[source,xml]
----
<RootOrderedContent>
<underline>Moon</underline>
<italic>384,400 km</italic>
<bold>bell</bold>
</RootOrderedContent>
----
[source,ruby]
----
> instance = RootOrderedContent.from_xml(xml)
> #<RootOrderedContent:0x0000000104ac7240 @bold="bell", @italic="384,400 km", @underline="Moon">
> instance.to_xml
> #<RootOrderedContent><underline>Moon</underline><italic>384,400 km</italic><bold>bell</bold></RootOrderedContent>
----
**Without Ordered True:**
[source,ruby]
----
class RootOrderedContent < Lutaml::Model::Serializable
attribute :bold, :string
attribute :italic, :string
attribute :underline, :string
xml do
root "RootOrderedContent"
map_element :bold, to: :bold
map_element :italic, to: :italic
map_element :underline, to: :underline
end
end
----
[source,xml]
----
<RootOrderedContent>
<underline>Moon</underline>
<italic>384,400 km</italic>
<bold>bell</bold>
</RootOrderedContent>
----
[source,ruby]
----
> instance = RootOrderedContent.from_xml(xml)
> #<RootOrderedContent:0x0000000104ac7240 @bold="bell", @italic="384,400 km", @underline="Moon">
> instance.to_xml
> #<RootOrderedContent>\n <bold>bell</bold>\n <italic>384,400 km</italic>\n <underline>Moon</underline>\n</RootOrderedContent>
----
====


[[xml-schema-location]]
==== Automatic support of `xsi:schemaLocation`

Expand Down

0 comments on commit c1eae1d

Please sign in to comment.