带有param的XSL for-each循环不返回任何值

时间:2016-03-31 13:26:41

标签: xml xslt

所以我得到了以下XML文档:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<couriersystem title="Courier System"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="system.xsd">
    <branches>
        <branch bid="1">
            <name>Headquarters</name>
            <address>
                58, Main Street, Edinburgh, R79 2LR
            </address>
            <manager mid="1" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="None" />
            </deliverymethods>
        </branch>
        <branch bid="2">
            <name>Convenience Local Branch</name>
            <address>
                118, Renfrew Avenue, Dumfries, J6 8CZ
            </address>
            <manager mid="2" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="Bicycle" />
                <method name="Car" />
            </deliverymethods>
        </branch>
        <branch bid="3">
            <name>Westwood Shopping Centre</name>
            <address>
                119, London Street, Nidrie, F57 8NE
            </address>
            <manager mid="3" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="Bicycle" />
            </deliverymethods>
        </branch>
        <branch bid="4">
            <name>Logistics Warehouse</name>
            <address>
                160, Main Road, Dunfermline, A15 0PO
            </address>
            <manager mid="4" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="Car" />
                <method name="Van" />
            </deliverymethods>
        </branch>
        <branch bid="5">
            <name>Kingdom of Fife Shopping Centre</name>
            <address>
                92, Central Lane, Dunfermline, U38 2OD
            </address>
            <manager mid="5" />
            <headoffice hid="7" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="Bicycle" />
                <method name="Car" />
            </deliverymethods>
        </branch>
        <branch bid="6">
            <name>Capital Branch</name>
            <address>
                95, High Street, Inverness, W91 8IW
            </address>
            <manager mid="6" />
            <headoffice hid="7" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="Car" />
                <method name="Van" />
            </deliverymethods>
        </branch>
        <branch bid="7">
            <name>Glasgow City</name>
            <address>
                94, Harris Place, BathgateGlasgow, E74 2MR
            </address>
            <manager mid="7" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="Car" />
            </deliverymethods>
        </branch>
        <branch bid="8">
            <name>Edinburgh Omni Centre</name>
            <address>
                119, West Road, Edinburgh, A79 2EG
            </address>
            <manager mid="8" />
            <headoffice hid="1" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="None" />
            </deliverymethods>
        </branch>
        <branch bid="9">
            <name>Royal Plaza</name>
            <address>
                81, Royal Plaza, Bathgate, U52 7GV
            </address>
            <manager mid="9" />
            <headoffice hid="7" />
            <!-- delivery methods -->
            <deliverymethods>
                <method name="Bicycle" />
                <method name="Car" />
            </deliverymethods>
        </branch>
    </branches>
    <employees>
        <employee eid="1">
            <nin>AZ123518D</nin>
            <firstname>Peter</firstname>
            <lastname>Smith</lastname>
            <gender>Male</gender>
            <dob>1994-02-11</dob>
            <email>ps11@gmail.com</email>
            <address>
                119, London Street, Nidrie, F57 8NE
            </address>
            <tel>07005748900</tel>
            <salary>30526</salary>
            <empbranch bid="1" />
            <supervisor sid="1" />
        </employee>
        <employee eid="2">
            <nin>CN174869F</nin>
            <firstname>Jennifer</firstname>
            <lastname>Black</lastname>
            <gender>Female</gender>
            <dob>1984-12-24</dob>
            <email>jb21@gmail.com</email>
            <address>
                161, South Road, Nidrie, W79 8WG
            </address>
            <tel>07555111222</tel>
            <salary>40576</salary>
            <empbranch bid="2" />
            <supervisor sid="1" />
        </employee>
        <employee eid="3">
            <nin>ET127654M</nin>
            <firstname>Aaron</firstname>
            <lastname>Jones</lastname>
            <gender>Male</gender>
            <dob>1968-03-15</dob>
            <email>aj31@gmail.com</email>
            <address>
                66, High Road, Yoker, Q47 4SR
            </address>
            <tel>07856471267</tel>
            <salary>30526</salary>
            <empbranch bid="3" />
            <supervisor sid="1" />
        </employee>
        <!-- snip -->
    </employees>
</couriersystem>

我正在尝试使用下面的xsl文档:

<?xml version="1.0" encoding="utf-8"?>
<!-- datetime2.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <body>
                <h1 class="title">Courier System - Branches</h1><hr /><br />
                <nav>
                    <ul>
                        <li><a href="#all">Show All</a></li>
                        <li><a href="#bymanager">By Manager</a></li>
                    </ul>
                </nav>

                <!-- show all -->
                <h2 id="all">All Branches</h2>
                <table border="1">
                    <tr bgcolor="#C0C0C0">
                        <th>ID</th>
                        <th>Branch Name</th>
                        <th>Delivery Methods</th>
                    </tr>
                    <xsl:for-each select="//branch">
                        <tr>
                            <td><xsl:value-of select="@bid" /></td>
                            <td><xsl:value-of select="address" /></td>
                            <td>
                                <xsl:for-each select="deliverymethods/method">
                                    <xsl:choose>
                                        <xsl:when test="position() = last()">
                                            <xsl:value-of select="@name" />
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="@name" />,
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
                <a class="anchor-top" href="#">Back to top</a>

                <!-- by manager -->
                <h2 id="bymanager">Show Branches by Manager</h2>
                <table border="1">
                    <tr bgcolor="#C0C0C0">
                        <th>ID</th>
                        <th>Branch Name</th>
                        <th>Delivery Methods</th>
                        <th>Manager</th>
                    </tr>
                    <xsl:for-each select="//branch">
                        <xsl:sort select="manager/@mid" order="ascending" />
                        <tr>
                            <td><xsl:value-of select="@bid" /></td>
                            <td><xsl:value-of select="address" /></td>
                            <td>
                                <xsl:for-each select="deliverymethods/method">
                                    <xsl:choose>
                                        <xsl:when test="position() = last()">
                                            <xsl:value-of select="@name" />
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:value-of select="@name" />,
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                            </td>
                            <td>
                                <xsl:call-template name="showManager">
                                    <xsl:with-param name="mid" select="manager/@mid" />
                                </xsl:call-template>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
                <a class="anchor-top" href="#">Back to top</a>
            </body>
        </html>
    </xsl:template>
    <xsl:template name="show_title" match="//employees">
        <xsl:param name="mid" />
        <xsl:for-each select="employee[@eid=$mid]">
            <xsl:value-of select="firstname" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我想要完成的是在管理器列中显示每个经理的名字: 171 。这是使用我发送的模板和参数完成的。

但是,我的XML页面在运行时只显示一个空白屏幕,因此某处出现错误,但我无法找到它。

提前致谢!

1 个答案:

答案 0 :(得分:2)

您有xsl:call-template name="showManager"xsl:template name="show_title"。我不认为需要call-template,定义一个键<xsl:key name="mid" match="employee" use="@eid"/>,然后代替call-template使用

<xsl:apply-templates select="key('mid', manager/@mid)"/>

并有一个模板

<xsl:template match="employee">
    <xsl:if test="position() > 1">, </xsl:if>
    <xsl:value-of select="firstname" />
</xsl:template>

如果您知道只有一个经理参考,那么显然您不需要进行<xsl:if test="position() > 1">, </xsl:if>检查。