Data Preprocessing and XSLT Syntax

Often it is necessary to rework data for documents. This is not possible in Word itself. Examples are: special totals, complex sorting, address formatting, etc. For these cases, the data source offers the possibility to transform the data before returning it by XSLT. In this case, the data is first loaded on the basis of the data in the DataSource node and then transferred to the Transformation.

PreprocessingStructur Basic processing sequence


The configuration file must be a combination of data source (DataSource) and Stylesheet in the corresponding namespace.

Sampleconfiguration

The following example summarized DataSource configuration and stylesheet. The Stylesheet adds new nodes and in the XML output.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:ds="http://www.awisto.de/xrm/2013/officeintegration/I18NConfigurableDataSource"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <i18n:DataSource>
        <i18n:CallingUserDataSet />
        <i18n:DefaultDataSet />
    </i18n:DataSource>

    <xsl:template match="/data">
        <customtag>Dies wird der Inhalt eines neuen Knotens mit dem Namen ‚customtag’</customtag>
        <tax><xsl:value-of select="totalamount * 0.19"/></tax>
        <xsl:apply-templates />
    </xsl:template>

    <!--  copy all nodes and all children if nothing specified -->
    <xsl:template match='node()|@*'>
        <xsl:copy><xsl:apply-templates select='node()|@*'/></xsl:copy>
    </xsl:template>
</xsl:stylesheet>

XLST Syntax

To process existing XML-Nodes(e.g. with address data) you need to define them, such as

<xsl:template name="BillToAddress" ></xsl:template>

a new node with the name "BillToAdress" in your XML-Output. Afterwards, you need to define its contend via a preexisting nodename, variable and condition, calculations or programmatic logic.

<xsl:template name="BillToAddress" >
    <xsl:value-of select="billto_name" />
    <xsl:if test="billto_contactname/text() != ''">
      <xsl:value-of select="$CR" />
      <xsl:value-of select="billto_contactname" />
    </xsl:if>
    <xsl:if test="billto_line1/text() != ''">
      <xsl:value-of select="$CR" />
      <xsl:value-of select="billto_line1" />
    </xsl:if>
    <xsl:value-of select="$CR" />
    <xsl:value-of select="billto_postalcode" />
    <xsl:value-of select="$SPACE" />
    <xsl:value-of select="billto_city" />
    <xsl:if test="billto_country/text() != '' and billto_country/text() != 'DEUTSCHLAND'">
      <xsl:value-of select="$CR" />
      <xsl:value-of select="billto_country" />
    </xsl:if>
</xsl:template>

In the above example, the invoice address is composed of the fields billto_name, billto_contactname, billto_line1, billto_postalcode, billto_city and billto_country with line breaks $CR, as is usual for letterhead addresses. The field values ​​are inserted within the node in the XML output in sequence. The line break was previously defined in variable $CR:

  <xsl:variable name="CR">
        <xsl:text><![CDATA[]]></xsl:text>
  </xsl:variable>

Variables are thus addressed within the XSLT description language with a pre-defined dollar symbol. The syntax within the select and test attributes is called XPath. This allows you to select existing nodes or their attributes in an XML document, using extensive selection rules and filters. In addition, the data thus obtained can be analyzed and further processed using mathematical and logical operators and more than 100 standard functions.

awisto XLST extension

To simplify formatting inside XSLT documents the following XSLT extension are available. All of them allow formatting of numbers and dates based on the internationalised settings.

Integration

The Namespace of the extensions must be made known to the node in this way:

<xsl:stylesheet version="2.0"
    xmlns:fn="awisto:format-number"
    xmlns:fd="awisto:format-date"
    xmlns:fs="awisto:format-string">

format-number

  • formatint (number)
  • formatfloat(number, accuracy)
  • formatmoney(number, symbol, accuracy)

Format a number with specified accuracy or the currency symbol based on the user settings. Formatmoney allows to specify the accuracy, if no accuracy is specified the default value 2 is taken

<xsl:value-of select=“fn:formatfloat($myvaribale, 2)” />

formate-date

  • format(text, format)

Format a date text based on the user settings and the entered .NET Franwork Time- and Date-Formats.

format-string

  • replace(text, search, replace)

Replace a string search in text text with the specified replace replace.

<xsl:value-of select=“fs:replace($myvaribale, ‘MyString’, ‘MyReplacement’)” />

Script XSLT extensions

The use of scripts inside XSLT-Transformations is supported. Go to msdn.microsoft.com (Script Blocks Using msxsl:script) for more information.

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="userdefined">
<msxsl:script language="C#" implements-prefix="user">
<![CDATA[
    public string StripWhiteSpace(string input)
    {
     return input.Replace(" ", "");
    }
]]>
</msxsl:script>
<xsl:template match="firstname">
    <firstname><xsl:value-of select="user:StripWhiteSpace(text())" /></firstname>
</xsl:template>