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

如何将节点集合作为XSL模板参数传递?求替代人工节点方案

优化方案

方案1:直接传递属性节点集合(XSLT 1.0/2.0通用)

既然模板i的核心是遍历节点并输出其值,完全没必要手动创建<a>节点来包装属性值。可以直接把目标属性作为参数传递:

修改调用代码:

<xsl:template match="f">
  <xsl:call-template name="i">
    <xsl:with-param name="args" select="@one | @two"/>
  </xsl:call-template>
</xsl:template>

模板i同步简化(无需再选子节点,直接遍历参数本身):

<xsl:template name="i">
  <xsl:param name="args"/>
  <xsl:for-each select="$args">
    <xsl:value-of select="."/>
  </xsl:for-each>
</xsl:template>

方案2:利用XSLT 2.0+的序列特性

如果使用XSLT 2.0或更高版本,可以直接传递属性值的序列,代码更简洁:

调用代码:

<xsl:template match="f">
  <xsl:call-template name="i">
    <xsl:with-param name="args" select="@one, @two"/>
  </xsl:call-template>
</xsl:template>

模板i保持遍历逻辑即可(XSLT 2.0+支持直接遍历序列):

<xsl:template name="i">
  <xsl:param name="args"/>
  <xsl:for-each select="$args">
    <xsl:value-of select="."/>
  </xsl:for-each>
</xsl:template>

方案3:用模板匹配替代命名模板(更符合XSLT设计思想)

如果场景允许,放弃命名模板,改用模板匹配属性,代码会更贴合XSLT的模式匹配理念:

定义匹配属性的模板:

<xsl:template match="@one | @two">
  <xsl:value-of select="."/>
</xsl:template>

调用时直接应用模板:

<xsl:template match="f">
  <xsl:apply-templates select="@one | @two"/>
</xsl:template>

这种方式完全不需要手动构造节点,代码更简洁自然。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 19:45:51