Reputation: 47
Hoping someone can help me with this and i hope i explain it good enough for someone to be able to understand.
Basically in the xml file being being sent there is a tag "InvBatchNr" the length of this value in this could be anything, however the system i am trying to upload into can only accept 15 characters, so i want the style sheet to handle this situation.
So, if the length of "InvBatchNr" is greater than 15 characters, then take the last 15 characters, if not greater than 15 then use the whole value of "InvBatchNr"
Example: The XML contains
<InvBatchNr>A00006_54324033_PRIMA01ES</InvBatchNr>
With the stylesheet in this example i want to extract '24033_PRIMA01ES'
This is what i have tried so far
<xsl:choose>
<xsl:when test="string-length('InvBatchNr') > 15">
<xsl:value-of select="substring('InvBatchNr', string-length('InvBatchNr'), -15)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='InvBatchNr'/>
</xsl:otherwise>
</xsl:choose>
And despite this not giving me any errors the value returned is actually blank. Any pointers where i am going wrong would be great
Thanks in advance Alan
Upvotes: 1
Views: 1252
Reputation: 66783
If you are referencing an element, InBatchNr
should not have quotes around the element name. Your example is selecting the string literal instead of XPath to select the element named InBatchNr
. Change string-length('InvBatchNr') > 15
to string-length(InvBatchNr) > 15
, and also adjust inside the substring.
If you want to select the substring()
of the last 15 characters, the second parameter should be the position of the character to start at. You are saying to start at the position of string-length()
and continue for -15
characters.
Instead, you want to start at string-length(InBatchNr) - 14
, and then either specify to read 15 characters, or not specify the third parameter so that it reads to the end of the string.
<xsl:choose>
<xsl:when test="string-length(InvBatchNr) > 15">
<xsl:value-of select="substring(InvBatchNr, string-length(InvBatchNr) - 14)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="InvBatchNr"/>
</xsl:otherwise>
</xsl:choose>
Upvotes: 1