在多个地方使用一份 XML 数据

发布于 2024-08-17 19:13:11 字数 107 浏览 5 评论 0 原文

如何多次转换 XML 文档的一部分?

我正在从一个样式表中为特定节点调用模板。当我导入另一个转换相同数据的实用程序时,原始实用程序停止工作。

如何让两个样式表都起作用?

How do you transform a section of an XML document multiple times?

I'm calling a template from within one stylesheet for a particular node. When I import another utility that transforms that same data, the original stops working.

How do I get both style sheets to work?

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

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

发布评论

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

评论(2

九八野马 2024-08-24 19:13:11

在不查看样式表的情况下很难诊断,但我怀疑您的导入样式表和导入样式表具有具有相同匹配条件或相同名称和的模板导入样式表已“覆盖”导入的样式表模板,从而阻止其执行。

导入的样式表的优先级低于顶级样式表中的模板。

您可以使用 为该节点应用导入的模板。

<xsl:template match="foo">
  <!--First, turn foo into bar -->
  <bar>
    <xsl:apply-templates />
  </bar>
  <!--Now, apply the template from the imported file to do whatever it does-->
  <xsl:apply-imports />
</xsl:template>

您还可以使用模式为给定节点定义多个模板,然后在不同模式下应用模板来控制它们的执行时间。

http://www.dpawson.co.uk/xsl/sect2/modes。 html

例如,如果您想申请
style1.xsl 或 style2.xsl 来自
style.xsl,您可以定义所有
style1.xsl 中的模板
mode="style1" (并使用模式
属性也在所有调用模板中并且
apply-templates)和所有模板
style2.xsl,模式为“style2”。

然后,你可以有一个 style.xsl
样式表包含:

<xsl:include href="style1.xsl"/>
<xsl:include href="style2.xsl"/>

<xsl:template match="some pattern">
  <xsl:choose>
    <xsl:when test="some test">
      <xsl:apply-templates select="." mode="style1"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="." mode="style2"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

It is difficult to diagnose without seeing the stylesheets, but I suspect that your importing stylesheet and imported stylesheet have templates with the same match criteria or the same name and the importing stylesheet has "overridden" the imported stylesheet template, preventing it from executing.

Imported stylesheets have a lower precedence than the templates in your top level stylesheet.

You can use <xsl:apply-imports /> within your main stylesheet template to apply the imported template for that node.

<xsl:template match="foo">
  <!--First, turn foo into bar -->
  <bar>
    <xsl:apply-templates />
  </bar>
  <!--Now, apply the template from the imported file to do whatever it does-->
  <xsl:apply-imports />
</xsl:template>

You can also use mode to define multiple templates for a given node and then apply-templates in different modes to control when they are executed.

http://www.dpawson.co.uk/xsl/sect2/modes.html

For example, if you want to apply
style1.xsl or style2.xsl from
style.xsl, you could define all
templates in style1.xsl with
mode="style1" (and use the mode
attribute too in all call-template and
apply-templates) and all templates in
style2.xsl with mode="style2".

Then, you could have a style.xsl
styelsheet that contains:

<xsl:include href="style1.xsl"/>
<xsl:include href="style2.xsl"/>

<xsl:template match="some pattern">
  <xsl:choose>
    <xsl:when test="some test">
      <xsl:apply-templates select="." mode="style1"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="." mode="style2"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
梦境 2024-08-24 19:13:11

如果可能,请使用模板名称,而不是数据匹配。

使用这个

<xsl:call-template name="test" />

<xsl:template name="test">
    <!-- content -->
</xsl:template>

而不是这个

<xsl:template match="test/entry">
    <!-- content -->
</xsl:template>

If possible, use a template name, not a data match.

Use this

<xsl:call-template name="test" />

<xsl:template name="test">
    <!-- content -->
</xsl:template>

not this

<xsl:template match="test/entry">
    <!-- content -->
</xsl:template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文