1

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

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

如何让两个样式表都工作?

4

2 回答 2

1

没有看到样式表就很难诊断,但我怀疑您的导入样式表和导入的样式表具有相同匹配条件或相同名称的模板,并且导入样式表已“覆盖”导入的样式表模板,从而阻止它执行。

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

您可以<xsl:apply-imports />在主样式表模板中使用为该节点应用导入的模板。

<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>

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

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

例如,如果您想从 style.xsl 应用 style1.xsl 或 style2.xsl,您可以在 style1.xsl 中使用 mode="style1" 定义所有模板(并在所有 call-template 和 apply- 中使用 mode 属性)模板)和 style2.xsl 中所有带有 mode="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>
于 2010-01-18T03:27:28.647 回答
0

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

用这个

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

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

不是这个

<xsl:template match="test/entry">
    <!-- content -->
</xsl:template>
于 2011-01-01T07:06:52.470 回答