XSLT,我正在学习我自己的XSLT示例,但它不能像我预期的那样工作

时间:2016-05-07 11:54:33

标签: xslt

首先,我的代码是..

<?xml version="1.0" encoding="UTF-8"?>
<mpml>
   <problem>
      <context>
         <p>두 다항식 $A=x^2 - xy + 2y^2$, $B=3x^2 + 2xy - y^2$에 대하여 $A-B$를 계산한 식이 $ax^2 +bxy + cy^2$일 때, 상수 $a+b+c$의 값은?</p>
      </context>
      <answerlist>
         <i>-4</i>
         <i>-2</i>
         <i>0</i>
         <i>2</i>
         <i>4</i>
      </answerlist>
   </problem>

   <problem>
      <context>
         <p>연립방정식 $\begin{cases} x+y+z=30 \\ 2x+3y+4z=93 \\ y=z+3 \end{cases}$의 해를 $x=a$, $y=b$, $z=c$라 할 때, $a-2b+3c$의 값은? (단, $a$, $b$, $c$는 실수.)</p>
      </context>
      <answerlist>
         <i>7</i>
         <i>9</i>
         <i>11</i>
         <i>13</i>
         <i>15</i>
      </answerlist>
   </problem>
</mpml>

它的xsl代码是..

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

   <xsl:template match="/">
   <h2> Testing.. </h2>
   <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="problem">
      <div class="problem">
      <xsl:apply-templates select="context"/>
      <xsl:apply-templates select="answerlist"/>
      </div>
   </xsl:template>

   <xsl:template match="context">
      <div class="context">

      </div>
   </xsl:template>

   <xsl:template match="answerlist">
      <div class="answerlist">
         test answerlist
      </div>
   </xsl:template>

</xsl:transform>

在XSLT Tryit编辑器中(感谢w3school),上面的XSLT按照我的预期工作。但是当我在我的服务器上尝试这个时,文档末尾会有一个“额外的内容”。除了第一个模板外,显示的错误和错误。

1 个答案:

答案 0 :(得分:0)

我认为问题在于您的XSLT不会在输出中生成格式良好的XML:在模板<xsl:template match="/">中,您应该将内容包装在单个根标记中。

E.g:

   <xsl:template match="/">
     <body>
       <h2> Testing.. </h2>
       <xsl:apply-templates/>
     </body>
   </xsl:template>