在 xslt 中创建内部 link 以在单击 link 时移动到特定位置

create internal links in xslt to move to a particular location when clicked on link

我有一个 xml 具有以下数据

<Report>
<Reference Num="1">
<Error>xyz</Error>
</Reference>
<Reference Num="2">
<Error>abc</Error>
</Reference>
<Reference Num="3">
<Error>pqr</Error>
</Reference>
</Report>

在上面的例子中考虑了 1000 个引用,这实际上是我的情况

我创建了一个 xsl 以 table 的形式顺序显示它们。

<xsl:template match="/">
    <html>
      <body >
        <table>     
            <xsl:call-template name="ReferenceList">
            </xsl:call-template>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="ReferenceList">
    <xsl:for-each select="/Report/Reference">
        <br></br>
        <tr style="background-color: #000066; color: white;">
            <td  style="text-align:center;"> Reference </td>
            <td>ERROR</td>
        </tr>
        <tr>
            <td>
                Reference<xsl:value-of select="@Num"></xsl:value-of>
            </td>
            <td>
                <xsl:value-of select="ERROR"></xsl:value-of>
            </td>
        </tr>
    </xsl:for-each>
  </xsl:template> 

我想在页面顶部创建名称为 ref1、ref2、ref3……的链接,以便在单击它们时跳转到浏览器中看到的特定引用位置。

我想你只是想使用一种模式来处理你的节点两次:

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

   <xsl:template match="/">
    <html>
      <body >
        <div class="reference">
          <ul>
            <xsl:apply-templates select="Report/Reference" mode="links"/>
          </ul>
        </div>
        <table> 
            <thead>
              <tr style="background-color: #000066; color: white;">
                <th > Reference </th>
                <th>ERROR</th>
               </tr>
            </thead>
            <tbody>    
            <xsl:apply-templates select="Report/Reference"/>
            </tbody>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Report/Reference">
        <tr id="ref{position()}">
            <td>
                <xsl:text>Reference</xsl:text>
                <xsl:value-of select="@Num"/>
            </td>
            <td>
                <xsl:value-of select="Error"/>
            </td>
        </tr>
  </xsl:template> 

  <xsl:template match="Report/Reference" mode="links">
    <li>
      <a href="#ref{position()}">Ref<xsl:value-of select="position()"/></a>
    </li>
  </xsl:template>

</xsl:transform>

渲染HTML输出

显然你不必使用列表来构建你的链接,但我建议以某种方式构建它们,然后将 CSS 设置为 needed/wanted.