应用所有模板

时间:2017-08-15 09:37:50

标签: .net xslt xslt-1.0

我的主要xslt文件导入多个其他xslt文件,那些xslt文件也可能导入/包含0,1或多个xslt文件(也可能有更多级别)

我想从主xslt文件中调用匹配特定模式的所有导入/包含的模板(它们具有相同的名称,相同的模式,相同的匹配或其他内容)。< / p>

我希望能够在没有硬编码到特定导入列表的情况下执行此操作(例如,如果添加新导入,则应自动拾取)

或者,提取具有给定名称的变量的值。

在任何一种情况下,结果都应该连接在一起形成一个节点集。 结果的顺序并不重要,包装元素是可选的(但可取)

这可能吗?

示例输入:

Main.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="File1.xslt"/>
  <!-- Some rules here, including the solution -->
</xsl:style>

File1.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="File2.xslt"/>
  <!-- Some unrelated rules here -->
  <xsl:template name="Things">
    <!-- name could be mode or matches, or the template could be a variable instead -->
    <Something/>
  </xsl:template>
</xsl:style>

File2.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Some unrelated rules here -->
  <xsl:template name="Things">
    <!-- name could be mode or matches, or the template could be a variable instead -->
    <SomethingElse/>
  </xsl:template>
</xsl:style>

必需的输出:

<xml>
  <Something/>
  <SomethingElse/>
</xml>

N.B。在我的场景中,所需模板/变量的内容将是静态的,尽管有一个可以包含xslt的解决方案会很好。

1 个答案:

答案 0 :(得分:0)

我想出了一个稍微有点蠢的方法来做到这一点。

<xsl:template name="EntryPoint">
  <xml>
    <xsl:apply-templates select="document('')/xsl:stylesheet/xsl:import/@href" mode="FindThings"/>
  </xml>
</xsl:template>
<xsl:template mode="FindThings" match="/xsl:stylesheet">
  <xsl:apply-templates select="document(xsl:import/@href)" mode="FindThings"/>
  <xsl:apply-templates select="xsl:variable[@name='Things']" mode="FindThings"/>
</xsl:template>
<xsl:template mode="FindThings" match="xsl:variable[@name='Things']">
  <xsl:copy-of select="*"/>
</xsl:template>
<xsl:template mode="FindThings" match="xsl:import/@href">
  <xsl:apply-templates select="document(.)" mode="FindThings"/>
</xsl:template>

它输出所有顶级变量的内容,称为&#34; Things&#34;在每个导入的xslt文件中,无论导入的深度如何。