Skip to content

Commit

Permalink
Fix sequence numbering in choice with sequences with complexTypes
Browse files Browse the repository at this point in the history
The sequence numbering was resent within the complexType handling and
this change attempts to preserve the sequence numbering across the
elements in the choice alternative handling.
  • Loading branch information
Lars Hesel Christensen committed Jan 10, 2025
1 parent 70cbbf7 commit 9666175
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions priv/choice_complex/choice_complex.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ns:L1 xmlns:ns="http://example.com">
<E_MAIL>
<TEXT>text</TEXT>
</E_MAIL>
</ns:L1>
24 changes: 24 additions & 0 deletions priv/choice_complex/choice_complex.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.com"
targetNamespace="http://example.com" elementFormDefault="unqualified"
attributeFormDefault="unqualified">
<xs:element name="L1">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element name="E_MAIL" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="TEXT" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>

<xs:sequence>
<xs:element name="FLAG" type="xs:boolean" fixed="true" minOccurs="0" />
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
23 changes: 10 additions & 13 deletions src/erlsom_compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -784,24 +784,21 @@ isAny(#alt{tag = '#any', anyInfo = #anyInfo{prCont = "lax", ns = "##any"}}) ->
isAny(_) ->
false.

%% TODO: seqCnt should be passed from one element to the next, but not down into the
%% tree (it should be reset to 0) (the first sequence-in-sequnce should always have nr. 1).
%% -record(localElementType, {name, type, ref, minOccurs, maxOccurs, simpleOrComplex}).
translateSequence([LocalElement = #localElementType{} |Tail], ElementsSoFar, Acc) ->
{Element, Acc2} = translateLocalElement(LocalElement, Acc),
translateSequence(Tail, [Element | ElementsSoFar], Acc2);
{Element, Acc2} = translateLocalElement(LocalElement, Acc#p1acc{seqCnt = 0}),
translateSequence(Tail, [Element | ElementsSoFar], Acc2#p1acc{seqCnt = Acc#p1acc.seqCnt});
translateSequence([ChoiceElement = #choiceType{} |Tail], ElementsSoFar, Acc) ->
{Element, Acc2} = translateElement(ChoiceElement, Acc),
translateSequence(Tail, [Element | ElementsSoFar], Acc2);
{Element, Acc2} = translateElement(ChoiceElement, Acc#p1acc{seqCnt = 0}),
translateSequence(Tail, [Element | ElementsSoFar], Acc2#p1acc{seqCnt = Acc#p1acc.seqCnt});
translateSequence([AnyElement = #anyType{} |Tail], ElementsSoFar, Acc) ->
{Element, Acc2} = translateElement(AnyElement, Acc),
translateSequence(Tail, [Element | ElementsSoFar], Acc2);
{Element, Acc2} = translateElement(AnyElement, Acc#p1acc{seqCnt = 0}),
translateSequence(Tail, [Element | ElementsSoFar], Acc2#p1acc{seqCnt = Acc#p1acc.seqCnt});
translateSequence([GroupElement = #groupRefType{} |Tail], ElementsSoFar, Acc) ->
{Element, Acc2} = translateElement(GroupElement, Acc),
translateSequence(Tail, [Element | ElementsSoFar], Acc2);
{Element, Acc2} = translateElement(GroupElement, Acc#p1acc{seqCnt = 0}),
translateSequence(Tail, [Element | ElementsSoFar], Acc2#p1acc{seqCnt = Acc#p1acc.seqCnt});
translateSequence([SequenceElement = #sequenceType{} | Tail], ElementsSoFar, Acc) ->
{Element, Acc2} = translateSequenceInSequence(SequenceElement, Acc),
translateSequence(Tail, [Element | ElementsSoFar], Acc2);
{Element, Acc2} = translateSequenceInSequence(SequenceElement, Acc#p1acc{seqCnt = 0}),
translateSequence(Tail, [Element | ElementsSoFar], Acc2#p1acc{seqCnt = Acc#p1acc.seqCnt});

translateSequence(undefined, ElementsSoFar, Acc) ->
{lists:reverse(ElementsSoFar), Acc};
Expand Down
6 changes: 6 additions & 0 deletions test/erlsom_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ xsi_type_no_prefix_read_test() ->
?assertMatch({'ExtType', _, "base", "ext"}, Parsed).


choice_complex_test() ->
{ok, Xsd} = file:read_file(priv_path(["choice_complex", "choice_complex.xsd"])),
{ok, Xml} = file:read_file(priv_path(["choice_complex", "choice_complex.xml"])),
{ok, Model} = erlsom:compile(Xsd),
{ok, _Data, _} = erlsom:scan(Xml, Model).

0 comments on commit 9666175

Please sign in to comment.