如何编写一个捕获所有 XSL 模板并传递所有参数?

发布于 2024-12-26 14:17:20 字数 1310 浏览 4 评论 0原文

我正在编写一个“扩展”模板的模板,但它有一些问题。

<xsl:template match="*[@condition]" mode="#all">
<xsl:element name="condition">
  <xsl:attribute name="name">
    <xsl:value-of select="@condition"></xsl:value-of>
  </xsl:attribute>
  <xsl:apply-imports>
  </xsl:apply-imports>
</xsl:element>

问题是使用 调用的模板缺少参数。 参数列表未知,因为此模板尝试扩展许多不同的模板(因此是 mode="#all")。

有没有好的办法解决这个问题?


其他示例:

考虑两个最终模板(只读):

<xsl:template match="*" mode="mode1">
    <param name="p1"/>
</xsl:template>

<xsl:template match="*" mode="mode2">
    <param name="p2"/>
</xsl:template>

它们在某处被调用(只读):

<xsl:apply-templates mode="mode1">
    <xsl:with-param name="mode1" select="$mode1"/>
</xsl:apply-templates>

<xsl:apply-templates mode="mode2">
    <xsl:with-param name="mode2" select="$mode2"/>
</xsl:apply-templates>

可能有 100 个模式 1、模式 2、模式 3、模式 4 ...,并且名称没有模式。

我想要一个全局模板,其中包含最终模板的附加信息。类似的:

<xsl:template match="*" mode="#all">
    <xsl:next-match/>
</xsl:element>

问题是上面的全局模板没有将参数传递给模板。

I am writing a template that "extends" templates, but it has some issues.

<xsl:template match="*[@condition]" mode="#all">
<xsl:element name="condition">
  <xsl:attribute name="name">
    <xsl:value-of select="@condition"></xsl:value-of>
  </xsl:attribute>
  <xsl:apply-imports>
  </xsl:apply-imports>
</xsl:element>

The problem with this is templates called using <xsl:apply-imports> are missing params.
The list of params are not known since there are many different templates that this template is trying to extend (hence the mode="#all").

Is there a good way around this?


Additional example:

Consider two final templates (read-only):

<xsl:template match="*" mode="mode1">
    <param name="p1"/>
</xsl:template>

<xsl:template match="*" mode="mode2">
    <param name="p2"/>
</xsl:template>

they are called somewhere (read-only):

<xsl:apply-templates mode="mode1">
    <xsl:with-param name="mode1" select="$mode1"/>
</xsl:apply-templates>

<xsl:apply-templates mode="mode2">
    <xsl:with-param name="mode2" select="$mode2"/>
</xsl:apply-templates>

There might be 100s of mode1, mode2, mode3, mode4 ... and the names do not have a pattern.

I would like to have a global template that wraps additional info around the final templates. Something like:

<xsl:template match="*" mode="#all">
    <xsl:next-match/>
</xsl:element>

The problem is the above global template does not pass the params to the templates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2025-01-02 14:17:20

如果您的问题是,当您调用 时,它不包含传递给当前模板的参数,您可以使用隧道参数。在您要扩展的模板中,像这样标记您的参数:

<xsl:param name="foo" tunnel="yes"/>

此外,当您传递参数时:

<xsl:with-param name="foo" tunnel="yes"/>

我还建议使用 而不是

If your problem is that when you call <xsl:apply-imports> it doesn't include the parameters passed to the current template, you can use tunnel parameters. In the template you are extending mark your parameters like this:

<xsl:param name="foo" tunnel="yes"/>

Also, when you pass the parameters:

<xsl:with-param name="foo" tunnel="yes"/>

I also suggest using <xsl:next-match> instead of <xsl:apply-imports>.

与酒说心事 2025-01-02 14:17:20

您可以按照 Max Toro 的建议使用隧道参数,也可以只传递单个参数,其子参数是不同模板需要并识别的参数。

类似这样的

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pComposite" as="element()*">
   <param for="templateX" name="pA">
    12
   </param>
   <param for="templateY" name="pB">
    Some String
   </param>
 </xsl:param>

 <xsl:template match="*[@condition]">
  <condition name="{@condition}">
   <xsl:apply-imports>
     <xsl:with-param name="pComposite"
      select="$pComposite"/>
   </xsl:apply-imports>
  </condition>
 </xsl:template>
</xsl:stylesheet>

You can either use tunnel parameters as suggested by Max Toro, or you could just pass a single parameter, whose children are the parameters that different templates will need and will recognize.

Something like this:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pComposite" as="element()*">
   <param for="templateX" name="pA">
    12
   </param>
   <param for="templateY" name="pB">
    Some String
   </param>
 </xsl:param>

 <xsl:template match="*[@condition]">
  <condition name="{@condition}">
   <xsl:apply-imports>
     <xsl:with-param name="pComposite"
      select="$pComposite"/>
   </xsl:apply-imports>
  </condition>
 </xsl:template>
</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文