XSLT:连接并选择元素

时间:2015-02-01 23:44:36

标签: xslt

XSLT: 像这样编写我的XSLT,但是我没有得到如下所示的输出,这意味着我想循环遍历每条记录并为每次检查获取frontimagefilename和rearimagefilename。

   <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>

输出寻找:

-<Check><CheckAmount>300</CheckAmount><Codeline><006474< :########## 12345678<</Codeline><FrontImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135318f001.tif</FrontImage><RearImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135318r001.tif</RearImage><CourtesyAmount>300</CourtesyAmount></Check>-<Check><CheckAmount>300</CheckAmount><Codeline><006474< :########## 12345678<</Codeline><FrontImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135320f002.tif</FrontImage><RearImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135320r002.tif</RearImage><CourtesyAmount>300</CourtesyAmount></Check>

但我现在得到的输出是:

-<Check><CheckAmount>1</CheckAmount><FrontImage>1</FrontImage><RearImage>1</RearImage></Check>-<Check><CheckAmount>2</CheckAmount><FrontImage>2</FrontImage><RearImage>2</RearImage></Check>

1 个答案:

答案 0 :(得分:1)

如果我正确猜测(!),您的输入看起来像这样:

<ChqDepAppSrvChequeScanComplete>
    <CourtesyAmount1>100</CourtesyAmount1>
    <FrontImageFilename1 t="String">R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImageFilename1>
    <RearImageFilename1 t="String">R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImageFilename1>
    <RefuseReason1 t="String">None</RefuseReason1>

    <CourtesyAmount2>200</CourtesyAmount2>
    <FrontImageFilename2 t="String">R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImageFilename2>
    <RearImageFilename2 t="String">R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImageFilename2>
    <RefuseReason2 t="String">None</RefuseReason2>

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

你的错误在于你构建这个表达式的方式:

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

您的输入中没有<CourtesyAmount>节点,因此您将连接任何内容和$ i,从而产生$ i的当前值。

您真正想要做的是从名称字符串“CourtesyAmount”和$ i的当前值的串联的节点获取值。类似的东西:

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>

当应用于上述输入时结果

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <Check>
      <CheckAmount>100</CheckAmount>
      <FrontImage>R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImage>
      <RearImage>R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImage>
   </Check>
   <Check>
      <CheckAmount>200</CheckAmount>
      <FrontImage>R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImage>
      <RearImage>R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImage>
   </Check>
</output>

注意

这里的真正的问题是输入。它不应该以这种方式格式化。如果前面的人做了适当的工作并为您提供了结构良好的XML,事情就会容易得多,例如:

<ChqDepAppSrvChequeScanComplete>
    <NumberOfCheques t="Int32">2</NumberOfCheques>
    <Cheque>
        <CourtesyAmount>100</CourtesyAmount>
        <FrontImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImageFilename>
        <RearImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImageFilename>
        <RefuseReason t="String">None</RefuseReason>
    </Cheque>
    <Cheque>
        <CourtesyAmount>200</CourtesyAmount>
        <FrontImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImageFilename>
        <RearImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImageFilename>
        <RefuseReason t="String">None</RefuseReason>
    </Cheque>
</ChqDepAppSrvChequeScanComplete>

没有这个编号的节点是无意义的。