how to target ninth position of a string
hii all can any one please help me
<block2>
<recei开发者_StackOverflowverAddress>BKTRUS33XBRD</receiverAddress>
</block2>
in the above receiver address if x is presented in the ninth position we need to take 1 to 8 digits othervise if x is not presented in ninth position we need to trim last letter of that string
output if x is 9 position is there: BKTRUS33 if x is not present in ninth position :BKTRUS33XBR
how to do in xslt
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="receiverAddress[substring(.,9,1)='X']">
<xsl:value-of select="substring(.,1,8)"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="receiverAddress[not(substring(.,9,1)='X')]">
<xsl:value-of select="substring(.,1,string-length() -1)"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<block2>
<receiverAddress>BKTRUS33XBRD</receiverAddress>
<receiverAddress>BKTRUS33YBRD</receiverAddress>
</block2>
produces the wanted, correct results:
BKTRUS33
BKTRUS33YBR
Do note:
The use of the standard XPath function
substring()
.How the standard XPath 1.0 functions
substring()
andstring-length()
are used together in order to locate/exclude a string at the end of another string.You could use in XPath 2.0 the standard XPath 2.0 function
ends-with()
.How pattern matching is used so that no XSLT conditional instructions are necessary inside the templates.
精彩评论