我的XSL文档中的数字函数和格式问题,以及名称和百分比都没有显示在结果文档中

时间:2015-11-01 03:59:58

标签: xml xslt

我已经坚持这个任务好几个星期了,我认为我解决了大部分问题(请在必要时指出)。我正在完成一个XSL文档,该文档主要由教科书作者完成,它与预先制作的XML文档相关联,该文档显示候选人姓名,投票数,百分比,派对和地区。该页面呈现的大致正确,除了候选人,地区,政党(D或R)和投票百分比(百分比显示为 NaN )的名称缺失。除此之外,它看起来很好并且有效。

" candidatePercent"变量包含分配给候选人的投票百分比,通过划分" candidateVotes" by" totalVotes"。这是我的代码:

<xsl:variable name="candidatePercent" select="($candidateVotes) div count($totalVotes)" />

在&#34; drawCells&#34;模板,它说要制作&#34; cellCount&#34;参数等于&#34; candidatePercent&#34;的值乘以100并四舍五入到最接近的整数。这是我的代码:

<xsl:with-param name="cellCount" select="100 * round($candidatePercent)" />

以下是完整样式表供参考:

   <?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="html"
      doctype-system="about:legacy-compat"
      encoding="UTF-8"
      indent="yes" />

<xsl:variable name="candidateInfo" 
    select="/candidates/candidate" />

   <xsl:template match="/">
      <html>
         <head>
            <title>Minnesota Congressional Election Results</title>
            <link href="vwstyles.css" rel="stylesheet" type="text/css" />
         </head>

         <body>
            <div id="wrap">
               <header>
                  <img src="vwlogo.png" alt="Voter Web" />
               </header>

               <h1>Minnesota Congressional Election Results</h1>

               <section id="votingResults">
                  <xsl:apply-templates select="congressResults/district" />
               </section>

             </div>
         </body>
      </html>
   </xsl:template>

   <xsl:template match="district">
      <h2>District <xsl:value-of select="@dNumber" /></h2>
      <table class="electionTable">
         <thead>
            <tr>
               <th>Candidate</th>
               <th>Votes</th>
            </tr>
         </thead>
         <tbody>
            <xsl:apply-templates select="candidates/candidate" />
         </tbody>
      </table>
   </xsl:template>

   <xsl:template match="candidate">
        <xsl:variable name="candidateVotes" select="sum(votes)" />
        <xsl:variable name="totalVotes" select="sum(..//votes)" />
            <xsl:variable name="candidatePercent" select="($candidateVotes) div count($totalVotes)" />
        <xsl:variable name="candidateName" select="$candidateInfo[@candidateID=current()/@candidateID]/name" />
        <xsl:variable name="candidateParty" select="$candidateInfo[@candidateID=current()/@candidateID]/party" />
      <tr>
        <th>
            <xsl:value-of select="$candidateName" />
            (<xsl:value-of select="$candidateParty" />)
        </th>
        <th>
            <xsl:value-of select="format-number($candidateVotes, '###,##0')" />
            (<xsl:value-of select="format-number(ScandidatePercent, '#0.0%')" />)
        </th>y
        <td>
            <xsl:call-template name="drawCells">
            <xsl:with-param name="cellCount" select="100 * round($candidatePercent)" />
            <xsl:with-param name="party" select="$candidateParty" />
            </xsl:call-template>
        </td>
      </tr>
   </xsl:template>

   <xsl:template name="drawCells">
      <xsl:param name="cellCount" />
      <xsl:param name="party" />
      <xsl:if test="$cellCount > 0">
         <td class="{$party}"></td>
         <xsl:call-template name="drawCells">
            <xsl:with-param name="cellCount" select="$cellCount - 1" />
            <xsl:with-param name="party" select="$party" />
         </xsl:call-template>
      </xsl:if>
   </xsl:template>

</xsl:stylesheet>

XML文档(由教科书作者创建):

   <?xml version="1.0" encoding="UTF-8" ?>

<candidates>
   <candidate candidateID="DFL1">
      <name>Sanchez, Onita</name>
      <party>D</party>
   </candidate>
   <candidate candidateID="DFL2">
      <name>Berk, Thomas</name>
      <party>D</party>
   </candidate>
   <candidate candidateID="DFL3">
      <name>Thomas, Howard</name>
      <party>D</party>
   </candidate>
   <candidate candidateID="DFL4">
      <name>Sweet, Alice</name>
      <party>D</party>
   </candidate>
   <candidate candidateID="DFL5">
      <name>Aitkens, Mary</name>
      <party>D</party>
   </candidate>
   <candidate candidateID="DFL6">
      <name>Nielsen, Kevin</name>
      <party>D</party>
   </candidate>
   <candidate candidateID="DFL7">
      <name>Pulaski, Stewart</name>
      <party>D</party>
   </candidate>
   <candidate candidateID="DFL8">
      <name>Venn, Michael</name>
      <party>D</party>
   </candidate>

   <candidate candidateID="REP1">
      <name>Troutman, Rachel</name>
      <party>R</party>
   </candidate>
   <candidate candidateID="REP2">
      <name>Chiang, Michael</name>
      <party>R</party>
   </candidate>
   <candidate candidateID="REP3">
      <name>Olsen, Fred</name>
      <party>R</party>
   </candidate>
   <candidate candidateID="REP4">
      <name>Grovener, Stewart</name>
      <party>R</party>
   </candidate>
   <candidate candidateID="REP5">
      <name>Mundleberg, Linda</name>
      <party>R</party>
   </candidate>
   <candidate candidateID="REP6">
      <name>Francis, Trevor</name>
      <party>R</party>
   </candidate>
   <candidate candidateID="REP7">
      <name>Biersen, Nancy</name>
      <party>R</party>
   </candidate>
   <candidate candidateID="REP8">
      <name>Ramirez, Juan</name>
      <party>R</party>
   </candidate>

   <candidate candidateID="IND1">
      <name>Whitman, Gary</name>
      <party>I</party>
   </candidate>
   <candidate candidateID="IND2">
      <name>Larson, Alicia</name>
      <party>I</party>
   </candidate>
   <candidate candidateID="IND3">
      <name>Shapiro, Douglas</name>
      <party>I</party>
   </candidate>
   <candidate candidateID="IND4">
      <name>Reardin, Samuel</name>
      <party>I</party>
   </candidate>
   <candidate candidateID="IND5">
      <name>Ketrick, Rachel</name>
      <party>I</party>
   </candidate>
   <candidate candidateID="IND6">
      <name>Inglessohn, Ray</name>
      <party>I</party>
   </candidate>
   <candidate candidateID="IND7">
      <name>Pope, Richard</name>
      <party>I</party>
   </candidate>
   <candidate candidateID="IND8">
      <name>Zander, Audry</name>
      <party>I</party>
   </candidate>

   <candidate candidateID="WI1">
      <name>Other</name>
      <party>O</party>
   </candidate>
   <candidate candidateID="WI2">
      <name>Other</name>
      <party>O</party>
   </candidate>
   <candidate candidateID="WI3">
      <name>Other</name>
      <party>O</party>
   </candidate>
   <candidate candidateID="WI4">
      <name>Other</name>
      <party>O</party>
   </candidate>
   <candidate candidateID="WI5">
      <name>Other</name>
      <party>O</party>
   </candidate>
   <candidate candidateID="WI6">
      <name>Other</name>
      <party>O</party>
   </candidate>
   <candidate candidateID="WI7">
      <name>Other</name>
      <party>O</party>
   </candidate>
   <candidate candidateID="WI8">
      <name>Other</name>
      <party>O</party>
   </candidate>

</candidates>

Source Document (congress.xml) for reference

谢谢。

1 个答案:

答案 0 :(得分:0)

我认为我实际上已经解决了(以及其他错误),当然还有一些帮助。

首先,我错过了我在这里有一个首都“S”

  (<xsl:value-of select="format-number(ScandidatePercent, '#0.0%')" />) 

而不是“$”。其次,我应该从

中删除“计数”
<xsl:variable name="candidatePercent" select="($candidateVotes) div count($totalVotes)" />
    `

我也没有坚持使用全局变量的 document()函数。我认为,因为我上周几乎完成了它只有路径,它将没有工作。 TLDR;多注意,使用更好的眼镜,不要让事情变得更复杂......

无论如何我都很感激!