【问题标题】:Passing XSLT variable from a XSL to another called by a call-template将 XSLT 变量从 XSL 传递到另一个由调用模板调用的变量
【发布时间】:2011-10-28 21:53:27
【问题描述】:

我有 2 个文件。

1->index.xsl

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

    <!-- Includes -->
    <xsl:include href="navigation.xsl" />
    <xsl:include href="head.xsl" />

    <xsl:template match="/">

    <!-- WANT TO PASS THIS VARIABLE TO navigation.xsl-->
    <xsl:variable name="value" select="1"/>
    <!-- WANT TO PASS THIS VARIABLE TO navigation.xsl-->

    <html>
        <head>
            <!-- Basic -->
            <xsl:call-template name="HtmlBasicHead"/>
            <!-- Seo -->
            <title>Impress</title>
            <meta name="description" content="..." />
            <meta name="keywords" content="..." />
        </head>

        <body>
            <div id="main">

                <xsl:call-template name="Header"/>

                <xsl:call-template name="NavigationMenu"/>

            </div>
        </body>
    </html>

    </xsl:template>

</xsl:stylesheet>

2->导航.xsl

<xsl:template name="NavigationMenu">
    <!-- NAVIGATION MENU BEGIN -->
    <div id="tray">
    <ul>
        <xsl:if test="$value='1'">
        VALUE IS 1
        </xsl:if>

        <li id="tray-active"><a href="#">Homepage</a></li>
        <li><a href="#">Live demo</a></li>
        <li><a href="#">About product</a></li>
        <li><a href="#">Testimonials</a></li>
        <li><a href="#">Download</a></li>
        <li><a href="#">Purchase</a></li>

    </ul>
    <!-- NAVIGATION MENU END -->
</xsl:template>

我想要做的是在 index.xsl 中声明变量,并且仍然在 navigation.xsl 中通过调用模板使用它,因为我收到了一个错误,比如“变量‘值’没有被声明。” ....

我这样做的原因是因为我需要指定应该突出显示的按钮。

谢谢!

【问题讨论】:

    标签: xml tags xslt xslt-1.0


    【解决方案1】:

    使用xsl:with-param

    http://www.w3schools.com/xsl/el_with-param.asp

    按照以下方式修改navigation.xsl

    <xsl:template name="NavigationMenu">
       <xsl:param name="value" />
       ...
    

    然后以这种方式从index.xsl调用它

    <xsl:call-template name="NavigationMenu">
       <xsl:with-param name="value" select="1" />
    </xsl:call-template>
    

    【讨论】:

    • 使用 with-param 代替变量 ?
    • 就像这样,我猜:&lt;xsl:with-param name="your-param-name" select="$value" /&gt;。或者那是select="{$value}",我记不得了。
    • 嘿,你救了我的命 Marian Bazalik,谢谢。但我必须承认,xslt 很好,但有时让我头疼……
    • GH - 期望 XPath 表达式(选择和测试)的属性从不使用花括号。您只需要在需要文字值的属性中使用花括号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多