开发者

using one piece of XML data in multiple place

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?


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>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜