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

关于XSLT中apply-templates元素参数传值与调用逻辑的疑问

Breaking Down Your XSLT apply-templates Logic

Let me walk you through this XSLT snippet step by step to untangle the call chain and parameter passing—this kind of selector and mode logic can feel opaque until you break it down piece by piece.

First, the apply-templates Core Behavior

The <xsl:apply-templates> element is XSLT’s way of saying "find the nodes matching this selector, then hand them off to the appropriate template for processing." Let’s break down your specific selector:

  • DocumentType[string(text())]: This filters all DocumentType child nodes of the current context node to only those that have non-empty text content. The string(text()) check converts the node’s text to a string; if it’s not blank (i.e., has actual text), the node is included in the selection.
  • [1]: This grabs just the first node from that filtered list—so only the very first DocumentType node with text content gets processed here.

The mode="XmlString" Purpose

The mode attribute is like a "filter" for templates. XSLT lets you have multiple templates that match the same node type (DocumentType in this case), but only the template that declares mode="XmlString" will respond to this apply-templates call. This prevents conflicts if you have other templates handling DocumentType nodes for different purposes (like formatting for HTML vs. generating raw XML strings).

Parameter Passing with xsl:with-param

The <xsl:with-param> line is explicitly passing a named parameter to the target template:

  • name="name": This defines the parameter’s name (the template will need to declare a matching <xsl:param name="name"/> to receive it).
  • select="'DocumentType'": This sets the parameter’s value to the literal string DocumentType (note the nested single quotes inside the double-quoted select attribute—this is how you define string literals in XSLT select expressions).

What the Target Template Looks Like (Example)

To use this parameter, the corresponding template would look something like this:

<xsl:template match="DocumentType" mode="XmlString">
  <!-- Declare the parameter to receive the value -->
  <xsl:param name="name"/>
  
  <!-- Use the parameter in processing—here's a common use case -->
  <xsl:element name="{$name}">
    <!-- Output the text content of the matched DocumentType node -->
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

Full Call Chain Recap

  1. Starting from the current context node (where the apply-templates is placed in your XSLT), the selector finds the first child DocumentType node with non-empty text.
  2. XSLT looks for a template that matches DocumentType and has mode="XmlString".
  3. It passes the name parameter (value: DocumentType) to that template.
  4. The template uses the parameter (via $name) along with the matched DocumentType node’s content to generate output.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:18:07