zyberjock
zyberjock

Reputation: 347

Looping thru XSLT

I have this Input XML:

<?xml version='1.0' encoding='UTF-8'?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn-sample">
  <CustomerRecord>
    <Statement>
      <StmtId>123</StmtId>
      <StmtDate>2013-08-16</StmtDate>
      <AcctNumber>123456789</AcctNumber>
      <Balance>
        <Type>OP</Type>
        <Amount>1.00</Amount>
        <CreditOrDebit>DR</CreditOrDebit>
        <Date>2013-08-15</Date>
      </Balance>
      <Balance>
        <Type>CL</Type>
        <Amount>2.00</Amount>
        <CreditOrDebit>CR</CreditOrDebit>
        <Date>2013-08-16</Date>
      </Balance>
      <Balance>
        <Type>FW</Type>
        <Amount>3.00</Amount>
        <CreditOrDebit>CR</CreditOrDebit>
        <Date>2013-08-17</Date>
      </Balance>
      <Entry>
        <Amount>7778.70</Amount>
        <CreditOrDebit>DR</CreditOrDebit>
        <EntryDtls>
          <TransactionDetails>
            <Parties>
              <Customer>
                <Name>Customer 1 Name</Name>
                <Address>Address Line 1</Address>
              </Customer>
              <CustomerAcct>
                <AcctName>Account Name 1</AcctName>
                <AcctNumber>12345677</AcctNumber>
              </CustomerAcct>
            </Parties>
            <AddlInfo>
              <Info1>Info 1</Info1>
              <Info1>Info 2</Info1>
              <Info1>Info 3</Info1>
            </AddlInfo>
          </TransactionDetails>
        </EntryDtls>
      </Entry>
      <Entry>
        <Amount>5555.70</Amount>
        <CreditOrDebit>DR</CreditOrDebit>
        <EntryDtls>
          <TransactionDetails>
            <Parties>
              <Customer>
                <Name>Customer 2 Name</Name>
                <Address>Address Line 2</Address>
              </Customer>
              <CustomerAcct>
                <AcctName>Account Name 2</AcctName>
                <AcctNumber>12345678</AcctNumber>
              </CustomerAcct>
            </Parties>
            <AddlInfo>
              <Info1>Info 1</Info1>
              <Info1>Info 2</Info1>
            </AddlInfo>
          </TransactionDetails>
        </EntryDtls>
      </Entry>
    </Statement>
    <Statement>
      <StmtId>124</StmtId>
      <StmtDate>2013-08-18</StmtDate>
      <AcctNumber>123456780</AcctNumber>
      <Balance>
        <Type>OP</Type>
        <Amount>4.00</Amount>
        <CreditOrDebit>CR</CreditOrDebit>
        <Date>2013-08-15</Date>
      </Balance>
      <Balance>
        <Type>CL</Type>
        <Amount>5.00</Amount>
        <CreditOrDebit>DR</CreditOrDebit>
        <Date>2013-08-16</Date>
      </Balance>
      <Balance>
        <Type>FW</Type>
        <Amount>6.00</Amount>
        <CreditOrDebit>CR</CreditOrDebit>
        <Date>2013-08-17</Date>
      </Balance>
      <Entry>
        <Amount>7777.70</Amount>
        <CreditOrDebit>DR</CreditOrDebit>
        <EntryDtls>
          <TransactionDetails>
            <Parties>
              <Customer>
                <Name>Customer 3 Name</Name>
                <Address>Address Line 3</Address>
              </Customer>
              <CustomerAcct>
                <AcctName>Account Name 3</AcctName>
                <AcctNumber>12345679</AcctNumber>
              </CustomerAcct>
            </Parties>
            <AddlInfo>
              <Info1>Info 1</Info1>
              <Info1>Info 2</Info1>
              <Info1>Info 3</Info1>
            </AddlInfo>
          </TransactionDetails>
        </EntryDtls>
      </Entry>
    </Statement>
  </CustomerRecord>
</Document>

Then I have this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:urn="urn-sample">
  <xsl:strip-space elements="*" />
  <xsl:output indent="yes" omit-xml-declaration="yes" />

  <xsl:template match="/urn:Document">
    <Root>
      <xsl:apply-templates select="/urn:Document/urn:CustomerRecord/urn:Statement" />
    </Root>
  </xsl:template>

  <xsl:template match="/urn:Document/urn:CustomerRecord/urn:Statement">
    <CStatement>
      <CStatementId>
        <xsl:value-of select="/urn:Document/urn:CustomerRecord/urn:Statement/urn:StmtId" />
      </CStatementId>
      <CStatementDate>
        <xsl:choose>
          <xsl:when test ="/urn:Document/urn:CustomerRecord/urn:Statement/urn:StmtDate!=''">
            <xsl:value-of select="/urn:Document/urn:CustomerRecord/urn:Statement/urn:StmtDate" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Balance[contains('|CL|FW|', concat('|', urn:Type, '|'))]/urn:Date" />
          </xsl:otherwise>
        </xsl:choose>
      </CStatementDate>
      <AccountNumber>
        <xsl:if test="/urn:Document/urn:CustomerRecord/urn:Statement/urn:AcctNumber!=''">
          <xsl:value-of select ="/urn:Document/urn:CustomerRecord/urn:Statement/urn:AcctNumber"/>
        </xsl:if>
      </AccountNumber>
      <OpeningBalance>
        <xsl:choose>
          <xsl:when test="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Balance[contains('|OP|PR|', concat('|', urn:Type, '|'))]/urn:CdtDbtInd='DR'">
            <xsl:value-of select="format-number(/urn:Document/urn:CustomerRecord/urn:Statement/urn:Balance[contains('|OP|PR|', concat('|', urn:Type, '|'))]/urn:Amount * -1, '#.00')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(/urn:Document/urn:CustomerRecord/urn:Statement/urn:Balance[contains('|OP|PR|', concat('|', urn:Type, '|'))]/urn:Amount, '#.00')" />
          </xsl:otherwise>
        </xsl:choose>
      </OpeningBalance>
      <ClosingBalance>
        <xsl:choose>
          <xsl:when test="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Balance[contains('|CL|FW|', concat('|', urn:Type, '|'))]/urn:CdtDbtInd='DR'">
            <xsl:value-of select="format-number(/urn:Document/urn:CustomerRecord/urn:Statement/urn:Balance[contains('|CL|FW|', concat('|', urn:Type, '|'))]/urn:Amount * -1, '#.00')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(/urn:Document/urn:CustomerRecord/urn:Statement/urn:Balance[contains('|CL|FW|', concat('|', urn:Type, '|'))]/urn:Amount, '#.00')" />
          </xsl:otherwise>
        </xsl:choose>
      </ClosingBalance>
      <xsl:apply-templates select="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Entry" />
    </CStatement>
  </xsl:template>

  <xsl:template match="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Entry">
    <StatementLine>
      <DebitOrCredit>
        <xsl:value-of select ="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Entry/urn:CreditOrDebit"/>
      </DebitOrCredit>
      <Amount>
        <xsl:choose>
          <xsl:when test="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Entry/urn:CreditOrDebit='DR'">
            <xsl:value-of select="format-number(/urn:Document/urn:CustomerRecord/urn:Statement/urn:Entry/urn:Amount * -1, '#.00')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="format-number(/urn:Document/urn:CustomerRecord/urn:Statement/urn:Entry/urn:Amount, '#.00')" />
          </xsl:otherwise>
        </xsl:choose>
      </Amount>
      <CustomerName>
        <xsl:value-of select="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Entry/urn:EntryDtls/urn:TransactionDetails/urn:Parties/urn:Customer/urn:Name" />
        <!--NtryDtls/TransactionDetails/Parties/Dbtr/Nm-->
      </CustomerName>
      <CustomerBankAccount>
        <xsl:value-of select="/urn:Document/urn:CustomerRecord/urn:Statement/urn:Entry/urn:EntryDtls/urn:TransactionDetails/urn:Parties/urn:CustomerAcct/urn:AcctNumber" />
      </CustomerBankAccount>
      <Description>


      </Description>
    </StatementLine>
  </xsl:template>
</xsl:stylesheet>

That should have this output:

<?xml version="1.0" encoding="utf-8"?>
<Root> <!--should have no attribute-->
  <CStatement>
    <CStatementId>123</CStatementId>
    <CStatementDate>2013-08-16</CStatementDate>
    <AccountNumber>123456789</AccountNumber>
    <OpeningBalance>-1.00</OpeningBalance>
    <ClosingBalance>2.00</ClosingBalance>
    <CStatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>-7778.70</Amount>
      <CustomerName>Customer 1 Name</CustomerName>
      <CustomerBankAccount>12345677</CustomerBankAccount>
      <Description></Description>
    </CStatementLine>
    <StatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>5555.70</Amount>
      <CustomerName>Customer 2 Name</CustomerName>
      <CustomerBankAccount>12345678</CustomerBankAccount>
      <Description></Description>
    </StatementLine>
  </CStatement>
  <CStatement>
    <CStatementId>124</CStatementId>
    <CStatementDate>2013-08-18</CStatementDate>
    <AccountNumber>123456780</AccountNumber>
    <OpeningBalance>4.00</OpeningBalance>
    <ClosingBalance>-5.00</ClosingBalance>
    <CStatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>7777.70</Amount>
      <CustomerName>Customer 3 Name</CustomerName>
      <CustomerBankAccount>12345679</CustomerBankAccount>
      <Description></Description>
    </CStatementLine>
  </CStatement>
</Root>

I have two problem with my XSLT: 1 It is only getting the first value of Statement or StatementLines like this:

<Root xmlns:urn="urn-sample">
  <CStatement>
    <CStatementId>123</CStatementId>
    <CStatementDate>2013-08-16</CStatementDate>
    <AccountNumber>123456789</AccountNumber>
    <OpeningBalance>1.00</OpeningBalance>
    <ClosingBalance>2.00</ClosingBalance>
    <StatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>-7778.70</Amount>
      <CustomerName>Customer 1 Name</CustomerName>
      <CustomerBankAccount>12345677</CustomerBankAccount>
      <Description />
    </StatementLine>
    <StatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>-7778.70</Amount>
      <CustomerName>Customer 1 Name</CustomerName>
      <CustomerBankAccount>12345677</CustomerBankAccount>
      <Description />
    </StatementLine>
    <StatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>-7778.70</Amount>
      <CustomerName>Customer 1 Name</CustomerName>
      <CustomerBankAccount>12345677</CustomerBankAccount>
      <Description />
    </StatementLine>
  </CStatement>
  <CStatement>
    <CStatementId>123</CStatementId>
    <CStatementDate>2013-08-16</CStatementDate>
    <AccountNumber>123456789</AccountNumber>
    <OpeningBalance>1.00</OpeningBalance>
    <ClosingBalance>2.00</ClosingBalance>
    <StatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>-7778.70</Amount>
      <CustomerName>Customer 1 Name</CustomerName>
      <CustomerBankAccount>12345677</CustomerBankAccount>
      <Description />
    </StatementLine>
    <StatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>-7778.70</Amount>
      <CustomerName>Customer 1 Name</CustomerName>
      <CustomerBankAccount>12345677</CustomerBankAccount>
      <Description />
    </StatementLine>
    <StatementLine>
      <DebitOrCredit>DR</DebitOrCredit>
      <Amount>-7778.70</Amount>
      <CustomerName>Customer 1 Name</CustomerName>
      <CustomerBankAccount>12345677</CustomerBankAccount>
      <Description />
    </StatementLine>
  </CStatement>
</Root>

2 And lastly, not sure how I can use variable or any function to store the value of Description tag with this requirements:

I hope someone can help me with this. Thanks everyone

BTW I need it on XSLT 1.0

Upvotes: 0

Views: 64

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117100

Your basic mistake is that you are using absolute paths instead of relative ones. Compare the results of these two minimized stylesheets:

XSLT 1.0 (absolute)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:urn="urn-sample">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" omit-xml-declaration="yes" />

<xsl:template match="/urn:Document">
    <Root>
        <xsl:apply-templates select="/urn:Document/urn:CustomerRecord/urn:Statement" />
    </Root>
</xsl:template>

<xsl:template match="/urn:Document/urn:CustomerRecord/urn:Statement">
    <CStatement>
        <CStatementId>
            <xsl:value-of select="/urn:Document/urn:CustomerRecord/urn:Statement/urn:StmtId" />
        </CStatementId>
    </CStatement>
</xsl:template>

</xsl:stylesheet>

XSLT 1.0 (relative)

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:urn="urn-sample"
exclude-result-prefixes="urn">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="/urn:Document">
    <Root>
        <xsl:apply-templates select="urn:CustomerRecord/urn:Statement" />
    </Root>
</xsl:template>

<xsl:template match="urn:Statement">
    <CStatement>
        <CStatementId>
            <xsl:value-of select="urn:StmtId" />
        </CStatementId>
    </CStatement>
</xsl:template>

</xsl:stylesheet>

In the first example, the instruction:

<xsl:value-of select="/urn:Document/urn:CustomerRecord/urn:Statement/urn:StmtId" />

starts from the root and always gets the Id value of the first statement, no matter which statement is being currently processed by the loop.

Upvotes: 3

Related Questions