开发者

how to target ninth position of a string

开发者 https://www.devze.com 2023-02-11 13:49 出处:网络
hii all can any one please help me <block2> <recei开发者_StackOverflowverAddress>BKTRUS33XBRD</receiverAddress>

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>&#xA;</xsl:text>
 </xsl:template>

 <xsl:template match="receiverAddress[not(substring(.,9,1)='X')]">
     <xsl:value-of select="substring(.,1,string-length() -1)"/>
     <xsl:text>&#xA;</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:

  1. The use of the standard XPath function substring().

  2. How the standard XPath 1.0 functions substring() and string-length() are used together in order to locate/exclude a string at the end of another string.

  3. You could use in XPath 2.0 the standard XPath 2.0 function ends-with() .

  4. How pattern matching is used so that no XSLT conditional instructions are necessary inside the templates.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号