Advanced XML rule example: Remove hyphens (-) from an extracted value

Some number fields on invoices contain values that include hyphens which INVOICES cannot handle in XML invoices. For this example, we will remove hyphens from the SupplierAccountNumber2 field in a Svefaktura invoice so that the extracted value matches the format specification for the field:

  1. Select the SupplierAccountNumber2 field in the Fields section on the Fields tab of the XML Mapper (on the right side of the window).
  2. Click the button to the right of the value to display the Field mapping rules for the field on the left.
  3. Select Use XSL on the bottom left of the screen.
  4. Click Import from advanced rules to add the XSL equivalent of the advanced rules above to the XSL editing box below. (What should it look like?)
    <xsl:template match="/">
      <xsl:for-each select="/x:Invoice/cac:PaymentMeans/cac:PayeeFinancialAccount">
        <xsl:if test="cac:FinancialInstitutionBranch/cac:FinancialInstitution/cac:ID='BGABSESS'">
          <xsl:value-of select="./cac:ID"/>
        </xsl:if>
        <xsl:if test="cac:FinancialInstitutionBranch/cac:FinancialInstitution/cac:ID!='BGABSESS'"></xsl:if>
      </xsl:for-each>
    </xsl:template>
  5. Change the code like this to remove the hyphens.

Required changes added with bold text:

<xsl:template match="/">
  <xsl:for-each select="/x:Invoice/cac:PaymentMeans/cac:PayeeFinancialAccount">
    <xsl:if test="cac:FinancialInstitutionBranch/cac:FinancialInstitution/cac:ID='BGABSESS'">
      <xsl:value-of select="translate(./cac:ID, '-', '')"/>
    </xsl:if>
    <xsl:if test="cac:FinancialInstitutionBranch/cac:FinancialInstitution/cac:ID!='BGABSESS'"></xsl:if>
  </xsl:for-each>
</xsl:template>

Click Extract and review the image to check the results. If you need to modify the rule, you need to click it in the Rules applied to map this field box, click Del to delete it, make the changes needed to fix the rule, and click Add again to add the modified rule. If you need to modify a condition, the process is the same (and if a rule is based on it, the rule is also deleted if you delete the condition).

Consider if the change(s) should apply to all invoices or only those for the current XML invoice definition. Click Save XML definition to save the change for the current XML invoice definition only (that is, the current supplier). Click Save XML profile if you want the rule to apply to all XML invoices that match the XML invoice profile regardless of the XML invoice definition.

Alternative using variables

As an alternative to the changes made in step 5 above, you can also change the extracted value by modifying the XSL using variables in this way:

<xsl:template match="/">
  <xsl:for-each select="/x:Invoice/cac:PaymentMeans/cac:PayeeFinancialAccount">
    <xsl:if test="cac:FinancialInstitutionBranch/cac:FinancialInstitution/cac:ID='BGABSESS'">
      <xsl:variable name="startstring">
        <xsl:value-of select="substring-before(./cac:ID, '-')" />
      </xsl:variable>
      <xsl:variable name="endstring">
        <xsl:value-of select="substring-after(./cac:ID, '-')" />
      </xsl:variable>
      <xsl:value-of select="concat($startstring, $endstring)"/>
    </xsl:if>
    <xsl:if test="cac:FinancialInstitutionBranch/cac:FinancialInstitution/cac:ID!='BGABSESS'"></xsl:if>
  </xsl:for-each>
</xsl:template>
In this case, the <xsl:value-of select="./cac:ID"/> string is replaced by the text in red above. The startstring and endstring variables are declared using the values of the ID node before and after each hyphen. The concat function is then used to combine the two strings. Multiple strings can be combined this way.

Note

Whenever you use XSL to get a desired value, be sure to test the output when the data contains special characters (such as &).

To display special characters normally (an ampersand character, &, would be displayed as &, for example), add <xsl:output method="text" /> to the XSL snippet in this way.  



If this code is not added, the ampersand would be displayed as &amp;.

Other examples of advanced XML rules:

Correct a misspelled field name in connection with master data

Format an extracted XML value with XSL

Modify an extracted XML field value with XSL

Select node X based on node Y (or on attribute a)

Select a specific node when more than one node matches after using a standard rule

Select node X if it exists and node Y if it does not

Specify a constant value for a field

Summarize data from line items as a note in summary invoices

Translate an XML value to one that Kofax ReadSoft Invoices understands