> For the complete documentation index, see [llms.txt](https://support.docstudio.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://support.docstudio.com/admin-panel/conversion-rules/how-to-create-a-conversion-rule-outgoing-documents/conversion-rule-example-outgoing-documents.md).

# Conversion Rule Example (Outgoing Documents)

```xml
<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:wdExtensions="java:com.whitedoc.xslt.extensions.WdExtensions"
                exclude-result-prefixes="wdExtensions">
    <xsl:output indent="yes"/>
    <xsl:param name="SENDER_MAILBOX"/>
    <xsl:param name="TEMPLATE_UUID"/>
    <xsl:param name="TEMPLATE_VERSION"/>

    <xsl:template match="/">
        <xsl:apply-templates select="EDIFACT/ORDERS"/>
        <xsl:apply-templates select="EANCOM/ORDERS"/>
    </xsl:template>

    <xsl:template match="ORDERS">
        <xsl:variable name="supplierGLN" select="../UNB/UNB03/UNB0301"/>
        <xsl:variable name="senderGLN" select="../UNB/UNB02/UNB0201"/>
        <xsl:variable name="supplierUuid" select="{wdExtensions:getValueFromDictionary('a5390637-f3b5-49f3-b7f6-48132f6fe8bb', '7f9d20ab-71d8-45e0-9756-2887fd427cd6', $supplierGLN, 'f69ee017-1fb3-4ff1-a803-c4ade48ea65e')}"/>
        <envelope templateUuid="{$TEMPLATE_UUID}" templateVersion="{$TEMPLATE_VERSION}">
            <info>
                <subject>Замовлення на постачання № <xsl:choose><!--Dlya MTI-->
                    <xsl:when test="../UNB/UNB02/UNB0201=4820140450000"><xsl:value-of select="BGM/BGM02/BGM0201"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="BGM/BGM02"/></xsl:otherwise>
                </xsl:choose>
                </subject>
                <message>Оригінал замовлення</message>
            </info>
            <state>
              <message/>
              <date>
                2021-02-03T12:22:52.709Z
              </date>
              <roleId>

              </roleId>
            </state>
            <documents>
                <document id="cd511754-cfd1-49a3-aae4-aab212d83cea">
                    <field name="NUMBER">
                        <xsl:choose><!--Dlya MTI-->
                            <xsl:when test="../UNB/UNB02/UNB0201=4820140450000"><xsl:value-of select="BGM/BGM02/BGM0201"/></xsl:when>
                            <xsl:otherwise><xsl:value-of select="BGM/BGM02"/></xsl:otherwise>
                        </xsl:choose>
                    </field>
                    <xsl:apply-templates select="DTM"/>
                    <xsl:apply-templates select="GROUP_2" mode="YC1"/>
                    <xsl:if test="GROUP_7/CUX/CUX01/CUX0102 and ../UNB/UNB03/UNB0301!='4829900003227'">
                        <field name="CURRENCY"><xsl:value-of select="GROUP_7/CUX/CUX01/CUX0102"/></field>
                    </xsl:if>
                    <xsl:choose>
                        <xsl:when test="GROUP_1/RFF/RFF01/RFF0102 and ../UNB/UNB02/UNB0201='4820086630009'">
                            <field name="INFO">№ акции: <xsl:value-of select="GROUP_1/RFF/RFF01/RFF0102"/></field>
                        </xsl:when>
                        <xsl:when test="../UNB/UNB02/UNB0201='4823060600005'">
                            <field name="INFO"><xsl:value-of select="FTX/FTX04/FTX0401"/>, <xsl:value-of select="FTX/FTX04/FTX0402"/>, <xsl:value-of select="FTX/FTX04/FTX0403"/></field>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:apply-templates select="GROUP_2" mode="NAD"/>
                    <fieldgroup name="POSITION">
                        <xsl:choose>
                            <xsl:when test="../UNB/UNB03/UNB0301='4829900003227'">
                                <xsl:apply-templates select="GROUP_28" mode="LOREAL"/>
                            </xsl:when>
                            <xsl:when test="GROUP_28">
                                <xsl:apply-templates select="GROUP_28"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:apply-templates select="GROUP_25"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </fieldgroup>
                    <field name="POSITIONSCOUNT"><xsl:value-of select="count(//GROUP_28)"/></field>
                    <field name="SUMORDEREDQUANTITY"><xsl:value-of select="sum(//QTY0102)"/></field>
                </document>
            </documents>
            <flow>
                <roles>
                    <role id="f9378c46-5dfe-484a-b985-5a157d238b5c" mailboxUuid="{$SENDER_MAILBOX}"/>
                    <role id="d59d8545-f1d2-4008-951f-2f43509d966e" mailboxUuid="{$supplierUuid}"/>
                </roles>
            </flow>
        </envelope>
    </xsl:template>

    <xsl:template match="DTM">
        <xsl:choose>
            <xsl:when test="DTM01/DTM0101 = '137'">
                <field name="DATE">
                    <xsl:value-of select="substring(DTM01/DTM0102, 7, 2)"/>-<xsl:value-of select="substring(DTM01/DTM0102, 5, 2)"/>-<xsl:value-of select="substring(DTM01/DTM0102, 1, 4)"/>
                </field>
            </xsl:when>
            <xsl:when test="DTM01/DTM0101 = '2'">
                <xsl:if test="string-length(DTM01/DTM0102) = '12'">
                    <field name="DELIVERYDATE">
                        <xsl:value-of select="substring(DTM01/DTM0102, 7, 2)"/>-<xsl:value-of select="substring(DTM01/DTM0102, 5, 2)"/>-<xsl:value-of select="substring(DTM01/DTM0102, 1, 4)"/>
                    </field>
                </xsl:if>
                <xsl:if test="string-length(DTM01/DTM0102) = '8'">
                    <field name="DELIVERYDATE">
                        <xsl:value-of select="substring(DTM01/DTM0102, 7, 2)"/>-<xsl:value-of select="substring(DTM01/DTM0102, 5, 2)"/>-<xsl:value-of select="substring(DTM01/DTM0102, 1, 4)"/>
                    </field>
                </xsl:if>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="GROUP_2" mode="YC1">
        <xsl:choose>
            <xsl:when test="NAD/NAD01 = 'SU'">
                <xsl:if test="NAD/NAD03">
                    <field name="CAMPAIGNNUMBER">
                        <xsl:value-of select="NAD/NAD03"/>
                    </field>
                </xsl:if> <xsl:apply-templates select="GROUP_3" mode="YC1"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="GROUP_3" mode="YC1">
        <xsl:choose>
            <xsl:when test="RFF/RFF01/RFF0101='YC1'">
                <field name="CAMPAIGNNUMBER">
                    <xsl:value-of select="RFF/RFF01/RFF0102"/>
                </field>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="GROUP_2" mode="NAD">
        <xsl:choose>
            <xsl:when test="NAD/NAD01='SU'">
                <xsl:variable name="supplierRecordValue" select="NAD/NAD02/NAD0201"></xsl:variable>
                <field name="SUPPLIER" recordUuid="{wdExtensions:getRecordUuidByValueFromDictionary('a5390637-f3b5-49f3-b7f6-48132f6fe8bb', '7f9d20ab-71d8-45e0-9756-2887fd427cd6', $supplierRecordValue)}">
                    <xsl:value-of select="$supplierRecordValue"/>
                </field>
            </xsl:when>
            <xsl:when test="NAD/NAD01='BY'">
                <xsl:variable name="buyer" select="string-length(NAD/NAD02/NAD0201)"/>
                <xsl:choose>
                    <xsl:when test="$buyer=13">
                        <xsl:variable name="buyerRecordValue" select="NAD/NAD02/NAD0201"></xsl:variable>
                        <field name="BUYER" recordUuid="{wdExtensions:getRecordUuidByValueFromDictionary('258a20bd-ffe5-47b4-a76a-5d440469e444', '592553ba-3e02-43c1-bff6-b7fa438b8fda', $buyerRecordValue)}">
                            <xsl:value-of select="$buyerRecordValue"/>
                        </field>
                    </xsl:when>
                    <xsl:otherwise>
                        <field name="BUYERCODE">
                            <xsl:value-of select="NAD/NAD02/NAD0201"/>
                        </field>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="NAD/NAD01='DP'">
                <xsl:variable name="deliveryPlaceValue" select="NAD/NAD02/NAD0201"></xsl:variable>
                <field name="DELIVERYPLACE" recordUuid="{wdExtensions:getRecordUuidByValueFromDictionary('258a20bd-ffe5-47b4-a76a-5d440469e444', '592553ba-3e02-43c1-bff6-b7fa438b8fda', $deliveryPlaceValue)}">
                    <xsl:value-of select="$deliveryPlaceValue"/>
                </field>
            </xsl:when>
            <xsl:when test="NAD/NAD01='IV'">
                <xsl:variable name="invoicePartnerRecordValue" select="NAD/NAD02/NAD0201"></xsl:variable>
                <field name="INVOICEPARTNER" recordUuid="{wdExtensions:getRecordUuidByValueFromDictionary('258a20bd-ffe5-47b4-a76a-5d440469e444', '592553ba-3e02-43c1-bff6-b7fa438b8fda', $invoicePartnerRecordValue)}">
                    <xsl:value-of select="$invoicePartnerRecordValue"/>
                </field>
            </xsl:when>
            <xsl:when test="NAD/NAD01='CA'">
                <xsl:if test="NAD/NAD02/NAD0201">
                    <xsl:if test="13>string-length(NAD/NAD02/NAD0201)">
                        <field name="RECIPIENTCODE">
                            <xsl:value-of select="NAD/NAD02/NAD0201"/>
                        </field>
                    </xsl:if>
                </xsl:if>
                <xsl:if test="NAD/NAD05">
                    <field name="RECIPIENTADRESS">
                        <xsl:value-of select="NAD/NAD05"/>
                    </field>
                </xsl:if>
                <xsl:if test="NAD/NAD06">
                    <field name="RECIPIENTCITY">
                        <xsl:value-of select="NAD/NAD06"/>
                    </field>
                </xsl:if>
                <xsl:if test="NAD/NAD04">
                    <field name="RECIPIENTNAME">
                        <xsl:value-of select="NAD/NAD04"/>
                    </field>
                </xsl:if>
            </xsl:when>
            <xsl:when test="NAD/NAD01='CL'">
                <field name="INVOICEPARTNER">
                    <xsl:value-of select="NAD/NAD02/NAD0201"/>
                </field>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="GROUP_28">
        <fieldset index="{position() - 1}">
            <field name="POSITIONNUMBER">
                <xsl:value-of select="position()"/>
            </field>
            <xsl:choose>
                <xsl:when test="boolean(LIN/LIN03/LIN0301)">
                    <field name="PRODUCT">
                        <xsl:value-of select="LIN/LIN03/LIN0301"/>
                    </field>
                </xsl:when>
                <xsl:otherwise>
                    <field name="PRODUCT">4829090909095</field>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates select="PIA"/>
            <xsl:apply-templates select="QTY"/>
            <xsl:choose>
                <xsl:when test="GROUP_32/PRI/PRI01/PRI0101 = 'AAA' and boolean(substring-before(GROUP_32/PRI/PRI01/PRI0102, '.'))">
                    <field name="ORDERPRICE">
                        <xsl:value-of select="substring-before(GROUP_32/PRI/PRI01/PRI0102, '.')"/>.<xsl:value-of select="substring(substring-after(GROUP_32/PRI/PRI01/PRI0102, '.'), 1, 3)"/>
                    </field>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:if test="GROUP_32/PRI/PRI01/PRI0101 = 'AAA'">
                        <field name="ORDERPRICE">
                            <xsl:value-of select="GROUP_32/PRI/PRI01/PRI0102"/>
                        </field>
                    </xsl:if>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:choose>
                <xsl:when test="IMD/IMD03/IMD0304">
                    <field name="CHARACTERISTIC_DESCRIPTION">
                        <xsl:value-of select="translate(IMD/IMD03/IMD0304, '&amp;&lt;&gt;', '')"/><xsl:if test="boolean(IMD03/IMD0305)">(<xsl:value-of select="translate(IMD03/IMD0305, '&amp;&lt;&gt;', '')"/>)</xsl:if>
                    </field>
                </xsl:when>
            </xsl:choose>
            <xsl:if test="FTX"><xsl:apply-templates select="FTX"/></xsl:if>
        </fieldset>
    </xsl:template>

    <xsl:template match="GROUP_28" mode="LOREAL">
        <fieldset index="{position() - 1}">
            <field name="POSITIONNUMBER">
                <xsl:value-of select="position()"/>
            </field>
            <xsl:choose>
                <xsl:when test="boolean(LIN/LIN03/LIN0301)">
                    <field name="PRODUCT">
                        <xsl:value-of select="LIN/LIN03/LIN0301"/>
                    </field>
                </xsl:when>
                <xsl:otherwise>
                    <field name="PRODUCT">4829090909095</field>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates select="PIA" mode="LOREAL"/>
            <xsl:apply-templates select="QTY" mode="LOREAL"/>
        </fieldset>
    </xsl:template>

    <xsl:template match="GROUP_25">
        <fieldset index="{position() - 1}">
            <field name="POSITIONNUMBER">
                <xsl:value-of select="position()"/>
            </field>
            <xsl:choose>
                <xsl:when test="boolean(LIN/LIN03/LIN0301)">
                    <field name="PRODUCT">
                        <xsl:value-of select="LIN/LIN03/LIN0301"/>
                    </field>
                </xsl:when>
                <xsl:otherwise>
                    <field name="PRODUCT">4829090909095</field>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates select="PIA"/>
            <xsl:apply-templates select="QTY"/>
            <xsl:choose>
                <xsl:when test="GROUP_32/PRI/PRI01/PRI0101 = 'AAA' and boolean(substring-before(GROUP_32/PRI/PRI01/PRI0102, '.'))">
                    <field name="ORDERPRICE">
                        <xsl:value-of select="substring-before(GROUP_32/PRI/PRI01/PRI0102, '.')"/>.<xsl:value-of select="substring(substring-after(GROUP_32/PRI/PRI01/PRI0102, '.'), 1, 3)"/>
                    </field>
                </xsl:when>
                <xsl:when test="GROUP_28/PRI/PRI01/PRI0101 = 'AAA' and boolean(substring-before(GROUP_28/PRI/PRI01/PRI0102, '.'))">
                    <field name="ORDERPRICE">
                        <xsl:value-of select="substring-before(GROUP_28/PRI/PRI01/PRI0102, '.')"/>.<xsl:value-of select="substring(substring-after(GROUP_28/PRI/PRI01/PRI0102, '.'), 1, 3)"/>
                    </field>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:if test="GROUP_32/PRI/PRI01/PRI0101 = 'AAA'">
                        <field name="ORDERPRICE">
                            <xsl:value-of select="GROUP_32/PRI/PRI01/PRI0102"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="GROUP_28/PRI/PRI01/PRI0101 = 'AAA'">
                        <field name="ORDERPRICE">
                            <xsl:value-of select="GROUP_28/PRI/PRI01/PRI0102"/>
                        </field>
                    </xsl:if>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:choose>
                <xsl:when test="IMD/IMD03/IMD0304">
                    <field name="CHARACTERISTIC_DESCRIPTION">
                        <xsl:value-of select="translate(IMD/IMD03/IMD0304, '&amp;&lt;&gt;', '')"/><xsl:if test="boolean(IMD/IMD03/IMD0305)">(<xsl:value-of select="translate(IMD/IMD03/IMD0305, '&amp;&lt;&gt;', '')"/>)</xsl:if>
                    </field>
                </xsl:when>
            </xsl:choose>
        </fieldset>
    </xsl:template>

    <xsl:template match="PIA">
        <xsl:choose>
            <xsl:when test="PIA01 = '1'">
                <xsl:if test="PIA02/PIA0202 = 'IN' or PIA02/PIA0202 = 'BP'">
                    <field name="PRODUCTIDBUYER">
                        <xsl:value-of select="PIA02/PIA0201"/>
                    </field>
                </xsl:if>
                <xsl:if test="PIA02/PIA0202 = 'SA'">
                    <field name="PRODUCTIDSUPPLIER">
                        <xsl:value-of select="PIA02/PIA0201"/>
                    </field>
                </xsl:if>
            </xsl:when>
            <xsl:when test="PIA01 = '5'">
                <xsl:if test="PIA02/PIA0202 = 'IN' or PIA02/PIA0202 = 'BP'">
                    <field name="BUYERPARTNUMBER">
                        <xsl:value-of select="PIA02/PIA0201"/>
                    </field>
                </xsl:if>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="PIA" mode="LOREAL">
        <xsl:choose>
            <xsl:when test="PIA01 = '5'">
                <xsl:if test="PIA02/PIA0202 = 'IN' or PIA02/PIA0202 = 'BP'">
                    <field name="PRODUCTIDBUYER">
                        <xsl:value-of select="PIA02/PIA0201"/>
                    </field>
                </xsl:if>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="QTY">
        <xsl:choose>
            <xsl:when test="QTY01/QTY0101 = '21'">
                <field name="ORDEREDQUANTITY">
                    <xsl:value-of select="QTY01/QTY0102"/>
                </field>
                <xsl:if test="boolean(QTY01/QTY0103)">
                    <field name="ORDERUNIT">
                        <xsl:variable name="orderUnitValue" select="QTY01/QTY0103"/>
                        <xsl:value-of select="wdExtensions:getValueFromDictionary('ee0aeb8e-ba06-41c7-8851-8ac38874fd4b', '52140044-da47-4cf9-90c6-988f44499d11', $orderUnitValue, '7aa2b0d9-bb42-4a4f-8588-46cfa4eda07c')"/>
                    </field>
                </xsl:if>
            </xsl:when>
            <xsl:when test="QTY01/QTY0101 = '59'">
                <field name="QUANTITYOFCUINTU">
                    <xsl:value-of select="QTY01/QTY0102"/>
                </field>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="QTY" mode="LOREAL">
        <xsl:choose>
            <xsl:when test="QTY01/QTY0101 = '21'">
                <field name="ORDEREDQUANTITY">
                    <xsl:value-of select="QTY01/QTY0102"/>
                </field>
                <xsl:if test="boolean(QTY01/QTY0103)">
                    <field name="ORDERUNIT">
                        <xsl:variable name="orderUnitValue" select="QTY01/QTY0103"/>
                        <xsl:value-of select="wdExtensions:getValueFromDictionary('ee0aeb8e-ba06-41c7-8851-8ac38874fd4b', '52140044-da47-4cf9-90c6-988f44499d11', $orderUnitValue, '7aa2b0d9-bb42-4a4f-8588-46cfa4eda07c')"/>
                    </field>
                </xsl:if>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="FTX">
        <xsl:choose>
            <xsl:when test="FTX01='QQD'">
                <field name="CONDITIONSTATUS">
                    <xsl:value-of select="FTX04"/>
                </field>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="GROUP_2" mode="NADUP">
        <xsl:if test="NAD/NAD01='DP'">
            <xsl:choose>
                <xsl:when test="NAD/NAD02/NAD0201 = 'xxxxxxxxxxxxx'">xxxxxxxxxxxxx</xsl:when>
                <xsl:otherwise>9099999104043</xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
```

Conversion rule example for outgoing invoice correction. Previous invoice will be chained to a new one.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:wdExtensions="java:com.whitedoc.xslt.extensions.WdExtensions"
                exclude-result-prefixes="wdExtensions">
    <xsl:param name="SENDER_MAILBOX"/>
    <xsl:param name="TEMPLATE_UUID"/>
    <xsl:param name="TEMPLATE_VERSION"/>

    <xsl:template match="/Invoice">
        <envelope templateUuid="{$TEMPLATE_UUID}" templateVersion="{$TEMPLATE_VERSION}">
            <xsl:choose>
                <xsl:when test="InvoiceType = 'InvoiceCorrection'">
                    <xsl:variable name="originalEnvUuid" select="wdExtensions:findEnvelopeUuidByField($TEMPLATE_UUID, 'invoice-number', string(OriginalInvoiceNumber))"/>
                    <xsl:if test="$originalEnvUuid">
                        <xsl:variable name="chainUuid" select="wdExtensions:chainEnvelope($originalEnvUuid)"/>
                        <xsl:if test="$chainUuid">
                            <xsl:attribute name="chainUuid"><xsl:value-of select="$chainUuid"/></xsl:attribute>
                        </xsl:if>
                    </xsl:if>
                </xsl:when>
            </xsl:choose>
            <info>
                <message>
                    <xsl:value-of select="InvoiceNumber"/>
                </message>
            </info>
            <flow>
                <roles>
                    <role id="roleId-0" mailboxUuid="{$SENDER_MAILBOX}"/>
                    <role id="roleId-1" mailboxUuid="{approver}"/>
                </roles>
            </flow>
            <documents>
                <document id="11111111-1111-1111-1111-111111111111">
                    <field name="invoice-number">
                        <xsl:value-of select="InvoiceNumber"/>
                    </field>
                    <field name="invoice-total">
                        <xsl:value-of select="InvoiceTotal"/>
                    </field>
                </document>
            </documents>
        </envelope>
    </xsl:template>
</xsl:stylesheet>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://support.docstudio.com/admin-panel/conversion-rules/how-to-create-a-conversion-rule-outgoing-documents/conversion-rule-example-outgoing-documents.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
