There's good news, and bad news.

The good news: The dfdl:newVariableInstance is the way this is supposed to work.


It creates a "stack allocated" variable. The variable and its type must be 
declared at top level, then entering a scope having newVariableInstance 
referencing that var creates a local one which is temporary, until the 
parser/unparser leaves that scope.


The bad news:  Alas.... Daffodil has not yet implemented newVariableInstance.


The good news: One can always achieve (for parsing anyway) the same thing by an 
element having dfdl:inputValueCalc.


So below, instead of your


               <xsl:variable name="Hidden_color_number" 
select="../Hidden_color_number"/>


write


               <xs:element name="HCN"  type="xs:int"

                    dfdl:inputValueCalc='{ ../Hidden_color_number }' />


Instead of a variable you get an element, in this case "HCN", with a known and 
fixed relative path, which is defined in terms of the expression which contains 
the varying relative path that you are trying to keep out of your expression.


The existence of this work-around is one of the reasons we haven't gotten 
around to dfdl:newVariableInstance as of yet.


This is one of the softer areas of the DFDL spec. This workaround works, but 
only for parsing, since inputValueCalc elements aren't evaluated when 
unparsing. So really we do need newVariableInstance to work; however, there are 
shortcoming there as well having to do with array instances.

________________________________
From: Costello, Roger L. <[email protected]>
Sent: Monday, November 5, 2018 11:44 AM
To: [email protected]
Subject: Can I create a local variable in DFDL?

Hello DFDL community!

I want to create a local variable; is that possible in DFDL?

For example, I would like to create a variable to hold the value of a hidden 
element and then use that variable in an inputValueCalc expression. Below I 
show the idea: I create a variable named Hidden_color_number and then use that 
variable in the inputValueCalc expression. I show creating an XSLT variable, 
which is obviously not correct. Is there a way to do this in DFDL?  /Roger

<xs:element name="enum-test">
    <xs:complexType>
        <xs:sequence>

            <xs:sequence dfdl:hiddenGroupRef="hidden_Color_Group" />

            <xsl:variable name="Hidden_color_number" 
select="../Hidden_color_number"/>

            <xs:element name='color' type='xs:string' dfdl:inputValueCalc="{
                doc('Colors.xml')//row[number eq 
xs:string($Hidden_color_number)]/symbol}" />

        </xs:sequence>
    </xs:complexType>
</xs:element>

Reply via email to