aspose file tools*
The moose likes XML and Related Technologies and the fly likes XSLT template use inside a for-each cursor to map the last element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSLT template use inside a for-each cursor to map the last element" Watch "XSLT template use inside a for-each cursor to map the last element" New topic
Author

XSLT template use inside a for-each cursor to map the last element

Raj Ms
Greenhorn

Joined: Mar 18, 2014
Posts: 6
Following is the sample file that is have:

Input:
-----------------------------------
<ElementOne>
<RecordA>AAA</RecordA>
</ElementOne>
<ElementOne>
<RecordA>BBB</RecordA>
</ElementOne>
<ElementOne>
<RecordA>CCC</RecordA>
</ElementOne>
<ElementTwo>
<DfiAccttNumber>123</DfiAccttNumber>
</ElementTwo>
<ElementTwo>
<DfiAccttNumber>9999</DfiAccttNumber>
</ElementTwo>
<ElementTwo>
<DfiAccttNumber>8889</DfiAccttNumber>
</ElementTwo>
<ElementTwo>
<DfiAccttNumber>0000</DfiAccttNumber>
</ElementTwo>
<ElementTwo>
<DfiAccttNumber>9696</DfiAccttNumber>
</ElementTwo>

XSD:
-----------
<xsd:element name="HeaderElement">
<xsd:element name="ElementOne" nxsd:style="array" nxsd:cellSeparatedBy="${eol}" nxsd:conditionValue="1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RecordA" type="xsd:string" nxsd:style="fixedLength" nxsd:length="2" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ElementTwo" nxsd:style="array" nxsd:cellSeparatedBy="${eol}" nxsd:conditionValue="6">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RecordB" type="xsd:string" nxsd:style="fixedLength" nxsd:length="17" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:element>


XSLT:
--------
<xsl:template match="/">
<xsl:variable name="cur"/>
<ebo:CreateReturnedItemListEBM>
<DataArea>
<xsl:for-each select="/imp1:HeaderElement/imp1:ElementTwo">
<xsl:variable name="cur" select="position()"/>
<ebo:CreateReturnedItemList>
<ebo:BankDetail>
<ebo:AccountNumber>
<ebo:Old>
<xsl:value-of select="/imp1:HeaderElement/imp1:ElementTwo[$cur]/imp1:RecordB"/>
</ebo:Old>
</ebo:AccountNumber>
</ebo:BankDetail>
<ebo:Payment>
<ebo:Bate>
<xsl:value-of select="../imp1:ElementOne[$cur]/imp1:RecordA"/>
</ebo:Bate>
</ebo:Payment>
</ebo:CreateReturnedItemList>
</xsl:for-each>
</DataArea>
</ebo:CreateReturnedItemListEBM>

Response:
-----------

<ebo:CreateReturnedItemList>
<ebo:BankDetail>
<ebo:AccountNumber>
<ebo:Old>123</ebo:Old>
</ebo:AccountNumber>
</ebo:BankDetail>
<ebo:Payment>
<ebo:Bate>AAA</ebo:Bate>
</ebo:Payment>
</ebo:CreateReturnedItemList>
<ebo:CreateReturnedItemList>
<ebo:BankDetail>
<ebo:AccountNumber>
<ebo:Old>9999</ebo:Old>
</ebo:AccountNumber>
</ebo:BankDetail>
<ebo:Payment>
<ebo:Bate>BBB</ebo:Bate>
</ebo:Payment>
</ebo:CreateReturnedItemList>
<ebo:CreateReturnedItemList>
<ebo:BankDetail>
<ebo:AccountNumber>
<ebo:Old>8889</ebo:Old>
</ebo:AccountNumber>
</ebo:BankDetail>
<ebo:Payment>
<ebo:Bate>CCC</ebo:Bate>
</ebo:Payment>
</ebo:CreateReturnedItemList>
<ebo:CreateReturnedItemList>
<ebo:BankDetail>
<ebo:AccountNumber>
<ebo:Old>0000</ebo:Old>
</ebo:AccountNumber>
</ebo:BankDetail>
<ebo:Payment>
<ebo:Bate/>
</ebo:Payment>
</ebo:CreateReturnedItemList>
<ebo:CreateReturnedItemList>
<ebo:BankDetail>
<ebo:AccountNumber>
<ebo:Old>9696</ebo:Old>
</ebo:AccountNumber>
</ebo:BankDetail>
<ebo:Payment>
<ebo:Bate/> -- Here the last 'Bate' element is getting empty because of the cusor is ended. I need to apply the last value of 'RecordA' to this Bate when this comes as empty. If possible with a template
</ebo:Payment>
</ebo:CreateReturnedItemList>
</DataArea>
</ebo:CreateReturnedItemListEBM>
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 544
    
    3
Let's see if you understand what you need: you mean leaving the last-but-one ebo:Bate empty is ok, leaving the last ebo:Bate is not and you want it to fill up with the last of imp1:RecordA. The generic reason of them being empty is due to the mismatch in the counts of ElementOne and ElementTwo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSLT template use inside a for-each cursor to map the last element