不确定我的XSL文档有什么问题,而且我的XML结果文档没有正确重新正确

时间:2015-10-25 22:08:22

标签: xml xslt functional-programming

DropBox link to XML file我一直在研究使用函数式编程所需的这个XSL文档,即使它没有错误,结果文档也没有正确呈现,看起来像只有数字的页面和页面没有文字(看起来像机器代码。)。

结果应显示候选人姓名,投票,投票百分比,地区和等于投票时间的条形图。我一直搞乱它,但似乎没有什么改变外观。令我困惑的一些部分是:引用"候选人/候选人"的全局变量;节点集; cellCount参数等于" candidatePercent"的值。变量MULTIPLIED 100并四舍五入到最接近的整数;除其他外。

这是我的代码:

<?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="document('candidates.xml')/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="sum($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>
        <td>
            <xsl:call-template name="drawCells">
            <xsl:param name="cellCount" select="100 * round($candidatePercent" />
            <xsl: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文档太长了,基本上只包含名称和投票。有人请提供一些指导吗?谢谢。

ORIGINAL XSL:

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

<!-- New Perspectives on XML, 3rd Edition Tutorial 6 Case Problem 1 Voter Web Style Sheet Author: Date: Filename: election.xsl Supporting Files: -->

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

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


-<xsl:template match="/">


-<html>


-<head>

<title>Minnesota Congressional Election Results</title>

<link type="text/css" href="vwstyles.css" rel="stylesheet"/>

</head>


-<body>


-<div id="wrap">


-<header>

<img alt="Voter Web" src="vwlogo.png"/>

</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">

<tr> </tr>

</xsl:template>


-<xsl:template name="drawCells">

<xsl:param name="cellCount"/>

<xsl:param name="party"/>


-<xsl:if test="$cellCount > 0">

<td class="{$party}"/>


-<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>

2 个答案:

答案 0 :(得分:1)

这是一个XSLT 2.0解决方案。如果您使用浏览器作为转换引擎,请链接到Saxon CE的本地副本。为简洁起见,我将候选数据嵌入到样式表中。我将其留给OP进行微调,以便从外部文档加载候选数据。

输入文档

<congressResults>
   <district dNumber="1">
      <candidates>
         <candidate candidateID="DFL1">
            <votes precinct="ADAMS" precinctID="0005">263</votes>
            <votes precinct="ADAMS TWP." precinctID="0010">181</votes>
            <votes precinct="ADRIAN" precinctID="0005">349</votes>
            <votes precinct="CITY OF COURTLAND" precinctID="0020">195</votes>
         </candidate>
         <candidate candidateID="DFL2">
            <votes precinct="ADAMS" precinctID="0005">363</votes>
            <votes precinct="ADAMS TWP." precinctID="0010">281</votes>
            <votes precinct="ADRIAN" precinctID="0005">1</votes>
            <votes precinct="CITY OF COURTLAND" precinctID="0020">200</votes>
         </candidate>
      </candidates>
   </district>
</congressResults>

** XSLT 2.0样式表**

<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
<xsl:output method="html" version="5" encoding="UTF-8" />
<xsl:strip-space elements="*" />

<xsl:variable name="bar-chart-width-for-100pct-votes" select="500" />

<xsl:variable name="candidate-info">
  <candidate candidateID="DFL1">
    <name>Sean</name>
    <party>red-party</party>
  </candidate>
  <candidate candidateID="DFL2">
    <name>John</name>
    <party>blue-party</party>
  </candidate>
</xsl:variable>

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

<body>
    <img src="http://www.brandsoftheworld.com/sites/default/files/styles/logo-thumbnail/public/0022/7347/brand.gif?itok=CiI8BEGj" />
    <h1>Minnesota Congressional Election Results</h1>
    <xsl:apply-templates />
</body>
</html>
</xsl:template>

<xsl:template match="district">
    <section>
      <h2>District <xsl:value-of select="@dNumber" /></h2>
      <xsl:variable name="total-votes" select="sum(candidates/candidate/votes)" />
      <p><xsl:value-of select="$total-votes" /> votes in total for district <xsl:value-of select="@dNumber" /></p>
      <table class="vote-table">
        <thead>
          <tr>
            <th class="name-col">Candidate</th>
            <th colspan="3">Votes</th>
          </tr>
        </thead>
        <tbody>
          <xsl:apply-templates select="candidates/candidate">
            <xsl:with-param name="total-votes" select="$total-votes" />
          </xsl:apply-templates>
        </tbody>
      </table>
    </section>
</xsl:template>

<xsl:template match="candidate">
  <xsl:param name="total-votes" />
  <xsl:variable name="candidate" select="$candidate-info/candidate[@candidateID eq current()/@candidateID]" />
  <xsl:variable name="votes"     select="sum(votes)" />  
  <tr>
    <xsl:if test="(position()) mod 2 eq 0">
      <xsl:attribute name="class">alt</xsl:attribute>
    </xsl:if>
            <td><xsl:value-of select="$candidate/name" /></td>
            <td><xsl:value-of select="format-number( $votes, '###,##0')" /></td>
            <td><xsl:value-of select="format-number( $votes div $total-votes, '#,##0.0000%')" /></td>
            <td style="padding: 5px;">
              <div class="{$candidate/party} logo"/>
              <div class="{$candidate/party} chart" style="width:{ round($votes div $total-votes * $bar-chart-width-for-100pct-votes)}px;" />
            </td>
  </tr>
</xsl:template>

</xsl:transform>

...产生结果......

&#13;
&#13;
/* vwstyles.css */
.logo {
  width: 20px;
  height: 20px;
  background-repeat: no-repeat; 
}

.chart {
  height: 15px;
  position: relative;
  left: 30px;
  margin: 1px;
  color: white;        
}

.red-party.chart {
  background-color: indianred;    
}

.blue-party.chart {
  background-color: steelblue;    
}

.red-party.logo {
  background-image: url('http://cdn.sstatic.net/img/favicons-sprite16.png?v=f55037417ca7155882f8bca641c8c8bb');
}

.blue-party.logo {
  background-image: url('https://www.gravatar.com/avatar/69d49d43101b3ba3adb7e53d23369551?s=64&d=identicon&r=PG');
}

.vote-table {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    width: 100%;
    border-collapse: collapse;
}

.vote-table td, #customers th {
    font-size: 1em;
    border: 1px solid #98bf21;
    padding: 3px 7px 2px 7px;
}

.vote-table th {
    font-size: 1.1em;
    text-align: left;
    padding-top: 5px;
    padding-bottom: 4px;
    background-color: #A7C942;
    color: #ffffff;
}

.vote-table tr.alt td {
    color: #000000;
    background-color: #EAF2D3;
}

.name-col {
  width: 150px;   
}    
    
.number-col {
  width: 50px;   
}    
    
}
&#13;
<!DOCTYPE HTML>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Minnesota Congressional Election Results</title>
      <link href="vwstyles.css" rel="stylesheet" type="text/css">
   </head>
   <body><img src="http://www.brandsoftheworld.com/sites/default/files/styles/logo-thumbnail/public/0022/7347/brand.gif?itok=CiI8BEGj"><h1>Minnesota Congressional Election Results</h1>
      <section>
         <h2>District 1</h2>
         <p>1833 votes in total for district 1</p>
         <table class="vote-table">
            <thead>
               <tr>
                  <th class="name-col">Candidate</th>
                  <th colspan="3">Votes</th>
               </tr>
            </thead>
            <tbody>
               <tr>
                  <td>Sean</td>
                  <td>988</td>
                  <td>53.9007%</td>
                  <td style="padding: 5px;">
                     <div class="red-party logo"></div>
                     <div class="red-party chart" style="width:270px;"></div>
                  </td>
               </tr>
               <tr class="alt">
                  <td>John</td>
                  <td>845</td>
                  <td>46.0993%</td>
                  <td style="padding: 5px;">
                     <div class="blue-party logo"></div>
                     <div class="blue-party chart" style="width:230px;"></div>
                  </td>
               </tr>
            </tbody>
         </table>
      </section>
   </body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是我的xml类的问题。解决方案是。

(\d+)

您的代码无效的原因。

  

<?xml version="1.0" encoding="UTF-8" ?> <!-- New Perspectives on XML, 3rd Edition Tutorial 6 Case Problem 1 Voter Web Style Sheet Author: Nicholas English Date: 4/16/2018 Filename: election.xsl Supporting Files: --> <!-- Style and output declaration. --> <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" /> <!-- The candidate info variable. --> <xsl:variable name="candidateInfo" select="document('candidates.xml')/candidates/candidate" /> <!-- Display the header. --> <xsl:template match="/"> <html> <!-- Page head. --> <head> <!-- Page title. --> <title>Minnesota Congressional Election Results</title> <!-- Include styles to make it look pretty. --> <link href="vwstyles.css" rel="stylesheet" type="text/css" /> </head> <!-- Page body. --> <body> <div id="wrap"> <!-- Page header. --> <header> <!-- Include the logo. --> <img src="vwlogo.png" alt="Voter Web" /> </header> <!-- Site header text. --> <h1>Minnesota Congressional Election Results</h1> <!-- Voting results --> <section id="votingResults"> <xsl:apply-templates select="congressResults/district" /> </section> </div> </body> </html> </xsl:template> <!-- Display template for each district. --> <xsl:template match="district"> <!-- District number. --> <h2>District <xsl:value-of select="@dNumber" /></h2> <!-- The candidates for each district. --> <table class="electionTable"> <thead> <!-- The header texts. --> <tr> <th>Candidate</th> <th>Votes</th> </tr> </thead> <tbody> <!-- Get the candidates information. --> <xsl:apply-templates select="candidates/candidate" /> </tbody> </table> </xsl:template> <!-- The candidate template. --> <xsl:template match="candidate"> <!-- Local variables. --> <xsl:variable name="candidateVotes" select="sum(votes)" /> <xsl:variable name="totalVotes" select="sum(..//votes)" /> <xsl:variable name="candidatePercent" select="$candidateVotes div $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($candidatePercent, '#0.0%')" />) </th> <td> <!-- Call draw cell template. --> <xsl:call-template name="drawCells"> <!-- Draw the cells. --> <xsl:with-param name="party" select="$candidateParty" /> <xsl:with-param name="cellCount" select="round(100 * $candidatePercent)" /> </xsl:call-template> </td> </tr> </xsl:template> <!-- The draw cells template. --> <xsl:template name="drawCells"> <!-- Set params. --> <xsl:param name="cellCount" /> <xsl:param name="party" /> <!-- Count the cells --> <xsl:if test="$cellCount > 0"> <td class="{$party}"></td> <!-- The cell template. --> <xsl:call-template name="drawCells"> <!-- The cell template params. --> <xsl:with-param name="cellCount" select="$cellCount - 1" /> <xsl:with-param name="party" select="$party" /> </xsl:call-template> </xsl:if> </xsl:template> <!-- End of file. --> </xsl:stylesheet> 需要sum(..//votes
  sum(..//votes)需要sum($candidateVotes) div count($totalVotes)
  $candidateVotes div $totalVotes需要format-number(candidateVotes, '###,##0')
  format-number($candidateVotes, '###,##0')需要format-number(ScandidatePercent, '#0.0%')
  format-number($candidatePercent, '#0.0%')需要100 * round($candidatePercent
  round(100 * $candidatePercent)必须为xsl:param