首页 > 解决方案 > 使用 XSLT 如何让我的输出重复而不是只返回第一个实例?

问题描述

我正在编写一个 XSLT 脚本来输出一个包含来自 XML 文件的数据的 HTML 表,但我生成的文档只在我需要每组时才给我第一组。

这是我的 XML:

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

  <!DOCTYPE map PUBLIC "-//KPE//DTD DITA KPE Map//EN" "kpe-map.dtd" []>
<map>
  <title><ph conref="../../titles/sec_s63_title_l1.dita#sec_s63_title_l1/topic_title"/></title>
  
  <topicref href="../questions/sec_question_00260_1.dita">
    <topicsubject keyref="sec_s63_los_1"/>
  </topicref>
  
  <topicref href="../questions/sec_question_00260_2.dita">
    <topicsubject keyref="sec_s63_los_1"/>
  </topicref>
  
  <topicref href="../questions/sec_question_00260_3.dita">
    <topicsubject keyref="sec_s63_los_1"/>
  </topicref> 
</map>

这是我的 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="/">
        <html> 
            <body>
                <h2></h2>
                <table border="1">     
                        <tr>
                            <td><xsl:value-of select="//topicref/@href"/></td>
                            <td><xsl:value-of select="//topicref/topicsubject/@keyref"/></td>
                        </tr>                
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

这是我得到的输出:

<html>
   <body>
      <h2></h2>
      <table border="1">
         <tr>
            <td>../questions/sec_question_00260_1.dita</td>
            <td>sec_s63_los_1</td>
         </tr>
      </table>
   </body>
</html>

这就是我想要得到的:

<html>
   <body>
      <h2></h2>
      <table border="1">
         <tr>
            <td>../questions/sec_question_00260_1.dita</td>
            <td>sec_s63_los_1</td>
         </tr>
         <tr>
            <td>../questions/sec_question_00260_2.dita</td>
            <td>sec_s63_los_1</td>
         </tr>
         <tr>
            <td>../questions/sec_question_00260_3.dita</td>
            <td>sec_s63_los_1</td>
         </tr>
      </table>
   </body>
</html>

我的脚本在哪里关闭?提前感谢您的帮助!

标签: xslt

解决方案


我想你想要一些类似的东西

<xsl:template match="/">
    <html> 
        <body>
            <h2></h2>
            <table border="1">     
                    <xsl:apply-templates/>                
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="topicref">
    <tr>
                        <td><xsl:value-of select="@href"/></td>
                        <td><xsl:value-of select="topicsubject/@keyref"/></td>
     </tr>
</xsl:template>

推荐阅读