Reputation: 127
I have the following XML document, and XSL stylesheet, and I've been tasked to apply the piece of CSS found in the XSL, to the stylesheet, and apply it when the result of the test is FAIL. I can't seem to work out how to do it, I've tried an xsl:if but couldn't seem to get it to change. Any ideas?
Here's the code:
XML File:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="results.xsl"?>
<request xmlns:xsi="httyp://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="results.xsd">
<authentication password="turkey" partnerid="exam" />
<method name="ListUserTestResults">
<parameters>
<user_id>42511</user_id>
<transcript>4234232</transcript>
<test_id>231</test_id>
<test_name>ASP 3.0 Test</test_name>
<percentage>75</percentage>
<test_result>PASS</test_result>
<time>2006-04-19T14:05:11Z</time>
</parameters>
<parameters>
<user_id>42511</user_id>
<transcript>2356545</transcript>
<test_id>12</test_id>
<test_name>PHP 5.0 Test</test_name>
<percentage>35</percentage>
<test_result>FAIL</test_result>
<time>2006-05-17T10:19:45Z</time>
</parameters>
</method>
</request>
And the XSL Stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0.1"/>
<xsl:template match="/">
<style type="text/css">
.FAIL {color:red;}
</style>
<h1>Results from tests:</h1>
<xsl:for-each select="request/method/parameters">
<h2>
<xsl:number count="parameters" />
User <xsl:value-of select="user_id"/>
</h2>
<h3>
<xsl:value-of select="test_name"/>
(<xsl:value-of select="test_id"/>)
</h3>
<dl>
<dt>Transcript</dt>
<dd><xsl:value-of select="transcript"/></dd>
<dt>Percentage</dt>
<dd><xsl:value-of select="percentage"/>
<xsl:if
(<xsl:value-of select="test_result"/>)
</dd>
<dt>Date/Time of test</dt>
<dd><xsl:value-of select="time"/></dd>
</dl>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The test_result element needs to be changed from black text, to the CSS defined red, when read in the browser. I just can't work out how to do it.
Upvotes: 0
Views: 1633
Reputation: 243469
Here is a complete transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<style type="text/css">
.FAIL {color:red;}
</style>
<h1>Results from tests:</h1>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="parameters">
<h2>
<xsl:number count="parameters" />
User <xsl:value-of select="user_id"/>
</h2>
<h3>
<xsl:value-of select="test_name"/>
(<xsl:value-of select="test_id"/>)
</h3>
<dl>
<dt>Transcript</dt>
<dd><xsl:value-of select="transcript"/></dd>
<dt>Percentage</dt>
<dd>
<xsl:value-of select="percentage"/>
</dd>
<dt>Result</dt>
<dd>
<xsl:if test="test_result = 'FAIL'">
<xsl:attribute name="class">FAIL</xsl:attribute>
</xsl:if>
(<xsl:value-of select="test_result"/>)
</dd>
<dt>Date/Time of test</dt>
<dd><xsl:value-of select="time"/></dd>
</dl>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<request xmlns:xsi="httyp://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="results.xsd">
<authentication password="turkey" partnerid="exam" />
<method name="ListUserTestResults">
<parameters>
<user_id>42511</user_id>
<transcript>4234232</transcript>
<test_id>231</test_id>
<test_name>ASP 3.0 Test</test_name>
<percentage>75</percentage>
<test_result>PASS</test_result>
<time>2006-04-19T14:05:11Z</time>
</parameters>
<parameters>
<user_id>42511</user_id>
<transcript>2356545</transcript>
<test_id>12</test_id>
<test_name>PHP 5.0 Test</test_name>
<percentage>35</percentage>
<test_result>FAIL</test_result>
<time>2006-05-17T10:19:45Z</time>
</parameters>
</method>
</request>
the wanted, correct result is produced:
<style type="text/css">
.FAIL {color:red;}
</style>
<h1>Results from tests:</h1>
<h2>1
User 42511</h2>
<h3>ASP 3.0 Test
(231)
</h3>
<dl>
<dt>Transcript</dt>
<dd>4234232</dd>
<dt>Percentage</dt>
<dd>75</dd>
<dt>Result</dt>
<dd>
(PASS)
</dd>
<dt>Date/Time of test</dt>
<dd>2006-04-19T14:05:11Z</dd>
</dl>
<h2>2
User 42511</h2>
<h3>PHP 5.0 Test
(12)
</h3>
<dl>
<dt>Transcript</dt>
<dd>2356545</dd>
<dt>Percentage</dt>
<dd>35</dd>
<dt>Result</dt>
<dd class="FAIL">
(FAIL)
</dd>
<dt>Date/Time of test</dt>
<dd>2006-05-17T10:19:45Z</dd>
</dl>
Explanation:
Proper use of xsl:if
and xsl:attribute
.
Upvotes: 1
Reputation: 11298
You need <xsl:when>
for condition checking here
<dl>
<dt>Transcript</dt>
<dd><xsl:value-of select="transcript"/></dd>
<dt>Percentage</dt>
<dd><xsl:value-of select="percentage"/>
<xsl:choose>
<xsl:when test="test_result = 'PASS'">
<dd><xsl:value-of select="test_result"/></dd>
</xsl:when>
<xsl:otherwise>
<dd style="color: red"><xsl:value-of select="test_result"/></dd>
</xsl:otherwise>
</xsl:choose>
</dd>
<dt>Date/Time of test</dt>
<dd><xsl:value-of select="time"/></dd>
</dl>
Upvotes: 0