You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@daffodil.apache.org by "Costello, Roger L." <co...@mitre.org> on 2018/05/30 10:29:50 UTC
Best way to convert a 4-byte, little endian integer to a symbolic
value?
I have a 4-byte, little endian integer.
Each integer denotes a shape.
For example, the integer 31 denotes the shape MultiPatch.
So, if the input file contains this value (hex for integer 31):
1F000000
Then I want the XML to be this:
<shapetype>MultiPatch</shapetype>
Of course, this input (lowercase F):
1f000000
should produce the same result:
<shapetype>MultiPatch</shapetype>
What is the best way to express this in DFDL?
/Roger
Re: Best way to convert a 4-byte, little endian integer to a symbolic
value?
Posted by Steve Lawrence <sl...@apache.org>.
Right now, you'd need to do something like this:
<xs:element name="shapeTypeValue" type="xs:int"
dfdl:lengthKind="explicit" dfdl:length="4" dfdl:length="bytes"
dfdl:byteOrder="littleEndian" dfdl:representation="binary" />
<xs:element name="shapeType" type="xs:string" dfdl:inputValueCalc="{
if (../shapeTypeValue eq 31) then "MultiPatch"
else if (../shapeTypeValue eq 32) then "ShapeType2"
else if (../shapeTypeValue eq 33) then "ShapeType3"
else if (../shapeTypeValue eq 34) then "ShapeType4"
...
"}
/>
You might even want to put the shapeTypeValue in a group and reference
it with dfdl:hiddenGroupRef so the physical value isn't in the infoset,
in which case the shapeTypeValue element needs a dfdl:outputValueCalc
property for unparsing, which would look something like this:
dfdl:outputValueCalc="{
if (../shapeType eq "MultiPatch") then 31
else if (../shapeType eq "ShapeType2") then 32
else if (../shapeType eq "ShapeType3") then 33
else if (../shapeType eq "ShapeType4") then 34
...
}"
The main problem with this is is does not scale very well. If there are
a lot of shapetypes, this expression could be huge and slow. And
unfortunately, we see this pattern alot--so often in fact that Mike B.
has written a proposal to support this exact thing. The proposal is here:
https://cwiki.apache.org/confluence/display/DAFFODIL/Proposal%3A+Features+to+Support+Table-Lookup
It isn't implemented yet so if you take a look at it and you think it
won't meet your needs, let us know. We're definitely open to suggestions.
- Steve
On 05/30/2018 06:29 AM, Costello, Roger L. wrote:
> I have a 4-byte, little endian integer.
>
> Each integer denotes a shape.
>
> For example, the integer 31 denotes the shape MultiPatch.
>
> So, if the input file contains this value (hex for integer 31):
>
> 1F000000
>
> Then I want the XML to be this:
>
> <shapetype>MultiPatch</shapetype>
>
> Of course, this input (lowercase F):
>
> 1f000000
>
> should produce the same result:
>
> <shapetype>MultiPatch</shapetype>
>
> What is the best way to express this in DFDL?
>
> /Roger
>