Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Attribute name

Description

Possible Values

Mandatory

Default

times

The number of times that the child nodes will repeat

Any of:

  • A number
  • A variable name, the variable holding the number of repeats – this variable must have been declared in the variables attribute of this or an ancestor node
  • An Expression inside curly braces which will calculate a whole number. The Expression may refer to Attribute Nodes which have been declared in the scriptVariables attribute of this or an ancestor node
    If this property is not set then the first attribute following the repeat node must contain a discriminator attribute. The repeat loop will stop once that discriminator value is encountered.

No

No limit

Snippet 1

For example, the following grammar snippet:
Snippet 1:

Code Block
languagexml
<Repeat times="3">

...


  <Attr name="text" bytes="2" type="String"/>

...


  <Attr name="separator" discriminator="," type="String"/>

...


</Repeat>

...


<Attr name="nextAttribute" bytes="4" type="String"/>


could be used to read the following file:

Code Block
languagexml
aa,bb,cc,John


The grammar would read exactly three two-character strings, separated by commas, before moving to the next attribute. However, if the exact number of times to carry out the repeat was not specified then the grammar would have no way of knowing when to stop. It would then read the bytes Jo as the next two-character string, and declare an error when it did not find the expected comma in the next byte.

Snippet 2

This next snippet is the same as the first, except that the number of times to repeat is read from a variable defined in an ancestor node.Snippet 2:

Code Block
languagexml
<Sequence name="commaSeparatedBlock" variables="repeatCount">

...


  <Attr name="repeatCount" bytes="1" type="Integer"/>

...


  <Repeat times="repeatCount">

...


    <Attr name="text" bytes="2" type="String"/>

...


    <Attr name="separator" discriminator="," type="String"/>

...


  </Repeat>

...


</Sequence>

...


<Attr name="nextAttribute" bytes="4" type="String"/>


To use the attribute repeatCount as a variable in the times parameter of the Repeat block, it must first be declared on an ancestor node in a variables attribute. The scope of the variable is the hierarchy of nodes inside the node that it is declared on. The value in the variable will be set to null when the scope finishes. The variable may only be used within the defined scope.

Snippet 3

The next snippet is the same as the previous, except that the number of times to repeat is calculated by an Expression using a script variable declared on an ancestor node.
Snippet 3:

Code Block
languagexml
<Sequence name="commaSeparatedBlock" scriptVariables="repeatCount">

...


  <Attr name="repeatCount" bytes="1" type="Integer"/>

...


  <Repeat times="{$commaSeparatedBlock_repeatCount + 2}">

...


    <Attr name="text" bytes="2" type="String"/>

...


    <Attr name="separator" discriminator="," type="String"/>

...


  </Repeat>

...


</Sequence>

...


<Attr name="nextAttribute" bytes="4" type="String"/>


To use the attribute repeatCount as a script variable in the times parameter of the Repeat block, it must first be declared on an ancestor node in scriptVariables attribute. Note that in Snippet 2 the variable can be used simply as repeatCount; but in the snippet above the script variable must be used with the format $scope_variableName, i.e. $commaSeparatedBlock_repeatCount.

Snippet 4

If there is no way to determine the number of repeats expected, the grammar must include a way of identifying the end of a repeat loop. Typically this is with some sequence of terminating characters. These are specified in the grammar by using the discriminator attribute, as shown in the snippet below.

Snippet 4:
<Repeat>
<Attr  

Code Block
languagexml
<Repeat>
  <Attr name="text" bytes="2" type="String"/>

...


  <Attr name="separator" discriminator="," type="String"/>

...


</Repeat>

...


<Attr name="terminator" discriminator="||" type="String"/>


In the above example the repeat loop will continue until the string "||" is found, when it will stop.

Snippet 5

The bytes that indicate the end of the repeat loop can also be specified as a hex string, using the hexDiscriminator attribute. The snippet below shows an example of this.

Snippet 5:
<Repeat>
<Attr  

Code Block
languagexml
<Repeat>
  <Attr name="text" bytes="2" type="String"/>

...


  <Attr name="separator" discriminator="," type="String"/>

...


</Repeat>

...


<Attr name="terminator" discriminator="A0FF" type="Integer" hexDiscriminator="T"/>

Sequence Node

Sequence Nodes simply define a collection of child nodes. These can be used to define a set of child nodes as a template; or to define the start and end of a sequence of Attribute Nodes that have an overall length specified by another Attribute Node (for example, a Length Node - see 3.4.4) somewhere within the same node hierarchy.
In addition to common attributes (see 3.1) Sequence Nodes may have the following attributes:

...

Consider a file which includes the following sequence of bytes:

Code Block
languagexml
6aabbcc8


The following grammar could be used to read this portion of the file:

Code Block
languagexml
<Attr name="bodyLength" bytes="1" type="Integer"/>

...


  <Sequence name="body">

...


    <Attr name="attribute1" bytes="2" type="String"/>

...


    <Attr name="attribute2" bytes="2" type="String"/>

...


    <Attr name="attribute3" bytes="2" type="String"/>

...


    <Attr name="attribute4" bytes="1" type="Integer" optional="T"/>

...


  </Sequence>

...


<Attr name="fileLength" bytes="1" type="Integer"/>


However, this grammar is ambiguous. It cannot determine whether the value 8 at the end of the data is the optional attribute4, or fileLength. To resolve this, the grammar below sets the length of the Sequence Node body, using the value in the Attribute Node bodyLength:

Code Block
languagexml
<Sequence name="dataBlock" variables="bodyLength">

...


  ...

...


  <Attr name="bodyLength" bytes="1" type="Integer"/>

...


  <Sequence name="body" length="bodyLength">

...


    <Attr name="attribute1" bytes="2" type="String"/>

...


    <Attr name="attribute2" bytes="2" type="String"/>

...


    <Attr name="attribute3" bytes="2" type="String"/>

...


    <Attr name="attribute4" bytes="1" type="Integer" optional="T"/>

...


  </Sequence>

...


  <Attr name="fileLength" bytes="1" type="Integer"/>

...


  ...

...


</Sequence>


In this grammar bodyLength is declared as a variable on an ancestor node (in this case, another Sequence Node, dataBlock). In the example data above the Attribute Node bodyLength has the value 6. Because we have a specified the length for the Sequence Node body the grammar can now tell, when it reaches the value 8, that it must be fileLength because it has already used 6 bytes for body.

The snippet above could also be written using a Length Node (see 3.4.4) as:

Code Block
languagexml
<Sequence name="body">

...


  <Length name="bodyLength" bytes="1" type="Integer"/>

...


  <Attr name="attribute1" bytes="2" type="String"/>

...


  <Attr name="attribute2" bytes="2" type="String"/>

...


  <Attr name="attribute3" bytes="2" type="String"/>

...


  <Attr name="attribute4" bytes="1" type="Integer" optional="T"/>

...


</Sequence>

...


<Attr name="fileLength" bytes="1" type="Integer"/>


The Length Node sets the length of its parent. This length is exclusive of the Length Node itself.
If the first attribute of the data indicated the length of the Sequence Node body inclusive of the Length Node itself, i.e. the data was:

Code Block
languagexml
7aabbcc8


then the snippet could be written as:

Code Block
languagexml
<Sequence name="body" variables="bodyLength" length="bodyLength">

...


  <Attr name="bodyLength" bytes="1" type="Integer"/>

...


  <Attr name="attribute1" bytes="2" type="String"/>

...


  <Attr name="attribute2" bytes="2" type="String"/>

...


  <Attr name="attribute3" bytes="2" type="String"/>

...


  <Attr name="attribute4" bytes="1" type="Integer" optional="T"/>

...


</Sequence>

...


<Attr name="fileLength" bytes="1" type="Integer"/>

Choice Node

A Choice Node specifies that only one or zero of its child nodes (with all its descendant nodes) may be present in the file. Each child node is therefore optional, and setting the optional attribute to F will have no effect. 
For the binary file reader to determine which of the child nodes under a Choice Node are in a file, the discriminator attribute must be set on:

...