6. XSL-FO extension for Office Open XML

6.1. What it is

XFC supports an XSL-FO extension to generate structured document tags (SDTs) in an Office Open XML document. Structured document tags are WordprocessingML elements that may be used to include form fields - such as text fields and drop-down lists - in an OOXML document and store form data in a dedicated part - called a Custom XML Data part - of the document. In other words, the SDT technology makes it possible to produce simple forms that can be loaded and filled in MS-Word 2007. As Custom XML Data parts are simple XML files the form data can then be easily extracted and processed. For further information regarding structured document tags refer to section 2.5.2 of part 4 (Markup Language Reference) of the Office Open XML specification, available from Ecma International.

6.2. How it works

To include form fields in an OOXML document one must embed custom elements in the XSL-FO tree. These elements must be in a separate namespace specified by XMLmind. This namespace - referred to by prefix sdt in this document - must be declared in the opening tag of the root element of the XSL-FO tree, as shown below.

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
         xmlns:sdt="http://www.xmlmind.com/foconverter/xsl/extensions/docx/sdt">

6.2.1. Text field example

Consider the XSL-FO snippet below:

      <fo:block margin-left="1cm" margin-right="1cm">Name: <fo:inline
      border="solid 1pt blue" padding="1mm"><sdt:text-field binding="name"
      prompt="[Enter your name here.]" title="Name" /></fo:inline></fo:block>

The sdt:text-field element will be converted by XFC to a plain text SDT, which provides the functionality of a basic text field. The prompt attribute specifies placeholder text to be initially displayed in the field. The sdt:text-field element is wrapped in an fo:inline object that carries presentation properties. The initial display of the whole block in MS-Word 2007 is shown below. The next image shows the appearance of the field when selected, and the last one shows the field once filled.

Figure 1. Text field (initial display)

Text field (initial display)

Figure 2. Text field (selected)

Text field (selected)

Figure 3. Text field (filled)

Text field (filled)

The binding attribute of the sdt:text-field element establishes the mapping between the field and an XML element in the Custom XML Data part. In the simplest case the value of this attribute is an XML element name. The Custom XML Data part will be automatically generated by XFC, in the form of a simple XML instance where all elements associated with form fields are children of the root element. Assuming the document contains no other field, XFC will therefore generate the XML instance below:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name></name>
</root>

When saving the document after an editing session MS-Word will store the current value of the field as the content of the name element in the Custom XML Data part, as shown below.

<?xml version="1.0" encoding="UTF-8"?><root>
  <name>John Smith</name>
</root>

6.2.2. Drop-down list example

Consider the XSL-FO snippet below:

      <fo:block margin-left="1cm" margin-right="1cm">Favorite Animal:
      <fo:inline border="solid 1pt blue" padding="1mm"><sdt:drop-down-list
          binding="favorite-animal" initial-value="cat"
          title="Favorite Animal">
          <sdt:list-entry value="cat" />

          <sdt:list-entry value="dog" />

          <sdt:list-entry value="hamster" />
        </sdt:drop-down-list></fo:inline></fo:block>

The sdt:drop-down-list element will be converted by XFC to a drop-down list SDT, which provides the ability to select a single value from a predefined list. The list entries are specified by the sdt:list-entry children. The initial-value attribute of the sdt:drop-down-list element specifies the initial value of the field. The initial display of the whole block in MS-Word 2007 is shown below. The next image shows the appearance of the field while selecting an entry in the list.

Figure 4. Drop-down list (initial display)

Drop-down list (initial display)

Figure 5. Drop-down list (selecting an entry)

Drop-down list (selecting an entry)

The initial-value attribute differs from the prompt attribute in that the specified value is initially stored in the Custom XML Data part. Assuming the document contains no other field, XFC will therefore generate the Custom XML Data part below:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <favorite-animal>cat</favorite-animal>
</root>

6.2.3. Specifying a Custom XML Data template

Sometimes it may be desirable to have form data stored in an XML instance more complex than the default instance generated by XFC. In this case a Custom XML Data template may be specified by inserting an sdt:configuration element before the first fo:page-sequence object in the XSL-FO tree, e.g.:

<sdt:configuration custom-xml-template="custom.xml" />

The custom-xml-template attribute specifies the URL of an XML template to be used as the initial content of the Custom XML Data part. This XML template must be encoded in UTF-8 or UTF-16.

When a Custom XML Data template is specified, the binding attribute of a form field associated with an XML element in the Custom XML Data part references that particular element by means of an XPath 1.0 expression. For instance, consider the XML template below:

<?xml version="1.0" encoding="UTF-8"?>
<order>
  <product>
    <reference />
    <quantity />
  </product>
  <product>
    <reference />
    <quantity />
  </product>
</order>

To associate the reference child of the first product element with a form field one would set the binding attribute value of that field to /order/product[1]/reference. Moreover, when a Custom XML Data template is specified the initial-value attribute of form fields is ignored. If a field is to be initialized the initial value must be stored in the Custom XML Data template as the content of the XML element associated with that field.

6.2.4. Extracting the Custom XML Data part

Office Open XML documents are basically ZIP archives, so the Custom XML Data part can be easily extracted. In accordance with MS-Word's naming scheme XFC stores the Custom XML Data part in ZIP entry customXml/item1.xml.

6.3. Reference Material

This section provides a comprehensive description of the custom elements that make up the XSL-FO extension for Office Open XML. These elements must be in a separate namespace specified by XMLmind. This namespace - referred to by prefix sdt in this document - must be declared in the opening tag of the root element of the XSL-FO tree, as shown below.

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
         xmlns:sdt="http://www.xmlmind.com/foconverter/xsl/extensions/docx/sdt">

There are five elements that translate into a form field:

  • sdt:text-field

  • sdt:drop-down-list

  • sdt:combo-box

  • sdt:date

  • sdt:picture

These are inline-level elements that may appear anywhere inline-level Formatting Objects are allowed.

6.3.1. Generic attributes

The attributes described below apply to all form fields, except for the initial-value and prompt attributes that do not apply to the sdt:picture element.

  • binding

    This attribute establishes the mapping between a field and an XML element in the Custom XML Data part. In the simplest case the value of this attribute is an XML element name. The Custom XML Data part will be automatically generated by XFC, in the form of a simple XML instance where all elements associated with form fields are children of the root element. When a Custom XML Data template is specified the attribute value is an XPath 1.0 expression that identifies the XML element associated with the field. If this attribute is omitted no mapping is established.

  • editable

    This attribute specifies whether or not the field content is editable. Possible values are true (default) and false.

  • initial-value

    This attribute specifies the initial value of the field. The specified value will be stored in the Custom XML Data part, unless a Custom XML Data template is in use. (This attribute has no effect if a Custom XML Data template has been specified. In this case the initial value must be stored in the Custom XML Data template as the content of the XML element associated with the field.)

  • locked

    This attribute specifies whether or not the field is locked. Possible values are true (default) and false. (The feature of a locked field is that it cannot be deleted from the document.)

  • prompt

    This attribute specifies placeholder text to be initially displayed in the field if no initial value is provided. (If both the prompt and initial-value attributes are specified the latter will take precedence.)

  • title

    This attribute specifies the field title. This title is displayed as part of the field outline when the field is selected. The default value is specific to each field type.

6.3.2. sdt:text-field

This element is converted to a plain text SDT, which provides the functionality of a basic text field.

Figure 6. Text field

Text field

Attributes:

  • binding

    See generic attributes.

  • editable

    See generic attributes.

  • initial-value

    See generic attributes.

  • locked

    See generic attributes.

  • multi-line

    This attribute specifies whether or not line breaks are allowed in the field value. Possible values are true and false (default).

  • prompt

    See generic attributes.

  • title

    See generic attributes. (The default value is Text Field).

Content model:

EMPTY

6.3.3. sdt:drop-down-list

This element is converted to a drop-down list SDT, which provides the ability to select a single value from a predefined list.

Figure 7. Drop-down list

Drop-down list

Attributes:

  • binding

    See generic attributes.

  • editable

    See generic attributes.

  • initial-value

    See generic attributes.

  • locked

    See generic attributes.

  • prompt

    See generic attributes.

  • title

    See generic attributes. (The default value is Drop-Down List).

Content model:

(sdt:list-entry)+

6.3.4. sdt:list-entry

This element specifies an entry in the list of possible values of a drop-down list or combo box SDT.

Attributes:

  • display-text

    This attribute specifies alternative text to be displayed when this entry is selected. (By default the actual entry value is displayed.)

  • value

    This attribute specifies the actual entry value. This is the value that will be stored in the Custom XML Data part when this entry is selected. This attribute is required. (The sdt:list-entry element is ignored if this attribute is omitted.)

Content model:

EMPTY

6.3.5. sdt:combo-box

This element is converted to a combo box SDT, which combines a text field and a drop-down list.

Attributes:

  • binding

    See generic attributes.

  • editable

    See generic attributes.

  • initial-value

    See generic attributes.

  • locked

    See generic attributes.

  • prompt

    See generic attributes.

  • title

    See generic attributes. (The default value is Combo Box).

Content model:

(sdt:list-entry)+

6.3.6. sdt:date

This element is converted to a date SDT, which is a text field with date semantics. This SDT provides a date picker for fast and secure input, though a date value may be typed in as well.

Figure 8. Date

Date

Attributes:

  • binding

    See generic attributes.

  • editable

    See generic attributes.

  • format

    This attribute specifies the date format. (This format is used by the date picker but is not enforced when a value is typed in directly.) The attribute value is a character string in which the following variables are recognized:

    VariableExpanded Value
    %Dday of month (01-31)
    %Mmonth (01-12)
    %Yyear (4 digits)
    %yyear (last 2 digits)

    The default value is %Y-%M-%D.

  • initial-value

    See generic attributes.

  • locked

    See generic attributes.

  • prompt

    See generic attributes.

  • title

    See generic attributes. (The default value is Date).

Content model:

EMPTY

6.3.7. sdt:picture

This element is converted to a picture SDT, which provides the ability to select, display and edit images. The value of this field - stored as the content of the associated XML element in the Custom XML Data part - is the Base64-encoded image data.

Figure 9. Picture

Picture

Attributes:

  • binding

    See generic attributes.

  • editable

    See generic attributes.

  • locked

    See generic attributes.

  • title

    See generic attributes. (The default value is Picture).

Content model:

(sdt:image-data)?

6.3.8. sdt:image-data

This element specifies the initial value of an sdt:picture element. It contains the Base64-encoded image data to be initially displayed in the picture SDT. If this element is omitted an image placeholder will be displayed. This placeholder includes a button to open an image selection dialog.

Attributes:

  • format

    This attribute specifies the image data format, in the form of a MIME type. Supported formats are GIF (image/gif), JPEG (image/jpeg) and PNG (image/png). This attribute is required. (The sdt:image-data element is ignored if this attribute is omitted.)

Content model:

#PCDATA

6.3.9. sdt:configuration

This element specifies optional parameters related to the Custom XML Data part. If this element is present in the XSL-FO tree it must occur before the first fo:page-sequence object.

Attributes:

  • custom-xml-template

    This attribute specifies the URL of an XML template to be used as the initial content of the Custom XML Data part. This XML template must be encoded in UTF-8 or UTF-16. The URL is resolved by XFC using its current URI resolver.

  • prefix-mappings

    This attribute specifies the mapping of namespace prefixes used in XPath expressions that identify an element in a Custom XML Data template. The attribute value is a list of namespace declarations separated by white space. This attribute is required if the Custom XML Data template makes use of namespaces. For instance, consider the XML template below:

    <?xml version="1.0" encoding="UTF-8"?>
    <order xmlns="http://www.xmlmind.com/ns/order">
      <product>
        <reference />
        <quantity />
      </product>
    </order>

    As this template contains a namespace declaration, names in XPath expressions that identify an element in the template should be qualified. For this purpose one would set the prefix-mappings attribute and use the so declared namespace prefix to qualify element names in XPath expressions, as shown below.

    <sdt:configuration
      custom-xml-template="custom.xml"
      prefix-mappings="xmlns:ns=&quot;http://www.xmlmind.com/ns&quot;/order" />
    <sdt:text-field binding="/ns:order/ns:product/ns:reference"
      prompt="[Enter product reference.]" title="Reference" />

Content model:

EMPTY