XSL: Handling Nested for-each loop Query


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: XSL: Handling Nested for-each loop Query

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Posts
    2

    XSL: Handling Nested for-each loop Query

    From the XML & XSL below, I just want to be able to create a list of all the items (as described below). However, I am having difficulty easily and efficiently producing the output I want without duplication.

    Basically, for each <Index> I want to report all it's child elements & attributes in a given format, including concatenating some of them together.

    Any help would be very much appreciated.

    XML:
    Code:
    <Config>
    <!--Indexes-->
    <Indexes RangeName="LookupIndexTradeIDs">
    	<!--CDX-->
    	<Index Name="cdx">
    		<Series>
    			<SeriesNumber>9</SeriesNumber>
    		</Series>
    		<Issuances>
    			<Maturity OldID="d10" NewID="3Y"/>
    			<Maturity OldID="d12" NewID="5Y"/>
    			<Maturity OldID="d14" NewID="7Y"/>
    			<Maturity OldID="d17" NewID="10Y"/>
    		</Issuances>
    		<Tranches>
    			<Tranche Attach="0" Detach="3" NonSS="True" ThirtyTop="False" SixtyTop="False"/>
    			<Tranche Attach="3" Detach="7" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="7" Detach="10" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="10" Detach="15" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="15" Detach="30" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="30" Detach="60" NonSS="False" ThirtyTop="False" SixtyTop="True"/>
    			<Tranche Attach="30" Detach="100" NonSS="False" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="60" Detach="100" NonSS="False" ThirtyTop="False" SixtyTop="True"/>
    		</Tranches>
    	</Index>
    	<!--HY-->
    	<Index Name="HY">
    		<Series>
    			<SeriesNumber>10</SeriesNumber>
    		</Series>
    		<Issuances>
    			<Maturity OldID="j10" NewID="3Y"/>
    			<Maturity OldID="j12" NewID="5Y"/>
    			<Maturity OldID="j14" NewID="7Y"/>
    		</Issuances>
    		<Tranches>
    			<Tranche Attach="0" Detach="10" NonSS="True" ThirtyTop="False" SixtyTop="False"/>
    			<Tranche Attach="10" Detach="15" NonSS="True" ThirtyTop="False" SixtyTop="False"/>
    			<Tranche Attach="15" Detach="25" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="25" Detach="35" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="35" Detach="100" NonSS="False" ThirtyTop="True" SixtyTop="True"/>
    		</Tranches>
    	</Index>
    	<!--iTraxx-->
    	<Index Name="itraxx">
    		<Series>
    			<SeriesNumber>9</SeriesNumber>
    		</Series>
    		<Issuances>
    			<Maturity OldID="j11" NewID="3Y"/>
    			<Maturity OldID="j13" NewID="5Y"/>
    			<Maturity OldID="j15" NewID="7Y"/>
    			<Maturity OldID="j18" NewID="10Y"/>
    		</Issuances>
    		<Tranches>
    			<Tranche Attach="0" Detach="3" NonSS="True" ThirtyTop="False" SixtyTop="False"/>
    			<Tranche Attach="3" Detach="6" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="6" Detach="9" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="9" Detach="12" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="12" Detach="22" NonSS="True" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="22" Detach="60" NonSS="False" ThirtyTop="False" SixtyTop="True"/>
    			<Tranche Attach="22" Detach="100" NonSS="False" ThirtyTop="True" SixtyTop="True"/>
    			<Tranche Attach="60" Detach="100" NonSS="False" ThirtyTop="False" SixtyTop="True"/>
    		</Tranches>
    	</Index>
    </Indexes>
    </Config>
    XSL:
    Code:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
    <xsl:for-each select="//Config/Indexes/Index">
    	<xsl:variable name="indexName" select="@Name"/>
    	<xsl:for-each select="//Config/Indexes/Index/Series/SeriesNumber">
    		<xsl:variable name="seriesNo" select="SeriesNumber"/>
    		<xsl:for-each select="//Config/Indexes/Index/Issuances/Maturity">
    			<xsl:variable name="issueOld" select="@OldID"/>
    			<xsl:variable name="issueNew" select="@NewID"/>
    			<xsl:for-each select="//Config/Indexes/Index/Tranches/Tranche">
    				<xsl:variable name="attachment" select="@Attach"/>
    				<xsl:variable name="detachment" select="@Detach"/>
    				<xsl:variable name="oldTradeID" select="concat($indexName, $seriesNo, '_', $issueOld, '_T', $attachment, '-', $detachment)"/>
    				<xsl:variable name="newTradeID" select="concat($indexName, $seriesNo, '_', $attachment, '_', $detachment, '_', $issueNew)"/>
    				Indexes|<xsl:value-of select="//Config/Indexes/@RangeName"/>|<xsl:value-of select="$oldTradeID"/>|<xsl:value-of select="$newTradeID"/>|<xsl:value-of select="@NonSS"/>|<xsl:value-of select="@ThirtyTop"/>|<xsl:value-of select="@SixtyTop"/>
    			</xsl:for-each>
    		</xsl:for-each>
    	</xsl:for-each>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
    Output Required:
    Code:
    cdx9|cdx9_d10_T0-3|cdx9_0_3_3Y|TRUE|FALSE|FALSE
    cdx9|cdx9_d10_T3-7|cdx9_3_7_3Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d10_T7-10|cdx9_7_10_3Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d10_T10-15|cdx9_10_15_3Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d10_T15-30|cdx9_15_30_3Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d10_T30-60|cdx9_30_60_3Y|FALSE|FALSE|TRUE
    cdx9|cdx9_d10_T30-100|cdx9_30_100_3Y|FALSE|TRUE|TRUE
    cdx9|cdx9_d10_T60-100|cdx9_60_100_3Y|FALSE|FALSE|TRUE
    cdx9|cdx9_d12_T0-3|cdx9_0_3_5Y|TRUE|FALSE|FALSE
    cdx9|cdx9_d12_T3-7|cdx9_3_7_5Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d12_T7-10|cdx9_7_10_5Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d12_T10-15|cdx9_10_15_5Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d12_T15-30|cdx9_15_30_5Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d12_T30-60|cdx9_30_60_5Y|FALSE|FALSE|TRUE
    cdx9|cdx9_d12_T30-100|cdx9_30_100_5Y|FALSE|TRUE|TRUE
    cdx9|cdx9_d12_T60-100|cdx9_60_100_5Y|FALSE|FALSE|TRUE
    cdx9|cdx9_d14_T0-3|cdx9_0_3_7Y|TRUE|FALSE|FALSE
    cdx9|cdx9_d14_T3-7|cdx9_3_7_7Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d14_T7-10|cdx9_7_10_7Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d14_T10-15|cdx9_10_15_7Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d14_T15-30|cdx9_15_30_7Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d14_T30-60|cdx9_30_60_7Y|FALSE|FALSE|TRUE
    cdx9|cdx9_d14_T30-100|cdx9_30_100_7Y|FALSE|TRUE|TRUE
    cdx9|cdx9_d14_T60-100|cdx9_60_100_7Y|FALSE|FALSE|TRUE
    cdx9|cdx9_d17_T0-3|cdx9_0_3_10Y|TRUE|FALSE|FALSE
    cdx9|cdx9_d17_T3-7|cdx9_3_7_10Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d17_T7-10|cdx9_7_10_10Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d17_T10-15|cdx9_10_15_10Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d17_T15-30|cdx9_15_30_10Y|TRUE|TRUE|TRUE
    cdx9|cdx9_d17_T30-60|cdx9_30_60_10Y|FALSE|FALSE|TRUE
    cdx9|cdx9_d17_T30-100|cdx9_30_100_10Y|FALSE|TRUE|TRUE
    cdx9|cdx9_d17_T60-100|cdx9_60_100_10Y|FALSE|FALSE|TRUE
    HY10|hy10_j10_T0-10|hy10_0_10_3Y|TRUE|FALSE|FALSE
    HY10|hy10_j10_T10-15|hy10_10_15_3Y|TRUE|FALSE|FALSE
    HY10|hy10_j10_T15-25|hy10_15_25_3Y|TRUE|TRUE|TRUE
    HY10|hy10_j10_T25-35|hy10_25_35_3Y|TRUE|TRUE|TRUE
    HY10|hy10_j10_T35-100|hy10_35_100_3Y|FALSE|TRUE|TRUE
    HY10|hy10_j12_T0-10|hy10_0_10_5Y|TRUE|FALSE|FALSE
    HY10|hy10_j12_T10-15|hy10_10_15_5Y|TRUE|FALSE|FALSE
    HY10|hy10_j12_T15-25|hy10_15_25_5Y|TRUE|TRUE|TRUE
    HY10|hy10_j12_T25-35|hy10_25_35_5Y|TRUE|TRUE|TRUE
    HY10|hy10_j12_T35-100|hy10_35_100_5Y|FALSE|TRUE|TRUE
    HY10|hy10_j14_T0-10|hy10_0_10_7Y|TRUE|FALSE|FALSE
    HY10|hy10_j14_T10-15|hy10_10_15_7Y|TRUE|FALSE|FALSE
    HY10|hy10_j14_T15-25|hy10_15_25_7Y|TRUE|TRUE|TRUE
    HY10|hy10_j14_T25-35|hy10_25_35_7Y|TRUE|TRUE|TRUE
    HY10|hy10_j14_T35-100|hy10_35_100_7Y|FALSE|TRUE|TRUE
    itraxx9|itraxx9_j11_T0-3|itrx9_0_3_3Y|TRUE|FALSE|FALSE
    itraxx9|itraxx9_j11_T3-6|itrx9_3_6_3Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j11_T6-9|itrx9_6_9_3Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j11_T9-12|itrx9_9_12_3Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j11_T12-22|itrx9_12_22_3Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j11_T22-60|itrx9_22_60_3Y|FALSE|FALSE|TRUE
    itraxx9|itraxx9_j11_T22-100|itrx9_22_100_3Y|FALSE|TRUE|TRUE
    itraxx9|itraxx9_j11_T60-100|itrx9_60_100_3Y|FALSE|FALSE|TRUE
    itraxx9|itraxx9_j13_T0-3|itrx9_0_3_5Y|TRUE|FALSE|FALSE
    itraxx9|itraxx9_j13_T3-6|itrx9_3_6_5Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j13_T6-9|itrx9_6_9_5Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j13_T9-12|itrx9_9_12_5Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j13_T12-22|itrx9_12_22_5Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j13_T22-60|itrx9_22_60_5Y|FALSE|FALSE|TRUE
    itraxx9|itraxx9_j13_T22-100|itrx9_22_100_5Y|FALSE|TRUE|TRUE
    itraxx9|itraxx9_j13_T60-100|itrx9_60_100_5Y|FALSE|FALSE|TRUE
    itraxx9|itraxx9_j15_T0-3|itrx9_0_3_7Y|TRUE|FALSE|FALSE
    itraxx9|itraxx9_j15_T3-6|itrx9_3_6_7Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j15_T6-9|itrx9_6_9_7Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j15_T9-12|itrx9_9_12_7Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j15_T12-22|itrx9_12_22_7Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j15_T22-60|itrx9_22_60_7Y|FALSE|FALSE|TRUE
    itraxx9|itraxx9_j15_T22-100|itrx9_22_100_7Y|FALSE|TRUE|TRUE
    itraxx9|itraxx9_j15_T60-100|itrx9_60_100_7Y|FALSE|FALSE|TRUE
    itraxx9|itraxx9_j18_T0-3|itrx9_0_3_10Y|TRUE|FALSE|FALSE
    itraxx9|itraxx9_j18_T3-6|itrx9_3_6_10Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j18_T6-9|itrx9_6_9_10Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j18_T9-12|itrx9_9_12_10Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j18_T22-60|itrx9_22_60_10Y|FALSE|FALSE|TRUE
    itraxx9|itraxx9_j18_T12-22|itrx9_12_22_10Y|TRUE|TRUE|TRUE
    itraxx9|itraxx9_j18_T22-100|itrx9_22_100_10Y|FALSE|TRUE|TRUE
    itraxx9|itraxx9_j18_T60-100|itrx9_60_100_10Y|FALSE|FALSE|TRUE

  2. #2
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    49

    Talking

    If each of your for each loops do not go back up to the root each time then there would not be the issue of duplication.

    Try one of the following...

    either:
    Code:
    <xsl:for-each select="//Config/Indexes/Index">
      ...
      <xsl:for-each select="./Series/SeriesNumber">
        ...
        <xsl:for-each select="ancestor::Index/Issuances/Maturity">
          ...
          <xsl:for-each select="ancestor::Index/Tranches/Tranche">
            ...
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
    or:
    Code:
    <xsl:for-each select="//Config/Indexes/Index">
      <xsl:variable name="index" select="." />
      ...
      <xsl:for-each select="$index/Series/SeriesNumber">
        ...
        <xsl:for-each select="$index/Issuances/Maturity">
          ...
          <xsl:for-each select="$index/Tranches/Tranche">
            ...
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>

  3. #3
    Join Date
    Oct 2010
    Posts
    2

    Thumbs up

    Excellent, many thanks reedy. I went with your first suggestion and it works great.

    Thanks again,

    Mark

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2008, 10:27 AM
  2. Replies: 1
    Last Post: 10-02-2006, 01:52 PM
  3. XSL: break for-each loop Help required !
    By back2grave in forum XML
    Replies: 1
    Last Post: 06-13-2006, 12:11 PM
  4. Nested loop limit
    By noelia in forum VB Classic
    Replies: 1
    Last Post: 05-11-2005, 03:37 PM
  5. GOSUB vs. Macros vs. Nested Functions
    By Jonathan Allen in forum .NET
    Replies: 331
    Last Post: 03-19-2001, 09:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center