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

XSLT: Written my XSLT like this but I'm not getting required output as below that means I want to loop through each record and get frontimagefilename and rearimagefilename for each check.

   <xsl:template name="ChequeDetailsTemplate">
                <xsl:param name ="vItemcurrIndx" />
            <xsl:variable name ="x" select ="$allRecs[$vItemcurrIndx]/Content/*" />             
        <xsl:call-template name="loop">
          <xsl:with-param name="i" select="1"/>
          <xsl:with-param name="limit" select="$x/NumberOfCheques"/>
          <xsl:with-param name="vItemcurrIndx" select="position()"></xsl:with-param>
        </xsl:call-template>
    </xsl:template>  
  <xsl:template name="loop">
       <xsl:param name="i"/>
       <xsl:param name="limit"/>
       <xsl:param name ="vItemcurrIndx" />
       <xsl:variable name ="y" select ="$allRecs[$vItemcurrIndx]/Content/*" />
          <xsl:if test="$i &lt;= $limit">
                    <Check>
                    <CheckAmount><xsl:value-of select="concat(./CourtesyAmount,$i)"/> </CheckAmount>
                    <FrontImage><xsl:value-of select="concat(./FrontImageFilename,$i)"/></FrontImage>
                    <RearImage><xsl:value-of select="concat(./RearImageFilename,$i)"/></RearImage>
                  </Check>
            <xsl:call-template name="loop">
              <xsl:with-param name="i" select="$i+1"/>
              <xsl:with-param name="limit" select="$limit"/>
            </xsl:call-template>
          </xsl:if>
  </xsl:template>

Output looking for:

-<Check><CheckAmount>300</CheckAmount><Codeline><006474< :########## 12345678<</Codeline><FrontImage>C:Jan292015archive (1) - CopyarchiveBINTempChequeImagesCHQNCR-2VD8NP348TD20150129135318f001.tif</FrontImage><RearImage>C:Jan292015archive (1) - CopyarchiveBINTempChequeImagesCHQNCR-2VD8NP348TD20150129135318r001.tif</RearImage><CourtesyAmount>300</CourtesyAmount></Check>-<Check><CheckAmount>300</CheckAmount><Codeline><006474< :########## 12345678<</Codeline><FrontImage>C:Jan292015archive (1) - CopyarchiveBINTempChequeImagesCHQNCR-2VD8NP348TD20150129135320f002.tif</FrontImage><RearImage>C:Jan292015archive (1) - CopyarchiveBINTempChequeImagesCHQNCR-2VD8NP348TD20150129135320r002.tif</RearImage><CourtesyAmount>300</CourtesyAmount></Check>

But the output which im getting now is:

-<Check><CheckAmount>1</CheckAmount><FrontImage>1</FrontImage><RearImage>1</RearImage></Check>-<Check><CheckAmount>2</CheckAmount><FrontImage>2</FrontImage><RearImage>2</RearImage></Check>
See Question&Answers more detail:os

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

1 Answer

If I am guessing (!) correctly, your input looks something like this:

<ChqDepAppSrvChequeScanComplete>
    <CourtesyAmount1>100</CourtesyAmount1>
    <FrontImageFilename1 t="String">R:in_simulatedImagesTempChequeImages100f001.bmp</FrontImageFilename1>
    <RearImageFilename1 t="String">R:in_simulatedImagesTempChequeImages100r001.bmp</RearImageFilename1>
    <RefuseReason1 t="String">None</RefuseReason1>

    <CourtesyAmount2>200</CourtesyAmount2>
    <FrontImageFilename2 t="String">R:in_simulatedImagesTempChequeImages200f002.bmp</FrontImageFilename2>
    <RearImageFilename2 t="String">R:in_simulatedImagesTempChequeImages200r002.bmp</RearImageFilename2>
    <RefuseReason2 t="String">None</RefuseReason2>

    <NumberOfCheques t="Int32">2</NumberOfCheques>
</ChqDepAppSrvChequeScanComplete>

Your mistake is in the way you have built this expression:

<xsl:value-of select="concat(./CourtesyAmount,$i)"/>

There is no <CourtesyAmount> node in your input, so you are concatenating nothing and $i, resulting in the current value of $i.

What you really want to do here is get the value from a node whose name is the concatenation of the string "CourtesyAmount" and the current value of $i. Something like:

XSLT 1.0

<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="/ChqDepAppSrvChequeScanComplete">
    <output>
        <xsl:call-template name="loop">
            <xsl:with-param name="limit" select="NumberOfCheques"/>
        </xsl:call-template>
    </output>
</xsl:template>

<xsl:template name="loop">
    <xsl:param name="i" select="1"/>
    <xsl:param name="limit"/>
    <xsl:if test="$i &lt;= $limit">
        <Check>
            <CheckAmount>
                <xsl:value-of select="*[local-name()=concat('CourtesyAmount', $i)]"/>
            </CheckAmount>
            <FrontImage>
                <xsl:value-of select="*[local-name()=concat('FrontImageFilename', $i)]"/>
            </FrontImage>
            <RearImage>
                <xsl:value-of select="*[local-name()=concat('RearImageFilename', $i)]"/>
            </RearImage>
        </Check>
        <xsl:call-template name="loop">
            <xsl:with-param name="i" select="$i+1"/>
            <xsl:with-param name="limit" select="$limit"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

which when applied to the above input will result in:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <Check>
      <CheckAmount>100</CheckAmount>
      <FrontImage>R:in_simulatedImagesTempChequeImages100f001.bmp</FrontImage>
      <RearImage>R:in_simulatedImagesTempChequeImages100r001.bmp</RearImage>
   </Check>
   <Check>
      <CheckAmount>200</CheckAmount>
      <FrontImage>R:in_simulatedImagesTempChequeImages200f002.bmp</FrontImage>
      <RearImage>R:in_simulatedImagesTempChequeImages200r002.bmp</RearImage>
   </Check>
</output>

Note:

The real problem here is the input. It should not be formatted this way. Things would be much easier if the person ahead of you did a proper job and supplied you with a well-structured XML, for example:

<ChqDepAppSrvChequeScanComplete>
    <NumberOfCheques t="Int32">2</NumberOfCheques>
    <Cheque>
        <CourtesyAmount>100</CourtesyAmount>
        <FrontImageFilename t="String">R:in_simulatedImagesTempChequeImages100f001.bmp</FrontImageFilename>
        <RearImageFilename t="String">R:in_simulatedImagesTempChequeImages100r001.bmp</RearImageFilename>
        <RefuseReason t="String">None</RefuseReason>
    </Cheque>
    <Cheque>
        <CourtesyAmount>200</CourtesyAmount>
        <FrontImageFilename t="String">R:in_simulatedImagesTempChequeImages200f002.bmp</FrontImageFilename>
        <RearImageFilename t="String">R:in_simulatedImagesTempChequeImages200r002.bmp</RearImageFilename>
        <RefuseReason t="String">None</RefuseReason>
    </Cheque>
</ChqDepAppSrvChequeScanComplete>

with none of this numbered nodes nonsense.


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