i am trying to update the creditcard number in the below xml, tokenize is not working, can you please look what i missing here?
Input XML:
<Payment>
<CreditCardDetails>
<CreditCard>
<Amount>31.0</Amount>
<CreditCardType>American Express</CreditCardType>
<CreditHolderName>test</CreditHolderName>
<Number>41111111111111</Number>
<ExpiryMonth>8</ExpiryMonth>
<ExpiryYear>2015</ExpiryYear>
</CreditCard>
<CreditCard>
<Amount>31.0</Amount>
<CreditCardType>AMERICAN EXPRESS</CreditCardType>
<CreditHolderName></CreditHolderName>
<Number>34123456544333</Number>
<ExpiryMonth>8</ExpiryMonth>
<ExpiryYear>2015</ExpiryYear>
</CreditCard>
<CreditCard>
<Amount>31.0</Amount>
<CreditCardType>Visa</CreditCardType>
<CreditHolderName>exman</CreditHolderName>
<Number>43434343434434343</Number>
<ExpiryMonth>8</ExpiryMonth>
<ExpiryYear>2015</ExpiryYear>
</CreditCard>
</CreditCardDetails>
</Payment>
XSLT which i am trying:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:str="http://exslt.org/strings"
exclude-result-prefixes="#all" >
<xsl:output method="xml" cdata-section-elements="Payment" omit-xml-declaration="yes" />
<!-- copy all elements and their attributes-->
<xsl:template match="* | @*">
<xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>
<xsl:template match="//*[local-name()='Payment']">
<xsl:variable name="inXML" select="text()"/>
<xsl:variable name="updatedCards">
<xsl:variable name="openCardDetailsTag"><xsl:value-of select="'<CreditCardDetails>'"/></xsl:variable>
<xsl:variable name="closeCardDetailsTag"><xsl:value-of select="'</CreditCardDetails>'"/></xsl:variable>
<xsl:variable name="openCNTag"><xsl:value-of select="'<Number>'"/></xsl:variable>
<xsl:variable name="closeCNTag"><xsl:value-of select="'</Number>'"/></xsl:variable>
<xsl:variable name="openCCTag"><xsl:value-of select="'<CreditCardType>'"/></xsl:variable>
<xsl:variable name="closeCCTag"><xsl:value-of select="'</CreditCardType>'"/></xsl:variable>
<xsl:variable name="allcreditCards" select="normalize-space(substring-before(substring-after($inXML,$openCardDetailsTag),$closeCardDetailsTag))"/>
<xsl:for-each select="str:tokenize($allcreditCards,'<CreditCard>')">
<xsl:variable name="Number" select="normalize-space(substring-before(substring-after(.,$openCNTag),$closeCNTag))"/>
<xsl:variable name="IncomingccType" select="normalize-space(substring-before(substring-after(.,$openCCTag),$closeCCTag))"/>
<xsl:variable name="BeforeCreditcard" select="normalize-space(substring-before(.,$openCNTag))"/>
<xsl:variable name="AfterCreditcard" select="normalize-space(substring-after(.,$closeCNTag))"/>
<xsl:variable name="newCard">
<xsl:call-template name="updateCard">
<xsl:with-param name="ccNum" select="$Number"/>
<xsl:with-param name="ccType" select="$IncomingccType"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="updatedCards">
<xsl:value-of select="concat($updatedCards,$beforeCardNumber,$openCardNumberTag,$newCard,$closeCardNumberTag,$afterCardNumber)"/>
</xsl:variable>
</xsl:for-each>
<xsl:element name="Payment" >
<xsl:value-of select="$updatedCards"/>
</xsl:element>
</xsl:template>
<xsl:template name="updateCard">
<xsl:param name="ccNum"/>
<xsl:param name="ccType"/>
<xsl:variable name="firstDigit" select="substring($ccNum,1,1)"/>
<xsl:variable name="firstTwoDigits" select="substring($ccNum,1,2)"/>
<xsl:variable name="ccTypeFromNumber">
<xsl:choose>
<xsl:when test="firstTwoDigits ='34' or $firstTwoDigits ='37'">
<xsl:value-of select="'AMERICAN EXPRESS'"/>
</xsl:when>
<xsl:when test="$firstDigit ='4'">
<xsl:value-of select="'Visa'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="UNKNOWN"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$ccType = $ccTypeFromNumber">
<xsl:value-of select="concat($ccNum,$ccType)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="WrongNumber"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
expecting OUTPUT XML:
<Payment>
<CreditCardDetails>
<CreditCard>
<Amount>31.0</Amount>
<CreditCardType>American Express</CreditCardType>
<CreditHolderName>test</CreditHolderName>
<Number>WrongNumber</Number>
<ExpiryMonth>8</ExpiryMonth>
<ExpiryYear>2015</ExpiryYear>
</CreditCard>
<CreditCard>
<Amount>31.0</Amount>
<CreditCardType>AMERICAN EXPRESS</CreditCardType>
<CreditHolderName></CreditHolderName>
<Number>34123456544333AMERICAN EXPRESS</Number>
<ExpiryMonth>8</ExpiryMonth>
<ExpiryYear>2015</ExpiryYear>
</CreditCard>
<CreditCard>
<Amount>31.0</Amount>
<CreditCardType>Visa</CreditCardType>
<CreditHolderName>exman</CreditHolderName>
<Number>41111111111111111Visa</Number>
<ExpiryMonth>8</ExpiryMonth>
<ExpiryYear>2015</ExpiryYear>
</CreditCard>
</CreditCardDetails>
</Payment>
trying to replace creditcard number with NewNumber, may i know how i can achieve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…