Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Reply: XSL & Javascript

Date Posted:1/23/2007 10:21 PM
Posted by:
k t
Hi All..!

I think this is a tough question... I have existing XSL/XML code that I need to transform so that there are three variables: Severity, Exposure, Probability and the values change depending on user selection.  Then I need to multiply those three variables using the javascript code within the "CDATA" tags.
How can I change the below code to do this??

Here is the code I need to change:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:foo="http://www.pacificedge.com/foo" extension-element-prefixes="msxsl" exclude-result-prefixes="foo">
  <xsl:output method="html" />
  <xsl:variable name="calcNameId">
    <xsl:value-of select="generate-id()" />
  </xsl:variable>
  <msxsl:script language="javascript" implements-prefix="foo">
    <![CDATA[
            var total = 0;
            function ProcessCalc(inum)
            {
                //
                // do some stuff here
                //
                return parseInt(inum);
            }
            function SetSubtotal(inum)
            {
                total += inum;
                return inum;
            }
            function GetTotal(iRowCount)
            {
                return Math_Round(parseFloat(total/iRowCount), 2);
            }
            function Math_Round(x, decimalPlaces)
            {
                if (isNaN(decimalPlaces))
                    return Math.round(x);
                var precision = Math.pow(10, decimalPlaces);
                x *= precision;
                x = Math.round(x);
                x /= precision;
                return x;
            }
            
            function GetFontColor(min, max, inum, color)
            {
                if(inum >= min && inum < max)
                {
                    return color;
                }
                return "";
            }
        ]]>
  </msxsl:script>
  <xsl:template match="root">
    <TABLE border="1" width="100%">
      <xsl:apply-templates select="columns" />
      <xsl:apply-templates select="row" />
    </TABLE>
  </xsl:template>
  <xsl:template match="columns">
    <TR>
      <xsl:for-each select="*/HeaderValue">
        <TD class="Heading">
          <xsl:value-of select="value" />
        </TD>
      </xsl:for-each>
    </TR>
  </xsl:template>
  <xsl:template match="row">
    <TR>
      <xsl:choose>
        <xsl:when test="position() mod 2 = 0">
          <xsl:attribute name="class">AR</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="class">R</xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:for-each select="*">
        <xsl:choose>
          <xsl:when test="@type='text'">
            <TD class="RHC" style="width:300px;">
              <DIV>
                <xsl:attribute name="id">
                  <xsl:value-of select="@id" />
                </xsl:attribute>
                <xsl:value-of select="value" />
              </DIV>
            </TD>
          </xsl:when>
          <xsl:when test="@type='list'">
            <TD class="RHC">
              <TABLE WIDTH="100%">
                <TR>
                  <TD align="center">
                    <xsl:choose>
                      <xsl:when test="http://root/@readonly='true'">
                        <xsl:for-each select="ListItem">
                          <xsl:if test="@selected='true'">
                            <xsl:value-of select="text()" />
                          </xsl:if>
                        </xsl:for-each>
                      </xsl:when>
                      <xsl:otherwise>
                        <SELECT onchange="fnSelect_Onchange();">
                          <xsl:attribute name="id">
                            <xsl:value-of select="@id" />
                          </xsl:attribute>
                          <xsl:attribute name="nodeName">
                            <xsl:value-of select="@nodeName" />
                          </xsl:attribute>
                          <xsl:attribute name="rowId">
                            <xsl:value-of select="@rowId" />
                          </xsl:attribute>
                          <xsl:if test="http://root/@readonly='true'">
                            <xsl:attribute name="DISABLED">true</xsl:attribute>
                          </xsl:if>
                          <xsl:for-each select="ListItem">
                            <OPTION>
                              <xsl:if test="@selected='true'">
                                <xsl:attribute name="SELECTED">
                                  <xsl:value-of select="@selected" />
                                </xsl:attribute>
                              </xsl:if>
                              <xsl:attribute name="value">
                                <xsl:value-of select="@key" />
                              </xsl:attribute>
                              <xsl:value-of select="text()" />
                            </OPTION>
                          </xsl:for-each>
                        </SELECT>
                      </xsl:otherwise>
                    </xsl:choose>
                  </TD>
                </TR>
              </TABLE>
            </TD>
          </xsl:when>
          <xsl:when test="@type='calc'">
            <xsl:variable name="localgrade">
              <xsl:choose>
                <xsl:when test="../cell/@key='Likelihood-1'">
                  <xsl:choose>
                    <xsl:when test="../cell/@key='Seriousness-1'">
                      <xsl:text>E</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-2'">
                      <xsl:text>D</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-3'">
                      <xsl:text>B</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-4'">
                      <xsl:text>A</xsl:text>
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
                <xsl:when test="../cell/@key='Likelihood-2'">
                  <xsl:choose>
                    <xsl:when test="../cell/@key='Seriousness-1'">
                      <xsl:text>D</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-2'">
                      <xsl:text>C</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-3'">
                      <xsl:text>B</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-4'">
                      <xsl:text>A</xsl:text>
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
                <xsl:when test="../cell/@key='Likelihood-3'">
                  <xsl:choose>
                    <xsl:when test="../cell/@key='Seriousness-1'">
                      <xsl:text>C</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-2'">
                      <xsl:text>B</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-3'">
                      <xsl:text>A</xsl:text>
                    </xsl:when>
                    <xsl:when test="../cell/@key='Seriousness-4'">
                      <xsl:text>A</xsl:text>
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
              </xsl:choose>
            </xsl:variable>
            <xsl:if test="@name='Grade'">
              <TD class="RHC" align="center" type="calc" dataKey="Grade" style="width:50px;">
                <xsl:attribute name="name">
                  <xsl:value-of select="$calcNameId" />
                </xsl:attribute>
                <xsl:value-of select="$localgrade" />
              </TD>
            </xsl:if>
            <xsl:variable name="localSubtotal">
              <xsl:choose>
                <xsl:when test="$localgrade='A'">
                  <xsl:choose>
                    <xsl:when test="../cell/@key='Cost-1'">
                      <xsl:value-of select="foo:ProcessCalc(3)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-2'">
                      <xsl:value-of select="foo:ProcessCalc(4)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-3'">
                      <xsl:value-of select="foo:ProcessCalc(5)" />
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
                <xsl:when test="$localgrade='B'">
                  <xsl:choose>
                    <xsl:when test="../cell/@key='Cost-1'">
                      <xsl:value-of select="foo:ProcessCalc(2)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-2'">
                      <xsl:value-of select="foo:ProcessCalc(3)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-3'">
                      <xsl:value-of select="foo:ProcessCalc(4)" />
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
                <xsl:when test="$localgrade='C'">
                  <xsl:choose>
                    <xsl:when test="../cell/@key='Cost-1'">
                      <xsl:value-of select="foo:ProcessCalc(1)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-2'">
                      <xsl:value-of select="foo:ProcessCalc(2)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-3'">
                      <xsl:value-of select="foo:ProcessCalc(3)" />
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
                <xsl:when test="$localgrade='D'">
                  <xsl:choose>
                    <xsl:when test="../cell/@key='Cost-1'">
                      <xsl:value-of select="foo:ProcessCalc(1)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-2'">
                      <xsl:value-of select="foo:ProcessCalc(1)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-3'">
                      <xsl:value-of select="foo:ProcessCalc(2)" />
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
                <xsl:when test="$localgrade='E'">
                  <xsl:choose>
                    <xsl:when test="../cell/@key='Cost-1'">
                      <xsl:value-of select="foo:ProcessCalc(1)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-2'">
                      <xsl:value-of select="foo:ProcessCalc(1)" />
                    </xsl:when>
                    <xsl:when test="../cell/@key='Cost-3'">
                      <xsl:value-of select="foo:ProcessCalc(1)" />
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
                <xsl:when test="$localgrade=''">
                  <xsl:choose>
                    <xsl:when test="../cell/value=''">
                      <xsl:text>average of all the above values</xsl:text>
                    </xsl:when>
                  </xsl:choose>
                </xsl:when>
              </xsl:choose>
            </xsl:variable>
            <xsl:if test="@name='Subtotal'">
              <TD class="RHC" align="center" type="calc" dataKey="Subtotal">
                <xsl:attribute name="subtotal">
                  <xsl:value-of select="$localSubtotal" />
                </xsl:attribute>
                <xsl:attribute name="style">
                  width:50px;
                  behavior:url('<xsl:value-of select="http://root/@webroot" />/controls/scorecard/numberlocale.htc');
                </xsl:attribute>
                <xsl:attribute name="name">
                  <xsl:value-of select="$calcNameId" />
                </xsl:attribute>
                <xsl:value-of select="foo:SetSubtotal(number($localSubtotal))" />
              </TD>
            </xsl:if>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </TR>
    <xsl:if test="position()=last()">
      <TR>
        <TABLE class="subtotal" BORDER="0" WIDTH="100%">
          <TR>
            <xsl:variable name="total">
              <xsl:value-of select="foo:GetTotal(count(//root/row))" />
            </xsl:variable>
            <xsl:variable name="fontcolor">
              <xsl:for-each select="http://root/columns/Column/StyleExpressions/StyleExpression">
                <xsl:value-of select="foo:GetFontColor(number(min), number(max), number($total), string(color))" />
              </xsl:for-each>
            </xsl:variable>
            <TD class="subtotaltxt">
              <xsl:attribute name="style">
                color:<xsl:value-of select="$fontcolor" />;
              </xsl:attribute>
              <i>SubTotal</i>
            </TD>
            <TD ID="ctlSubtotalId" class="subtotalnum" align="center" WIDTH="50">
              <xsl:attribute name="total">
                <xsl:value-of select="$total" />
              </xsl:attribute>
              <xsl:attribute name="colspan">
                <xsl:value-of select="count(//root/row/cell) div count(//root/row) - 1" />
              </xsl:attribute>
              <xsl:attribute name="style">
                color:<xsl:value-of select="$fontcolor" />;
                behavior:url('<xsl:value-of select="http://root/@webroot" />/controls/scorecard/numberlocale.htc');
              </xsl:attribute>
              <xsl:value-of select="$total" />
            </TD>
          </TR>
        </TABLE>
      </TR>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>



Here is my XML Layout already prepared for the change:

<?xml version="1.0" encoding="utf-8"?>
<ScoreCardDef>
  <Columns>
    <Column id="EWRMType" type="text">
      <HeaderValue>
        <value>Status</value>
      </HeaderValue>
    </Column>
    <Column id="Severity" type="list">
      <HeaderValue>
        <value>Severity</value>
      </HeaderValue>
      <ListItem key="Severity-1">a</ListItem>
      <ListItem key="Severity-2">b</ListItem>
      <ListItem key="Severity-3">c</ListItem>
      <ListItem key="Severity-3">d</ListItem>
      <ListItem key="Severity-3">e</ListItem>
      <ListItem key="Severity-3">f</ListItem>
      <ListItem key="Severity-3">g</ListItem>
    </Column>
    <Column id="Exposure" type="list">
      <HeaderValue>
        <value>Exposure</value>
      </HeaderValue>
      <ListItem key="Exposure-1">At Least Once A Week</ListItem>
      <ListItem key="Exposure-2">Once A Month Or So</ListItem>
      <ListItem key="Exposure-3">Once Or Twice A Year</ListItem>
      <ListItem key="Exposure-4">Once Or Twice Every 10 Years</ListItem>
      <ListItem key="Exposure-5">Once Or Twice in 100 Years</ListItem>
    </Column>
    <Column id="Probability" type="list">
      <HeaderValue>
        <value>Probability</value>
      </HeaderValue>
      <ListItem key="Probability-1">Happens Often</ListItem>
      <ListItem key="Probability-2">Could Easily Happen</ListItem>
      <ListItem key="Probability-3">Could Happen and Has Happened Here or Elsewhere</ListItem>
      <ListItem key="Probability-4">Hasn't Happened Yet But Could</ListItem>
      <ListItem key="Probability-5">Conceivable, But Only In Extreme Circumstances</ListItem>
    </Column>
    <!-- calc w/ xsl -->
    <Column id="EWRM" type="calc">
      <HeaderValue>
        <value>EWRM</value>
      </HeaderValue>
    </Column>
  </Columns>
  <Total>
  </Total>
</ScoreCardDef>

Any help would be greatly appreciated!

Kt





Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy links on this site!
remote data storage
Marc jacobs sunglasses
Security Camera
Web Design
Color Laser Printer
Buy links on this site!

Interested in Text ads?
2,277 Total Members
42 members(last 30 days)
13 members(last 7 days)
3 members(today)

1,629 Total Discussions
17 Posts(last 30 days)
2 Posts(last 7 days)
1 Posts(today)

20,047 Total Blog Posts
2,133 Blogs(last 30 days)
573 Blogs(last 7 days)
56 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,802 Total Resources
16 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139