You need to enable JavaScript to run this app. 如何使用XSLT 1.0解析多级编号XML生成嵌套列表并输出HTML格式
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSLT 1.0解析多级编号XML生成嵌套列表并输出HTML格式

XSLT 1.0 多级别嵌套条目拆分实现方案

核心实现规则

  • 层级匹配与边距对应关系完全对齐要求:
    • 顶级条目(A.、B.、C. 开头):左边距0px
    • 二级条目(1.、2.、3.、4. 开头):左边距10px
    • 三级条目(a)、b) 开头):左边距20px
    • 四级条目(1)、2) 开头):左边距30px
  • 拆分逻辑:对每个节点内部的
    标签做分隔识别,将混在单个节点内的多段条目拆分为独立

    段落

  • 自动过滤拆分过程中产生的空白文本,避免生成无意义空段落

完整可运行代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output indent="yes" method="xml" omit-xml-declaration="no"/>
  
  <!-- 递归拆分<BR>分隔的内容为独立段落 -->
  <xsl:template name="split-by-br">
    <xsl:param name="content" select="."/>
    <xsl:choose>
      <!-- 当前片段无内嵌<BR>时,直接进入层级判定生成段落 -->
      <xsl:when test="not($content/BR)">
        <xsl:if test="normalize-space($content)">
          <xsl:call-template name="get-level-margin">
            <xsl:with-param name="text-content" select="normalize-space($content)"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:when>
      <!-- 识别第一个<BR>位置,拆分前后两部分递归处理 -->
      <xsl:otherwise>
        <xsl:variable name="first-part" select="$content/text()[not(following-sibling::BR)][1]"/>
        <xsl:if test="normalize-space($first-part)">
          <xsl:call-template name="get-level-margin">
            <xsl:with-param name="text-content" select="normalize-space($first-part)"/>
          </xsl:call-template>
        </xsl:if>
        <!-- 处理<BR>之后的剩余内容片段 -->
        <xsl:call-template name="split-by-br">
          <xsl:with-param name="content" select="$content/following-sibling::node()[preceding-sibling::BR[1]]"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- 根据文本开头的编号判定层级,生成带对应边距样式的段落 -->
  <xsl:template name="get-level-margin">
    <xsl:param name="text-content"/>
    <xsl:variable name="margin-left">
      <xsl:choose>
        <!-- 顶级条目 -->
        <xsl:when test="starts-with($text-content, 'A. ') or starts-with($text-content, 'B. ') or starts-with($text-content, 'C. ')">0px</xsl:when>
        <!-- 二级条目 -->
        <xsl:when test="starts-with($text-content, '1. ') or starts-with($text-content, '2. ') or starts-with($text-content, '3. ') or starts-with($text-content, '4. ')">10px</xsl:when>
        <!-- 三级条目 -->
        <xsl:when test="starts-with($text-content, 'a) ') or starts-with($text-content, 'b) ')">20px</xsl:when>
        <!-- 四级条目 -->
        <xsl:when test="starts-with($text-content, '1) ') or starts-with($text-content, '2) ')">30px</xsl:when>
        <!-- 无明确编号的子项,默认沿用四级边距 -->
        <xsl:otherwise>30px</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <P STYLE="margin-left:{$margin-left}">
      <xsl:value-of select="$text-content"/>
    </P>
  </xsl:template>

  <!-- 匹配根节点生成最终输出结构 -->
  <xsl:template match="/CONTENT_LIST">
    <CONTENT_LIST>
      <xsl:for-each select="CONTENT">
        <xsl:call-template name="split-by-br"/>
      </xsl:for-each>
    </CONTENT_LIST>
  </xsl:template>
</xsl:stylesheet>

效果对齐说明

  • 输出结构完全匹配示例要求的<CONTENT_LIST>包裹

    标签的格式

  • 拆分后的段落边距严格对应层级,在浏览器中展示时可形成清晰的嵌套缩进效果
  • 原XML中混在单个里的多段内容(比如带多个
    的报告点坐标)会被拆分为独立段落,不会出现文字粘连的问题

内容的提问来源于stack exchange,提问作者Umaima Fatima

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.25 10:24:02