2016-12-29 5 views
1

以下は私のXMLの外観です。私はXSLTをコピーして出力しました。私の問題は、出力は常に同じ最初の行を繰り返すことです。 XMLファイルを入力する行を追加すると、最初の行は追加された行の数が多いほど出力ファイルで繰り返されます。理由は何でしょうか?<xsl:for-each>は最初の行を繰り返す

XML:

<Loans> 
    <Loan> 
     <loan_number>123</loan_number> 
     <loan_aqn_date>08-01-2016</loan_number> 
    </Loan> 
    <Loan> 
     <loan_number>456</loan_number> 
     <loan_aqn_date>10-01-2016</loan_number> 
    </Loan> 
    <Loan> 
     <loan_number>789</loan_number> 
     <loan_aqn_date>12-01-2016</loan_number> 
    </Loan> 
</Loans> 

出力:

loan_number|loan_aqn_date| 
123|08-01-2016| 
123|08-01-2016| 
123|08-01-2016| 

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" /> 
    <xsl:template match="/"> 
     <xsl:text>loan_number|loan_aqn_date|</xsl:text> 
     <xsl:for-each select="/Loans/Loan"> 
      <xsl:value-of select="concat(/Loans/Loan/loan_number,'|')" /> 
      <xsl:value-of select="concat(/Loans/Loan/loan_aqn_date,'|')" /> 
     </xsl:for-each> 
    </xsl:template>  
</xsl:stylesheet> 
+0

クイック補正:私は上記の投稿で、 "" 閉じ逃しました。私は正しく ""と私のxmlファイルに入れています – Pushpa

+0

私はこれを編集で修正しました。 – zx485

+0

'' 12-01-2016' 'の開始タグと終了タグの間には依然として不一致があります。 – zx485

答えて

1

あなたがループ内で "選択" のための絶対パスを使用しています。これを試してみてください :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" /> 
    <xsl:template match="/"> 
     <xsl:text>loan_number|loan_aqn_date|</xsl:text> 
     <xsl:for-each select="/Loans/Loan"> 
      <xsl:value-of select="concat(loan_number,'|')" /> 
      <xsl:value-of select="concat(loan_aqn_date,'|')" /> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 
+0

絶対パスが問題でした。私はそれを削除し、属性名だけを保持しました。それは現在、それぞれのために完璧に動作しています。おかげさまでラファエル・Z。 – Pushpa

0

あなたのアプローチは、より一般的にすることができ、テンプレートと<for-each>を交換します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" /> 
    <xsl:strip-space elements="Loans" />  <!-- Removes surrounding spaces --> 

    <xsl:template match="/Loans"> 
    <xsl:text>loan_number|loan_aqn_date|&#10;</xsl:text> 
    <xsl:apply-templates />    <!-- Processes 'Loan' nodes --> 
    </xsl:template>  

    <xsl:template match="Loan"> 
    <xsl:value-of select="concat(loan_number, '|', loan_aqn_date,'|')" /> 
    <xsl:text>&#10;</xsl:text>    <!-- Adds newlines --> 
    </xsl:template>  
</xsl:stylesheet> 

出力:

loan_number|loan_aqn_date| 
123|08-01-2016| 
456|10-01-2016| 
789|12-01-2016| 
関連する問題