Dinc
Dinc

Reputation: 33

How to get the last 4 characters only from a substring with variable length of characters each time

Im optimizing an invoice for xml and I want to have the last 4 characters only of an variable. The variable can have different length of characters for each invoice I receive, but I only want to have the last 4 characters as a result.

I already tried it with a substring code, but then I need to give a specific length where the code needs to get the last 4 charactes from.

Code i Used:

<xsl:template match="/">
   <xsl:for-each select="/x:Invoice">
      <xsl:if test="."><xsl:value-of select="substring(translate(cbc:Note,' ',''),27,4)"/></xsl:if>
   </xsl:for-each>
</xsl:template>

this is the code of an Invoice:

<UBLVersionID>2.0</UBLVersionID>
<CustomizationID>1.0</CustomizationID>
<ProfileID>PowerOnline</ProfileID>
<ID>194545</ID>
<IssueDate>2019-05-16</IssueDate>
<InvoiceTypeCode>1</InvoiceTypeCode>
<Note>FB - bestelling voor HH7416</Note>
<TaxPointDate>2019-05-16</TaxPointDate>
<DocumentCurrencyCode>EUR</DocumentCurrencyCode>
<LineCountNumeric>7</LineCountNumeric>
<OrderReference>
<ID>Invoice of an supplier</ID>
<SalesOrderID>19538</SalesOrderID>
<IssueDate>2019-04-30</IssueDate>

Expected result is the last4 characters of /x:Invoice/cbc:Note so 7416. The note can sometimes be FB - HH6464 so i always want to have the last 4 characters of that cbc:Note

Upvotes: 2

Views: 7856

Answers (1)

Tim C
Tim C

Reputation: 70638

You can use string-length in conjunction with substring to get the last 4 characters

<xsl:value-of select="substring(cbc:Note, string-length(cbc:Note) - 3)" />

(So, doing string-length(cbc:Note) would get the last character, string-length(cbc:Note) - 1 would get the last 2 characters, and so on...)

Upvotes: 7

Related Questions