You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT模板仅显示‘Author:’无作者名,表格展示异常求助

解决XML按作者分组后作者名不显示的问题

Hey there! Let's fix that missing author name issue in your grouped catalog table. The core problem here is XSLT version compatibility—most web browsers only support XSLT 1.0 natively, but your stylesheet uses XSLT 2.0-specific syntax (xsl:for-each-group and current-grouping-key()) that won't work in those environments.

问题诊断

Your XSLT logic is correct for XSLT 2.0, but if you're testing this in a browser (like Chrome, Firefox, or Edge), the built-in processor can't recognize current-grouping-key(), so it returns an empty value—hence why you only see "Author:" without the name.

解决方案

We have two paths depending on where you're running the transformation:

方案1:使用XSLT 2.0处理器(服务器端或本地工具)

If you're processing the XML server-side (e.g., with Java's Saxon, .NET's Saxon-HE, or command-line tools) or using a desktop XML editor that supports XSLT 2.0, your original code actually works perfectly. Just ensure your processor is configured to use XSLT 2.0.

方案2:兼容XSLT 1.0(浏览器端友好)

For browser-based transformations, we need to use the Muenchian Method—the standard way to group elements in XSLT 1.0. Here's the corrected stylesheet that will display author names and their books properly:

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

<!-- Define a key to group books by their author -->
<xsl:key name="books-by-author" match="book" use="author"/>

<xsl:template match="catalog">
<html>
<head>
<title>Books Catalog by Author</title>
<style>
  .author-header { background-color: #ef9f37; }
  table { border-collapse: collapse; width: 100%; }
  th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
</style>
</head>
<body>
<h1>Books Catalog by Author</h1>
<table CELLPADDING="5">
  <!-- Loop through unique authors (using Muenchian grouping) -->
  <xsl:for-each select="book[generate-id() = generate-id(key('books-by-author', author)[1])]">
    <!-- Sort authors alphabetically -->
    <xsl:sort select="author"/>
    
    <!-- Author header row -->
    <tr class="author-header">
      <td colspan="5">Author: <xsl:value-of select="author"/></td>
    </tr>
    
    <!-- Loop through all books by this author -->
    <xsl:for-each select="key('books-by-author', author)">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="genre"/></td>
        <td>$<xsl:value-of select="price"/></td>
        <td><xsl:value-of select="publish_date"/></td>
        <td><xsl:value-of select="description"/></td>
      </tr>
    </xsl:for-each>
  </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

代码说明

  • Muenchian Key: <xsl:key> creates an index of books grouped by their author value, making it easy to retrieve all books for a given author.
  • Unique Authors: The for-each selects only the first occurrence of each author using generate-id()—this ensures we don't repeat author headers.
  • Sorting: The xsl:sort ensures authors are displayed in alphabetical order.
  • Book Details: Added rows to display each book's title, genre, price, publish date, and description (you can adjust the columns as needed).

测试结果

When you apply this stylesheet to your XML, you'll see:

  1. An alphabetically sorted list of author headers in orange.
  2. Each author's books listed below their header in a clean table format.

内容的提问来源于stack exchange,提问作者Josh Gomez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:58:09