mbeckerle commented on a change in pull request #396: URL: https://github.com/apache/incubator-daffodil/pull/396#discussion_r453118846
########## File path: daffodil-test/src/test/resources/org/apache/daffodil/section07/discriminators/nestedChoiceDiscriminator.tdml ########## @@ -0,0 +1,306 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<tdml:testSuite + xmlns:ex="http://example.com" + xmlns:tdml="http://www.ibm.com/xmlns/dfdl/testData" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/" + xmlns:daf="urn:ogf:dfdl:2013:imp:daffodil.apache.org:2018:ext" + xmlns:xs="http://www.w3.org/2001/XMLSchema" + xmlns:fn="http://www.w3.org/2005/xpath-functions" + defaultRoundTrip="true" + defaultValidation="on"> + + <tdml:defineSchema name="s0" elementFormDefault="unqualified"> + <xs:include schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd"/> + <dfdl:format ref="ex:GeneralFormat" lengthKind="delimited"/> + + <!-- <![CDATA[ + + This idiom where we capture unrecognized messages under control of a variable is common and + there are issues with discriminators that need testing relative to it. + + ]]> --> + + <dfdl:defineVariable name="capture" type="xs:boolean" external="true" defaultValue="false"/> + + <xs:element name="checkVar" type="xs:string" + dfdl:inputValueCalc='{ xs:string($ex:capture) }'/> + + <xs:group name="discriminate"> + <xs:sequence> + <xs:annotation> + <xs:appinfo source="http://www.ogf.org/dfdl/"> + <dfdl:discriminator>{ fn:true() }</dfdl:discriminator> + </xs:appinfo> + </xs:annotation> + </xs:sequence> + </xs:group> + + <xs:complexType name="messageType"> + <xs:sequence dfdl:separator="|"> + <xs:element name="num" type="xs:int"/> + <xs:element name="text" type="xs:string"/> + </xs:sequence> + </xs:complexType> + + <xs:element name="root"> + <xs:complexType> + <xs:sequence> + <xs:element ref="ex:checkVar"/> + <xs:element name="msg" minOccurs="1" maxOccurs="unbounded"> + <xs:complexType> + <xs:sequence dfdl:initiator="%WSP*;[" dfdl:terminator="]%WSP*;"> + <xs:element name="messageID" type="xs:string" dfdl:lengthKind="explicit" dfdl:length="1"/> + <xs:sequence dfdl:initiator="|"> + <xs:choice> + <xs:choice dfdl:choiceDispatchKey="{ ./messageID }"> + <xs:sequence dfdl:choiceBranchKey="1"> + <!-- + This techique, where we discriminate the outer choice if the choice dispatch + is successful, is better during DFDL schema development because we won't backtrack + and create unrecognized messages that suppress errors during the parsing of a recognized + message type. + --> + <xs:group ref="ex:discriminate"/> + <xs:element name="message1" type="ex:messageType"/> + </xs:sequence> + <xs:sequence dfdl:choiceBranchKey="2"> + <xs:group ref="ex:discriminate"/> + <!-- + This shows the technique for coping with parse errors of recognized message types if we + want to capture them anyway. + + This is not necessarily desirable. The reason why the message failed is masked by this. + --> + <xs:choice> + <xs:element name="message2" type="ex:messageType"/> + <xs:group ref="ex:messageParseError"/> + </xs:choice> + </xs:sequence> + </xs:choice> + <xs:sequence> + <!-- + default case for completely unrecognized message + --> + <xs:sequence> + <xs:annotation> + <xs:appinfo source="http://www.ogf.org/dfdl/"> + <!-- + If capture variable not set, then just fail here, and report an error. + --> + <dfdl:discriminator message='{ fn:concat("Unrecognized messageID: ", ./messageID) }' + test='{ $ex:capture }'/> + </xs:appinfo> + </xs:annotation> + </xs:sequence> + <!-- + Otherwise, create this unrecognized capture element. + + Notice that maxOccurs="0" so valid data never contains these. + Validation checking will reject data that contains these. + + This tells us that the same DFDL schema, with these capture elements expressed in it + can be used for both parse/unparse and for separate validation of the data. + --> + <xs:element name="unrecognizedMessage" minOccurs="0" maxOccurs="0" + dfdl:occursCountKind="expression" + dfdl:occursCount='{ if ($ex:capture) then 1 else 0 }' + type="xs:string"/> + </xs:sequence> + </xs:choice> + </xs:sequence> + </xs:sequence> + </xs:complexType> + </xs:element> + </xs:sequence> + </xs:complexType> + </xs:element> + + <xs:group name="messageParseError"> + <xs:sequence> + <!-- default case for failure to parse a message --> + <xs:sequence> + <xs:annotation> + <xs:appinfo source="http://www.ogf.org/dfdl/"> + <!-- + If capture variable not set, then just fail here, and report an error. + --> + <dfdl:discriminator message='{ fn:concat("Unable to parse message with messageID: ", ./messageID) }' + test='{ $ex:capture }'/> + </xs:appinfo> + </xs:annotation> + </xs:sequence> + <!-- + Otherwise, create this unrecognized capture element Review comment: Not unrecognized capture element. This is a message parse error capture element. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
