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

如何编写XSLT让处理器计算元素位置时忽略注释?

XSLT忽略注释计算元素位置的解决方法

问题

需要编写XSLT,让处理器在计算元素位置时忽略注释。现有代码虽然匹配注释并空输出,但最后一个<li>后的注释仍会干扰position()和last()的判断,导致输出末尾多出一个逗号。

XML输入

<?xml version="1.0" encoding="utf-8"?>                                                                                                                                       
<html xmlns="http://www.w3.org/1999/xhtml">                                                                                                                                  
  <ul>                                                                                                                                                                       
    <li>fish1</li>                                                                                                                                                           
    <li>fish2</li>                                                                                                                                                           
    <li>fish3</li>                                                                                                                                                           
    <li>fish4</li>                                                                                                                                                           
    <!--                                                                                                                                                                     
      <li>fish5</li>                                                                                                                                                         
    -->                                                                                                                                                                      
  </ul>                                                                                                                                                                      
</html>

现有XSLT代码

<?xml version="1.0" encoding="utf-8"?>                                                                                                                                       
<xsl:stylesheet                                                                                                                                                              
  version="2.0"                                                                                                                                                              
  xmlns:xhtml="http://www.w3.org/1999/xhtml"                                                                                                                                 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">                                                                                                                          
                                                                                                                                                                             
  <xsl:output method="text" encoding="utf-8" indent="no" />                                                                                                                  
                                                                                                                                                                             
  <xsl:strip-space elements="*" />                                                                                                                                           
                                                                                                                                                                             
  <xsl:template match="comment()" />                                                                                                                                         
                                                                                 
  <xsl:template match="/">                                                                                                                                                   
    <xsl:apply-templates />                                                                                                                                                  
  </xsl:template>
                                                                                                                                                                                                                                                                                                                                        
  <xsl:template match="xhtml:ul">                                                                                                                                            
    <xsl:apply-templates />                                                                                                                                                  
  </xsl:template>                                                                                                                                                            
                                                                                                                                                                             
  <xsl:template match="xhtml:li">                                                                                                                                            
    <xsl:apply-templates />                                                                                                                                                  
    <xsl:if test="not(position()=last())">                                                                                                                                   
      <xsl:text>, </xsl:text>                                                                                                                                                
    </xsl:if>                                                                                                                                                                
  </xsl:template>                                                                                                                                                            
                                                                                                                                                                             
</xsl:stylesheet>                        

当前错误输出(SaxonJ-HE 12.1)

fish1, fish2, fish3, fish4, 

解决方案

方法一:限定处理的节点集合(推荐)

修改xhtml:ul的模板,明确只选择所有<li>元素进行处理,注释不会被纳入处理流程,position()和last()仅基于<li>计算:

<xsl:template match="xhtml:ul">
  <xsl:apply-templates select="xhtml:li" />
</xsl:template>

方法二:在位置判断中排除注释干扰

如果不想修改<ul>的模板逻辑,可以在<li>的判断中,直接对比当前节点是否为父节点下的最后一个<li>:

<xsl:template match="xhtml:li">
  <xsl:apply-templates />
  <xsl:if test="not(generate-id() = generate-id(parent::xhtml:ul/xhtml:li[last()]))">
    <xsl:text>, </xsl:text>
  </xsl:if>
</xsl:template>

修正后输出

调整后运行XSLT,输出会变为:

fish1, fish2, fish3, fish4

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 18:53:18