简体   繁体   English

如何在xslt中的另一个模板中调用一个模板?

[英]How to call a template in another template in xslt?

I have an .xml data like this: 我有一个.xml数据,如下所示:

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

I am trying to write .xsl data to make a table. 我正在尝试编写.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>

I need to create a superscript above city like this. 我需要像这样在城市上方创建一个上标。

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

So I am thinking to create a new template (eg called replacestatus ) and integrate inside template replacecity , but I don't know how. 因此,我正在考虑创建一个新模板(例如,称为replacestatus )并集成到模板replacecity ,但是我不知道如何。 Could you guys help me about this, or do you have a better idea for that? 你们可以帮我一下吗,或者您有个更好的主意吗? 在此处输入图片说明

I don't see why you need to call any additional templates here. 我不明白为什么您需要在这里调用任何其他模板。 Why can't you do simply: 您为什么不能简单地做:

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

Try using xsl:include or xsl:import 尝试使用xsl:include或xsl:import

Here are some examples: 这里有些例子:

http://www.xml.com/pub/2000/11/01/xslt/index.html http://www.xml.com/pub/2000/11/01/xslt/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM