You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@daffodil.apache.org by Roger L Costello <co...@mitre.org> on 2023/03/17 12:18:11 UTC

Error: If-expression branches must have similar types, but were int and string

Hi Folks,

I have a one-byte binary input file. If the input is 0 then I want the XML output to be "No Signal". If the input is 1 then I want the output to be "Green". If the input is 2 then I want the output to be "Yellow". If the input if 3 then I want the output to be "Red". For all other inputs, generate an error.

<xs:sequence dfdl:hiddenGroupRef="raw_Signal" />
<xs:element name='Signal' type='xs:string' dfdl:inputValueCalc='{
    if (../binary_Signal eq 0) then "No Signal"
    else if (../binary_Signal eq 1) then "Green"
    else if (../binary_Signal eq 2) then "Yellow"
    else if (../binary_Signal eq 3) then "Red"
    else "Error"
    }' />

<xs:group name="raw_Signal"> 
    <xs:sequence>
        <xs:element name="binary_Signal" type="unsignedint2" dfdl:outputValueCalc='{
            if (../Signal eq "No Signal") then 0
            else if (../Signal eq "Green") then 1
            else if (../Signal eq "Yellow") then 2
            else if (../Signal eq "Red") then 3
            else "daf:error()"
            }' />
    </xs:sequence>
</xs:group>

That results in Daffodil generating this error message:

Error: If-expression branches must have similar types, but were int and string

What am I doing wrong? Below is the entire DFDL schema.  /Roger

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
           xmlns:fn="http://www.w3.org/2005/xpath-functions">

    <xs:annotation>
        <xs:appinfo source="http://www.ogf.org/dfdl/">
            <dfdl:format 
                alignment="1"
                alignmentUnits="bits"  
                binaryFloatRep="ieee"
                binaryNumberRep="binary"   
                bitOrder="leastSignificantBitFirst"
                byteOrder="littleEndian"   
                calendarPatternKind="implicit"
                choiceLengthKind="implicit"
                documentFinalTerminatorCanBeMissing="yes"  
                emptyValueDelimiterPolicy="none" 
                encoding="ISO-8859-1" 
                encodingErrorPolicy="replace" 
                escapeSchemeRef="" 
                fillByte="f" 
                floating="no" 
                ignoreCase="no" 
                initiatedContent="no" 
                initiator="" 
                leadingSkip="0" 
                lengthKind="implicit"
                lengthUnits="bits"  
                nilKind="literalValue" 
                nilValueDelimiterPolicy="none" 
                occursCountKind="implicit"
                outputNewLine="%CR;%LF;"
                representation="binary"
                separator="" 
                separatorPosition="infix" 
                separatorSuppressionPolicy="anyEmpty"
                sequenceKind="ordered" 
                textStandardZeroRep="0" 
                textStandardDecimalSeparator="."
                textStandardInfinityRep="Inf" 
                textStandardExponentRep="E" 
                textStandardNaNRep="NaN" 
                textNumberPattern="#,##0.###;-#,##0.###" 
                textNumberRounding="explicit" 
                textNumberRoundingMode="roundUnnecessary" 
                textNumberRoundingIncrement="0" 
                textStandardGroupingSeparator="," 
                terminator="" 
                textBidi="no" 
                textNumberCheckPolicy="strict" 
                textNumberRep="standard" 
                textOutputMinLength="0" 
                textPadKind="none" 
                textStandardBase="10" 
                textTrimKind="none" 
                trailingSkip="0" 
                truncateSpecifiedLengthString="no" 
                utf16Width="fixed"/>
        </xs:appinfo>
    </xs:annotation>

    <xs:element name="Test">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Segment">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:sequence dfdl:hiddenGroupRef="raw_Signal" />
                            <xs:element name='Signal' type='xs:string' dfdl:inputValueCalc='{
                                if (../binary_Signal eq 0) then "No Signal"
                                else if (../binary_Signal eq 1) then "Green"
                                else if (../binary_Signal eq 2) then "Yellow"
                                else if (../binary_Signal eq 3) then "Red"
                                else "Error"
                                }' />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    
    <xs:group name="raw_Signal"> 
        <xs:sequence>
            <xs:element name="binary_Signal" type="unsignedint2" dfdl:outputValueCalc='{
                if (../Signal eq "No Signal") then 0
                else if (../Signal eq "Green") then 1
                else if (../Signal eq "Yellow") then 2
                else if (../Signal eq "Red") then 3
                else "daf:error()"
                }' />
        </xs:sequence>
    </xs:group>
    
    <xs:simpleType name="unsignedint2" dfdl:length="2" dfdl:lengthKind="explicit">
        <xs:restriction base="xs:unsignedInt"/>
    </xs:simpleType>

</xs:schema>

Re: Error: If-expression branches must have similar types, but were int and string

Posted by Steve Lawrence <sl...@apache.org>.
In your dfdl:outputValueCalc expression, your final else is

   else "daf:error()"

Which is a string because of the quotes. So your if-expression results 
in either a number or a string, which isn't allowed. You instead want to 
call daf:error() by removing the quotes, e.g.

   else daf:error()

Also note that fn:error() is now part of the DFDL spec, with daf:error() 
being deprecated. So changing to this:

   else fn:error()

will remove any warnings you might be seeing. The daf namespace has also 
been deprecated. Functions are now either in the dfdlx namespace if they 
are Daffodil specific functions:

   xmlns:dfdlx="http://www.ogf.org/dfdl/dfdl-1.0/extensions"

or are in the fn namespace.

- Steve

On 2023-03-17 08:18 AM, Roger L Costello wrote:
> Hi Folks,
> 
> I have a one-byte binary input file. If the input is 0 then I want the XML output to be "No Signal". If the input is 1 then I want the output to be "Green". If the input is 2 then I want the output to be "Yellow". If the input if 3 then I want the output to be "Red". For all other inputs, generate an error.
> 
> <xs:sequence dfdl:hiddenGroupRef="raw_Signal" />
> <xs:element name='Signal' type='xs:string' dfdl:inputValueCalc='{
>      if (../binary_Signal eq 0) then "No Signal"
>      else if (../binary_Signal eq 1) then "Green"
>      else if (../binary_Signal eq 2) then "Yellow"
>      else if (../binary_Signal eq 3) then "Red"
>      else "Error"
>      }' />
> 
> <xs:group name="raw_Signal">
>      <xs:sequence>
>          <xs:element name="binary_Signal" type="unsignedint2" dfdl:outputValueCalc='{
>              if (../Signal eq "No Signal") then 0
>              else if (../Signal eq "Green") then 1
>              else if (../Signal eq "Yellow") then 2
>              else if (../Signal eq "Red") then 3
>              else "daf:error()"
>              }' />
>      </xs:sequence>
> </xs:group>
> 
> That results in Daffodil generating this error message:
> 
> Error: If-expression branches must have similar types, but were int and string
> 
> What am I doing wrong? Below is the entire DFDL schema.  /Roger
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>             xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
>             xmlns:fn="http://www.w3.org/2005/xpath-functions">
> 
>      <xs:annotation>
>          <xs:appinfo source="http://www.ogf.org/dfdl/">
>              <dfdl:format
>                  alignment="1"
>                  alignmentUnits="bits"
>                  binaryFloatRep="ieee"
>                  binaryNumberRep="binary"
>                  bitOrder="leastSignificantBitFirst"
>                  byteOrder="littleEndian"
>                  calendarPatternKind="implicit"
>                  choiceLengthKind="implicit"
>                  documentFinalTerminatorCanBeMissing="yes"
>                  emptyValueDelimiterPolicy="none"
>                  encoding="ISO-8859-1"
>                  encodingErrorPolicy="replace"
>                  escapeSchemeRef=""
>                  fillByte="f"
>                  floating="no"
>                  ignoreCase="no"
>                  initiatedContent="no"
>                  initiator=""
>                  leadingSkip="0"
>                  lengthKind="implicit"
>                  lengthUnits="bits"
>                  nilKind="literalValue"
>                  nilValueDelimiterPolicy="none"
>                  occursCountKind="implicit"
>                  outputNewLine="%CR;%LF;"
>                  representation="binary"
>                  separator=""
>                  separatorPosition="infix"
>                  separatorSuppressionPolicy="anyEmpty"
>                  sequenceKind="ordered"
>                  textStandardZeroRep="0"
>                  textStandardDecimalSeparator="."
>                  textStandardInfinityRep="Inf"
>                  textStandardExponentRep="E"
>                  textStandardNaNRep="NaN"
>                  textNumberPattern="#,##0.###;-#,##0.###"
>                  textNumberRounding="explicit"
>                  textNumberRoundingMode="roundUnnecessary"
>                  textNumberRoundingIncrement="0"
>                  textStandardGroupingSeparator=","
>                  terminator=""
>                  textBidi="no"
>                  textNumberCheckPolicy="strict"
>                  textNumberRep="standard"
>                  textOutputMinLength="0"
>                  textPadKind="none"
>                  textStandardBase="10"
>                  textTrimKind="none"
>                  trailingSkip="0"
>                  truncateSpecifiedLengthString="no"
>                  utf16Width="fixed"/>
>          </xs:appinfo>
>      </xs:annotation>
> 
>      <xs:element name="Test">
>          <xs:complexType>
>              <xs:sequence>
>                  <xs:element name="Segment">
>                      <xs:complexType>
>                          <xs:sequence>
>                              <xs:sequence dfdl:hiddenGroupRef="raw_Signal" />
>                              <xs:element name='Signal' type='xs:string' dfdl:inputValueCalc='{
>                                  if (../binary_Signal eq 0) then "No Signal"
>                                  else if (../binary_Signal eq 1) then "Green"
>                                  else if (../binary_Signal eq 2) then "Yellow"
>                                  else if (../binary_Signal eq 3) then "Red"
>                                  else "Error"
>                                  }' />
>                          </xs:sequence>
>                      </xs:complexType>
>                  </xs:element>
>              </xs:sequence>
>          </xs:complexType>
>      </xs:element>
>      
>      <xs:group name="raw_Signal">
>          <xs:sequence>
>              <xs:element name="binary_Signal" type="unsignedint2" dfdl:outputValueCalc='{
>                  if (../Signal eq "No Signal") then 0
>                  else if (../Signal eq "Green") then 1
>                  else if (../Signal eq "Yellow") then 2
>                  else if (../Signal eq "Red") then 3
>                  else "daf:error()"
>                  }' />
>          </xs:sequence>
>      </xs:group>
>      
>      <xs:simpleType name="unsignedint2" dfdl:length="2" dfdl:lengthKind="explicit">
>          <xs:restriction base="xs:unsignedInt"/>
>      </xs:simpleType>
> 
> </xs:schema>