#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    XSL for-each loop


    This is my xml
    <cruisecontrol project="EIQServerSuiteBuild">
    <test-results name="EIQServerConfigAndQueryTests">
    <test-suite name="EIQServerConfigAndQueryTestSuite" success="True" />
    <results>

    <test-case name="ATTACHVDSQueryTest" executed="True" success="True">ATTACHVDSQueryTest</test-case>
    <test-case name="SAMPLEVDSQueryTest" executed="True" success="False">SAMPLEVDSQueryTest</test-case>
    <test-case name="INDEXEDVIEWVDSQueryTest" executed="True" success="True">INDEXEDVIEWVDSQueryTest</test-case>
    </results>
    </test-results>
    </cruisecontrol>

    I written an xsl script to print the names of the test suites based on the condition of
    if executed="True" success="False"
    print the test suite name.
    so in this case it should print
    SAMPLEVDSQueryTest
    as the output.

    but my xsl script prints ATTACHVDSQueryTest 3 times since loop count is 3.
    I think it is printing the first TestSuite name 3 times.
    If i chnaged the xml and have INDEXEDVIEWVDSQueryTest as the first <test-case> in <results> tag, it is printing INDEXEDVIEWVDSQueryTest 3 times.

    this is my xsl script that i am using

    <?xml version="1.0"?>
    <xsl:stylesheet
    xmlns:xsl="(...." version="1.0">
    <xslutput method="html"/>
    <xsl:variable name="tests.root" select="cruisecontrol//test-results"/>
    <xsl:template match="test-results">
    <tr>
    <td colspan="2" class="section-data">
    <tr>
    <td>
    <b>Tests Failed : </b>
    </td>
    </tr>
    <xsl:for-each select = "($tests.root//test-case)">
    <xsl:if test = "($tests.root//test-case[@success = 'False'])">
    <tr>
    <td>
    <b>
    <xsl:value-of select="($tests.root//test-case[@name])"/>
    </b>
    </td>
    </tr>
    </xsl:if>
    </xsl:for-each>
    </td>
    </tr>
    </xsl:template>
    </xsl:stylesheet>

    Please let me know why i am getting wrong results. Thank you
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,074
    Rep Power
    9398
    The for-each is setting a context for you. You need to use it.
    Code:
    <xsl:if test = "($tests.root//test-case[@success = 'False'])">
    By using the full path there you're completely ignoring what the loop has done for you. That expression will get the first matching node, and since the loop found three nodes it'll print that same first node three times.

    You can do the condition right in the query, eliminating the need for an xsl:if:
    Code:
    <xsl:for-each select="$tests.root/test-case[@executed='True'][@success='False']">
    	<tr>
    		<td>
    			<b><xsl:value-of select="@name" /></b>
    		</td>
    	</tr>	
    </xsl:for-each>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Thank you


    Originally Posted by requinix
    The for-each is setting a context for you. You need to use it.
    Code:
    <xsl:if test = "($tests.root//test-case[@success = 'False'])">
    By using the full path there you're completely ignoring what the loop has done for you. That expression will get the first matching node, and since the loop found three nodes it'll print that same first node three times.

    You can do the condition right in the query, eliminating the need for an xsl:if:
    Code:
    <xsl:for-each select="$tests.root/test-case[@executed='True'][@success='False']">
    	<tr>
    		<td>
    			<b><xsl:value-of select="@name" /></b>
    		</td>
    	</tr>	
    </xsl:for-each>

    Thank you. It worked.

IMN logo majestic logo threadwatch logo seochat tools logo