Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 1st, 2010, 10:29 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default Using position()

Hi frns,

I am first time using the function position().Please help me in resolving this.
input xml:
<LMPRequests>
<LMPRequest>
<LMPMessage>Details
<Section>Criteria
<Row>
<REGION_CODE>524002</REGION_CODE>
<STATUS>Y</STATUS>
<FUND_AMOUNT1>25.00</FUND_AMOUNT1>
<FUND_AMOUNT2>125.00</FUND_AMOUNT2>
<FUND_AMOUNT3>125.00</FUND_AMOUNT3>
<FUND_CODE1>A123</FUND_CODE1>
<FUND_CODE2>CB12</FUND_CODE2>
<FUND_CODE3>jk12</FUND_CODE3>
<UNIT_TYPE1>A</UNIT_TYPE1>
<UNIT_TYPE2>D</UNIT_TYPE2> <UNIT_TYPE3>C</UNIT_TYPE3>
</Row>
</Section>
</LMPMessage>
</LMPRequest>
</LMPRequests>


outputxml :
<?xml version="1.0" encoding="UTF-8"?>
<CreateClient xmlns="http://ufus.com/us/webservices">
<criteria>
<regioncode>524002</regioncode>
<Status>Y</Status>
<Assets>
<RegularInvestmentAsset>
<FUND_CODE>A123</FUND_CODE>
<UNIT_TYPE>A</UNIT_TYPE>
<FUND_AMOUNT>25.00</FUND_AMOUNT>
</RegularInvestmentAsset>
<RegularInvestmentAsset>
<FUND_CODE>CB12</FUND_CODE>
<UNIT_TYPE>D</UNIT_TYPE>
<FUND_AMOUNT>125.00</FUND_AMOUNT>
</RegularInvestmentAsset>
<RegularInvestmentAsset>
<FUND_CODE>jk12</FUND_CODE>
<UNIT_TYPE>C</UNIT_TYPE>
<FUND_AMOUNT>125.00</FUND_AMOUNT>
</RegularInvestmentAsset>
</Assets>
</criteria>
</CreateClient>
I used the following xslt :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://ufus.com/us/webservices" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<CreateClient>
<criteria>
<regioncode>524002</regioncode>
<Status>Y</Status>
<Assets>
<xsl:for-each select="descendant::*[starts-with(local-name(), 'FUND_AMOUNT')]">
<xsl:variable name="pos" select="position()"/>
<RegularInvestmentAsset>
<FUND_CODE><xsl:value-of select="../*[local-name() = concat('FUND_CODE', $pos)]"/></FUND_CODE>
<UNIT_TYPE><xsl:value-of select="../*[local-name() = concat('UNIT_TYPE', $pos)]"/></UNIT_TYPE>
<FUND_AMOUNT><xsl:value-of select="../*[local-name() = concat('FUND_AMOUNT', $pos)]"/></FUND_AMOUNT>
</RegularInvestmentAsset>
</xsl:for-each>
</Assets>
</criteria>
</CreateClient>
</xsl:template>
</xsl:stylesheet>

It worked well when i give the above input xml but when i give the input xml as below i.e starting with FUND_AMOUNT3 with out FUND_AMOUNT1 and FUND_AMOUNT2 , fund amount is not populating . i am attaching output xml also

<LMPRequests>
<LMPRequest>
<LMPMessage>Details
<Section>Criteria
<Row>
<REGION_CODE>524002</REGION_CODE>
<STATUS>Y</STATUS>
<FUND_AMOUNT3>125.00</FUND_AMOUNT3>
<FUND_CODE3>jk12</FUND_CODE3>
<UNIT_TYPE3>C</UNIT_TYPE3>
</Row>
</Section>
</LMPMessage>
</LMPRequest>
</LMPRequests>


output xml :
<?xml version="1.0" encoding="UTF-8"?>
<CreateClient xmlns="http://ufus.com/us/webservices">
<criteria>
<regioncode>524002</regioncode>
<Status>Y</Status>
<Assets>
<RegularInvestmentAsset>
<FUND_CODE></FUND_CODE>
<UNIT_TYPE></UNIT_TYPE>
<FUND_AMOUNT></FUND_AMOUNT>
</RegularInvestmentAsset>
</Assets>
</criteria>
</CreateClient>

CAn any one please help me in fixing the above problem.
 
Old September 1st, 2010, 10:40 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

position() returns the position, simple as that. In your first example it will return 1, then 2, then 3. In your final example it will return 1. There is no element called FUND_CODE1 so no value is returned.

Looks like you need to use substring_after to get the number part of the FUND_AMOUNTX and the concat that instead of using position().
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old September 1st, 2010, 10:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As Sam says - position() isn't going to help you unless the numbers run contiguously 1,2,3...

Try replacing

Code:
<xsl:variable name="pos" select="position()"/>
with

Code:
<xsl:variable name="pos" select="substring-after(local-name(), 'FUND_AMOUNT')"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
Divya (September 1st, 2010)
 
Old September 1st, 2010, 11:31 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks Sam and mhkay.
It got the expected output.
Thank you so much





Similar Threads
Thread Thread Starter Forum Replies Last Post
using position() Divya XSLT 2 April 27th, 2010 12:05 PM
POSITION() pallone XSLT 8 June 3rd, 2009 10:04 AM
Position barski XSLT 5 July 11th, 2007 01:54 PM
position:absolute nerssi CSS Cascading Style Sheets 3 February 21st, 2005 10:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.