2

XML:

<Root>
  <Elements>
    <Element>el1</Element>
    <Element>el2</Element>
   </Elements>

  <Elements>
    <Element>el1</Element>
    <Element>el2</Element>
   </Elements>
</Root>

尝试生成为同一元素应用两个不同的模板。

主模板:

<xsl:stylesheet version="1.0">
      <xsl:template match="/Root">
           At root level
             <xsl:apply-templates select="Elements">

             <h1>Render something more</h1>

             <xsl:apply-templates select="Elements" mode="1:Custom">
        </xsl:template>


    <!-- This doesn't render though it is called above-->
      <xsl:template match="Elements"> 
      render something here
      </xsl:template>

    <!-- This renders twice -->
      <xsl:template match="Elements" mode="1:Custom">
      render something else here
      </xsl:template>
</xsl:stylesheet>

如果我将模式添加到第一个模板,两者都不会呈现。

也试过:

 <xsl:apply-templates select="Elements" mode="1:Custom" />

使用不同的模板应用为:

<xsl:apply-templates select="Elements" mode="Different" />

两者中只有一个(呈现具有指定模式的第一个)。IE

<xsl:template match="Elements">
</xsl:template>

不渲染

<xsl:template match="Elements" mode="Different" />渲染两次。

我应该如何解决这个问题?在我研究的任何地方,它都建议优先考虑模式。这么多程序员都使用它,一定很简单吗?

4

2 回答 2

5
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/Root">
           At root level
             <xsl:apply-templates select="Elements"/>

             <h1>After first template</h1>

             <xsl:apply-templates select="Elements" mode="Custom"/>
        </xsl:template>

      <xsl:template match="Elements">
      <p>First template</p> 
          <xsl:apply-templates select="Element"/>
      </xsl:template>

      <xsl:template match="Elements" mode="Custom">
         <p>Second template      </p>
      </xsl:template>
      </xsl:stylesheet>
于 2011-06-10T16:34:35.660 回答
5
<xsl:template match="Elements" mode="1:Custom">

您在这里使用了语法上非法的模式名称(必须是QName)并且任何兼容的 XSLT 处理器都必须发出错误。

解决方案:只需更改

    mode="1:Custom"

    mode="Custom"

因此,这种转换是正确的

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/Root">
       At root level
     <xsl:apply-templates select="Elements"/>
     <h1>Render something more</h1>

     <xsl:apply-templates select="Elements" mode="Custom"/>
    </xsl:template>

    <xsl:template match="Elements">
       render something here

    </xsl:template>

    <xsl:template match="Elements" mode="Custom">

     render something else here
   </xsl:template>

   <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Root>
    <Elements>
        <Element>el1</Element>
        <Element>el2</Element>
    </Elements>
    <Elements>
        <Element>el1</Element>
        <Element>el2</Element>
    </Elements>
</Root>

产生了想要的正确结果:

       At root level

   render something here


   render something here

<h1>Render something more</h1>

 render something else here


 render something else here
于 2011-06-11T03:30:49.853 回答