Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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>
    &lt;CreditCardDetails&gt;
    &lt;CreditCard&gt;
      &lt;Amount&gt;31.0&lt;/Amount&gt;
      &lt;CreditCardType&gt;American Express&lt;/CreditCardType&gt;
      &lt;CreditHolderName&gt;test&lt;/CreditHolderName&gt;
      &lt;Number&gt;41111111111111&lt;/Number&gt;
      &lt;ExpiryMonth&gt;8&lt;/ExpiryMonth&gt;
      &lt;ExpiryYear&gt;2015&lt;/ExpiryYear&gt;
    &lt;/CreditCard&gt;
    &lt;CreditCard&gt;
      &lt;Amount&gt;31.0&lt;/Amount&gt;
      &lt;CreditCardType&gt;AMERICAN EXPRESS&lt;/CreditCardType&gt;
      &lt;CreditHolderName&gt;&lt;/CreditHolderName&gt;
      &lt;Number&gt;34123456544333&lt;/Number&gt;
      &lt;ExpiryMonth&gt;8&lt;/ExpiryMonth&gt;
      &lt;ExpiryYear&gt;2015&lt;/ExpiryYear&gt;
    &lt;/CreditCard&gt;
    &lt;CreditCard&gt;
      &lt;Amount&gt;31.0&lt;/Amount&gt;
      &lt;CreditCardType&gt;Visa&lt;/CreditCardType&gt;
      &lt;CreditHolderName&gt;exman&lt;/CreditHolderName&gt;
      &lt;Number&gt;43434343434434343&lt;/Number&gt;
      &lt;ExpiryMonth&gt;8&lt;/ExpiryMonth&gt;
      &lt;ExpiryYear&gt;2015&lt;/ExpiryYear&gt;
    &lt;/CreditCard&gt;
    &lt;/CreditCardDetails&gt;

    </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="'&lt;CreditCardDetails&gt;'"/></xsl:variable>
                <xsl:variable name="closeCardDetailsTag"><xsl:value-of select="'&lt;/CreditCardDetails&gt;'"/></xsl:variable>
                <xsl:variable name="openCNTag"><xsl:value-of select="'&lt;Number&gt;'"/></xsl:variable>
                <xsl:variable name="closeCNTag"><xsl:value-of select="'&lt;/Number&gt;'"/></xsl:variable>
                <xsl:variable name="openCCTag"><xsl:value-of select="'&lt;CreditCardType&gt;'"/></xsl:variable>
                <xsl:variable name="closeCCTag"><xsl:value-of select="'&lt;/CreditCardType&gt;'"/></xsl:variable>   
                <xsl:variable name="allcreditCards" select="normalize-space(substring-before(substring-after($inXML,$openCardDetailsTag),$closeCardDetailsTag))"/>

              <xsl:for-each select="str:tokenize($allcreditCards,'&lt;CreditCard&gt;')">                  
                        <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>
&lt;CreditCardDetails&gt;
&lt;CreditCard&gt;
  &lt;Amount&gt;31.0&lt;/Amount&gt;
  &lt;CreditCardType&gt;American Express&lt;/CreditCardType&gt;
  &lt;CreditHolderName&gt;test&lt;/CreditHolderName&gt;
  &lt;Number&gt;WrongNumber&lt;/Number&gt;
  &lt;ExpiryMonth&gt;8&lt;/ExpiryMonth&gt;
  &lt;ExpiryYear&gt;2015&lt;/ExpiryYear&gt;
&lt;/CreditCard&gt;
&lt;CreditCard&gt;
  &lt;Amount&gt;31.0&lt;/Amount&gt;
  &lt;CreditCardType&gt;AMERICAN EXPRESS&lt;/CreditCardType&gt;
  &lt;CreditHolderName&gt;&lt;/CreditHolderName&gt;
  &lt;Number&gt;34123456544333AMERICAN EXPRESS&lt;/Number&gt;
  &lt;ExpiryMonth&gt;8&lt;/ExpiryMonth&gt;
  &lt;ExpiryYear&gt;2015&lt;/ExpiryYear&gt;
&lt;/CreditCard&gt;
&lt;CreditCard&gt;
  &lt;Amount&gt;31.0&lt;/Amount&gt;
  &lt;CreditCardType&gt;Visa&lt;/CreditCardType&gt;
  &lt;CreditHolderName&gt;exman&lt;/CreditHolderName&gt;
  &lt;Number&gt;41111111111111111Visa&lt;/Number&gt;
  &lt;ExpiryMonth&gt;8&lt;/ExpiryMonth&gt;
  &lt;ExpiryYear&gt;2015&lt;/ExpiryYear&gt;
&lt;/CreditCard&gt;
&lt;/CreditCardDetails&gt;

</Payment>

trying to replace creditcard number with NewNumber, may i know how i can achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
184 views
Welcome To Ask or Share your Answers For Others

1 Answer

str:tokenize($allcreditCards,'&lt;CreditCard&gt;')

This cannot work as expected, because each character in the second argument is taken as a delimiting character - see: http://www.exslt.org/str/functions/tokenize/index.html

You could "tokenize" the input yourself, using a recursive processing template.


MODIFIED STYLESHEET:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
<Payment>
    <xsl:call-template name="replaceNumbers">
        <xsl:with-param name="string" select="Payment" />
    </xsl:call-template>
</Payment>
</xsl:template>

<xsl:template name="replaceNumbers">
    <xsl:param name="string"/>
    <xsl:param name="openNum" select="'&lt;Number&gt;'"/>
    <xsl:param name="closeNum" select="'&lt;/Number&gt;'"/>
    <xsl:param name="openType" select="'&lt;CreditCardType&gt;'"/>
    <xsl:param name="closeType" select="'&lt;/CreditCardType&gt;'"/>
<xsl:choose>
    <xsl:when test="contains($string, $openNum) and contains(substring-after($string, $openNum), $closeNum)" >
        <xsl:value-of select="substring-before($string, $openNum)" />
        <xsl:value-of select="$openNum" />
        <xsl:call-template name="updateCard">       
            <xsl:with-param name="ccType" select="substring-before(substring-after($string, $openType), $closeType)" />
            <xsl:with-param name="ccNum" select="substring-before(substring-after($string, $openNum), $closeNum)" />
        </xsl:call-template>
        <xsl:value-of select="$closeNum" />
        <!-- recurse -->
        <xsl:call-template name="replaceNumbers">
                <xsl:with-param name="string" select="substring-after($string, $closeNum)" />
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="updateCard">
    <xsl:param name="ccType"/>
    <xsl:param name="ccNum"/>
    <!-- INSERT YOUR PROCESS HERE -->
    <xsl:value-of select="concat($ccNum, $ccType)" />
</xsl:template>

</xsl:stylesheet> 

Note: you might want to include more tests, if it's possible for a CC to come in without a type before the number.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...