Answered by:
XSLT issue

Question
-
Hi,
I am writing Inline xslt for a map.The thing is i have two Record nodes on source schema Record 1 and Record 2.Now i am writing my xslt for Record2.I am looping Record 2 using for-each.I am performing various condition checks inside the xslt on the elements which belong to Record 2.And its working fine for that part.But the thing is I need to perform condition checks on the elements which belong to Record 1 under the loop of Record2.Is it possible to do that way for elements which belong to Record1.I tried to do it but the output is having the same values for that field.That field has multiple values in the incoming file.How can i loop that single element which belongs to a separate node inside the loop of another node and i also tried to write the xpath pointing to that element but its just returning a single value.When i tried to write xslt for that element by looping it separately then it generated multiple values for that element,when i place that code under Record 2 loop its just returning a single value.Is there any way to work around this scenario.
Thanks
- Edited by BTS_LEARNER Saturday, November 26, 2011 6:42 PM
Saturday, November 26, 2011 6:42 PM
Answers
-
Is the output the desired output, or the "wrong" output?
I still think preceding-sibling should work. Can you show me the XSLT and how you want the output to appear?
Morten la Cour
- Proposed as answer by Steef-Jan WiggersMVP, Moderator Sunday, January 8, 2012 3:13 PM
- Marked as answer by Steef-Jan WiggersMVP, Moderator Sunday, January 8, 2012 3:13 PM
Sunday, November 27, 2011 7:39 PM
All replies
-
I have made a small example, don't know if this is what you are looking for:
Input:
<?xml version="1.0" encoding="utf-16"?>
<ns0:Records xmlns:ns0="myNamespace">
<Record1>
<Item1>hello1</Item1>
<Item2>hey1</Item2>
<Show>1</Show>
<Display>1</Display>
</Record1>
<Record1>
<Item1>hello2</Item1>
<Item2>hey2</Item2>
<Show>0</Show>
<Display>0</Display>
</Record1>
<Record1>
<Item1>hello3</Item1>
<Item2>hey3</Item2>
<Show>0</Show>
<Display>0</Display>
</Record1>
<Record2>
<Item1>hello1</Item1>
<Item2>hey1</Item2>
</Record2>
<Record2>
<Item1>hello2</Item1>
<Item2>hey2</Item2>
</Record2>
<Record2>
<Item1>hello3</Item1>
<Item2>hey3</Item2>
</Record2>
</ns0:Records>
XSLT:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:source="myNamespace" xmlns:dest="myNamespace2">
<xsl:template match="source:Records">
<dest:Records>
<xsl:for-each select="Record2">
<Record>
<!--Test if the FIRST Record1 has Display set to 1 -->
<xsl:if test="//Record1[1]/Display = '1'">
<Item1>
<xsl:value-of select="Item1" />
</Item1>
</xsl:if>
<!-- Test if the Record1 with the SAME position as the Record2 we are currently at has Show set to 1 -->
<xsl:variable name="pos" select="position()" />
<xsl:if test="//Record1[$pos]/Show = '1'">
<Item2>
<xsl:value-of select="Item2" />
</Item2>
</xsl:if>
</Record>
</xsl:for-each>
</dest:Records>
</xsl:template>
</xsl:stylesheet>
Morten la Cour
Sunday, November 27, 2011 9:06 AM -
The thing is i am having a source schema this way morten
Account
Record_1
Bank_Number
Acct_Number
Location
Record_2
Name
City
State
Check_info
and so on...
Account is the rootnode
Record 1 and Record 2 are two different nodes
I am looping record 2 as <xsl-foreach select="//Record_2">
Under this loop i am writing my code for the required condition checks
Now the thing is i need to perform condition check on one the Record 1elements and insert the values into the destination loop
Condition is something like this if the Acct_Number element in the Record_1 is not equal to null
then the element in the destination should be set to the value of Acct_Number.
I tried to use the xpath and the things you mentioned but its not working.Acct_Number will have multiple values in the incoming file.
when i write xpath its just picking the firstvalue in the incoming file.
Please help.Thankss
- Edited by BTS_LEARNER Sunday, November 27, 2011 3:32 PM
Sunday, November 27, 2011 3:30 PM -
Is the structure like this then:Account-Record1(1)-Record2(1)-Record1(2)-Record2(2)And the Record2 needs to access the previous Record1? If this is not the structure, please provide an example with more than one Record1 and Record2In that case use preceding-sibling like this<xsl:if test="string-length(normalize-space(preceding-sibling::Record_1[1]/Acct_Number)) != 0"><destfield><xsl:value-of select="preceding-sibling::Record_1[1]/Acct_Number" /></destfield></xsl:if>Morten la CourSunday, November 27, 2011 4:39 PM
-
No i have only two records morten
ROOTNODE
-RECORD_1-RECORD_2
I dont have more than one RECORD_1 under the rootnode its just two record RECORD1 and RECORD 2 and i need to use the
value of an element which is under RECORD1 in the xslt loop of RECORD2
sriSunday, November 27, 2011 5:56 PM -
Morten even with the preceding sibling its returning only one value for all ACCT_ NUmber elements.My structure is some thing like this
ROOTNODE
-RECORD_1
-ACCT_NUM
-RECORD_2
so Record_1 has the acct number but my xslt is for RECORD 2 where i am doing a loop for that but i need to the element which
is under Record1 under the loop of Record 2 is it possible to do that.I used all the syntax which you provided but its returning only
one value for all the output elements to which ACCT_NUM is mapped.ACCT_NUM has multiple values in the incoming file and i think
that should be looped but i am not able to do a loop for one record under the loop of another record.My incoming file is a flatfile
which i am generating xml
sriSunday, November 27, 2011 6:15 PM -
Do you mean
RN
-R1
-ACCT_NUM
-ACCT_NUM
-ACCT_NUM
-R2
???
If so do this
<!--Inside the R2 element -->
<xsl:for-each select="//R1/ACCT_NUM">
</xsl:for-each>
If this is not what you mean, please provide a full example and with the desired output.
Morten la Cour
Sunday, November 27, 2011 6:24 PM -
Is the output the desired output, or the "wrong" output?
I still think preceding-sibling should work. Can you show me the XSLT and how you want the output to appear?
Morten la Cour
- Proposed as answer by Steef-Jan WiggersMVP, Moderator Sunday, January 8, 2012 3:13 PM
- Marked as answer by Steef-Jan WiggersMVP, Moderator Sunday, January 8, 2012 3:13 PM
Sunday, November 27, 2011 7:39 PM -
Also what did you mean by "I dont have more than one RECORD_1 under the rootnode its just two record RECORD1 and RECORD 2" in the example above I see 2 of each?
Morten la Cour
Sunday, November 27, 2011 7:44 PM