2017-08-24 102 views
0

我有这样的.xml数据:如何在xslt的另一个模板中调用模板?

<Check> 
    <NotfoundUser> 
     <User> 
      <Forename>Jenny</Forename> 
      <Surname>Hollands</Surname> 
      <Birthday>30.01.1985</Birthday> 
      <Status>Employee</Status> 
      <City>Los Angeles</City> 
     </User> 
     <User> 
      <Forename>Michael</Forename> 
      <Surname>Williams</Surname> 
      <Birthday>30.12.1965</Birthday> 
      <Status>Retired</Status> 
      <City>New York</City> 
     </User> 
    </NotfoundUser> 
</Check> 

我想写的.xsl数据做一个表。

<div class='div4'> 
    <table class='table4' style='font-size:12pt'> 
     <tr> 
      <th>Name</th> 
      <th>Birthday</th> 
      <th>Notice</th> 
     </tr> 
     <xsl:for-each select="/Check/NotfoundUser/*"> 
     <tr> 
      <td><xsl:value-of select="./Forename"/> <xsl:text> </xsl:text> <xsl:value-of select="Surname"/></td> 
      <td><xsl:value-of select="./Birthday"/></td> 
      <td> 
       <xsl:call-template name="replacecity"> 
       <xsl:with-param name="value" select="./City"/> 
       </xsl:call-template> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
</div> 

<!-- template to replace --> 
<xsl:template name="replacecity"> 
    <xsl:param name="value"/> 
     <xsl:choose> 
      <xsl:when test="$value = 'New York'"> 
       <xsl:text>Live in New York</xsl:text> 
      </xsl:when> 
      <xsl:when test="$value = 'Los Angeles'"> 
       <xsl:text>Live in Los Angeles</xsl:text> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$value"/> 
      </xsl:otherwise> 
     </xsl:choose> 
</xsl:template> 

我需要在这个城市上面创建一个上标。

If Status = Retired -> superscript is 1 
If Status = Employee -> superscript is 2 

所以我想创建一个新的模板(例如所谓的replacestatus)和整合内部模板replacecity,但我不知道怎么办。你们能帮我解决这个问题吗?还是你有更好的想法? enter image description here

+1

不能申报replacecity模板内replacestatus模板 - 所有模板必须是XSL的孩子:stylesheet元素;但使用replacecity模板中出现的xsl:call-template指令调用replacestatus模板没有任何问题。 –

回答

1

我不明白为什么你需要在这里调用任何其他模板。你为什么不能做简单:

XSLT 1.0

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

<xsl:template match="/Check"> 
    <table border="1"> 
     <tr> 
      <th>Name</th> 
      <th>Birthday</th> 
      <th>Notice</th> 
     </tr> 
     <xsl:for-each select="NotfoundUser/User"> 
      <tr> 
       <td> 
        <xsl:value-of select="Forename"/> 
        <xsl:text> </xsl:text> 
        <xsl:value-of select="Surname"/> 
       </td> 
       <td> 
        <xsl:value-of select="Birthday"/> 
       </td> 
       <td> 
        <xsl:text>Lives in </xsl:text> 
        <xsl:value-of select="City"/> 
        <sup> 
         <xsl:choose> 
          <xsl:when test="Status='Retired'">1</xsl:when> 
          <xsl:when test="Status='Employee'">2</xsl:when> 
         </xsl:choose> 
        </sup> 
       </td> 
      </tr> 
     </xsl:for-each> 
    </table> 
</xsl:template> 

</xsl:stylesheet> 
+0

非常感谢,您的解决方案非常简单:) – gnase

相关问题