Creating a script expression to style headings

You can create heading styles on template elements, which corresponds the values for data attributes or variables with certain formatting properties.

About this task

In Microsoft Word, PDF, and HTML outputs, you can create section titles and assign heading styles to those titles, creating an informational hierarchy in your document. Rational® Publishing Engine requires a numeric assignment for those titles that starts with 1 to align the heading style numbers with. Some data that you might want to assign heading styles to do not start with 1 or use string values instead of numeric ones. Complicating the standardization of these styles across all output formats, each output format uses different names for these heading styles. For example, in Microsoft Word, the largest heading style is named Heading 1. In HTML, the same heading style is named H1.

You can use the parseInt(attribute_or_variable_name) JavaScript expression to align attribute or variable values with numeric values starting with 1. Then, to create subheadings on child data, add one to the expression: parseInt(attribute_or_variable_name) + 1. This script expression allows the values to correspond incrementally to the heading styles in all of the Microsoft Word, PDF, and HTML outputs.

Procedure

  1. Drag a Text icon Text element into the template content editor.
  2. Select the text element.
  3. In the Properties view, expand Formatting > common.
  4. In the style name field, click the Configuration icon configuration icon.
  5. Select the Script Expression tab.
  6. Select a variable or attribute.
  7. Enter the JavaScript expression: parseInt(attribute_or_variable_name) + 1
  8. Click OK.

Example

This example uses RequisitePro.xsd Generic XML schema, which includes the Level attribute in the PRRequirement query.

The final output document created by the template in this example:
  • Displays the FullTag values as the section titles.
  • Displays a visible hierarchy of the requirements:
    • The requirements are the largest headings and use the Heading 1 style.
    • The child requirements are subheadings and use the Heading 2 style.
  • Creates the hierarchy on the values of the Level attribute.
To create a report with heading styles applied to the FullTag:
  1. In the Launcher, create a document specification and configure the data source.
  2. In Document Studio, open a new template.
  3. Add the sample data source schema:
    1. In the Data Source Schemas view, click the Add Data Source Schema icon. The Data Source Schema wizard opens and you can click Next.
    2. In the Schema Type field, select Generic XML.
    3. Click Browse and select the RequisitePro.xsd sample data file installed with the product. Default location: %RPE_HOME%\source\XML\examples\RequisitePro.xsd. Click Open.
    4. Enter a custom name for the Data Source ID.
    5. Click Next. Review your selections and click Finish.
  4. Add elements to your template.
    1. Add a Container icon Container element.
    2. Insert a Paragraph icon Paragraph element into the container element.
    3. Insert a Text icon Text element into the paragraph element.
      Template example with elements
  5. Add queries to your elements.
    1. In the Data Source Schemas view, expand the project and requirements.
    2. Drag PRRequirement into the container element.
    3. Drag FullTag (string) into the text element.
    4. In the Select Context window, select Use as value.
    5. Select the PRRequirement container and click OK.
      Template example with the queries in the elements
  6. Create a style script expression for the paragraph element.
    1. Select the paragraph element.
    2. In the Properties view, expand Formatting > common.
    3. In the style name field, click the Configuration icon configuration icon.
    4. In the Set style name value window, select the Script Expression tab.
    5. Expand Attributes and the data source.
    6. Select Level.
    7. Enter parseInt(Level) + 1.
  7. Click Publish > Generate Document to generate the output.

    As the Level values increase by one, the heading styling increases by one as well. The Heading 1 style is set on the FullTag values for the requirements that have 0 as the Level value. The Heading 2 style is set on the FullTag values for the requirements that have 1 as the Level value.

What to do next

A heading style is the most common styling to apply using script expressions. However, you might also try applying script expressions to font sizes, border sizes, or margin sizes as well.

Feedback