【问题标题】:Calling Template during certain conditions XSLT and C#在某些条件下调用模板 XSLT 和 C#
【发布时间】:2015-03-23 21:04:24
【问题描述】:

我正在创建一个 C# 应用程序。应用程序会将参数传递到 XSLT 文件中。我想要的是根据组合框中选择的参数调用某个模板。

我正在测试<xsl:call-template /> 方法,它只显示我的最后一个模板,而不是我正在调用的模板。我已经阅读了调用模板的作用,但仍然无法正常工作。我使用正确吗?

 <?xml version="1.0"?><!-- DWXMLSource="lab06.xml" -->
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >    

    <xsl:param name="selectedLanguage">Java</xsl:param>
    <xsl:param name="selectedUse">Application</xsl:param>
    <xsl:key name="language-by-use" match="language" use="purpose/intendedUse/@id" />
    <xsl:param name="selectedStandard">ECMA</xsl:param>
    <xsl:key name="language-by-standard" match="language" use="standards/standard/@id" />

    <xsl:template match="/">
    <xsl:call-template name="intendedUseTemp"/>
    </xsl:template>


<xsl:template match="/" name="intendedUseTemp" >
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Intended Use</th>
                    <th>Languages</th>
                </tr>
                <tr>
                    <td>
                        <xsl:value-of select="$selectedUse"/>
                    </td>
                    <td>
                        <xsl:for-each select="key('language-by-use', programming/purpose/intendedUse[. =$selectedUse]/@id)" >
                            <xsl:value-of select="name" />
                            <br />
                        </xsl:for-each>
                    </td>

                </tr>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="/" name="standardTemp" >
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Standard</th>
                    <th>Languages</th>
                </tr>
                <tr>
                    <td>
                        <xsl:value-of select="$selectedStandard"/>
                    </td>
                    <td>
                        <xsl:for-each select="key('language-by-standard', programming/standards/standard[.=$selectedStandard]/@id)" >
                            <xsl:value-of select="name" />
                            <br />
                        </xsl:for-each>
                    </td>
                </tr>
            </table>
        </body>
    </html>
</xsl:template>



    <xsl:template match="/" name="languageTemp">
        <html>

            <body>
                <table border="1">
                    <tr>
                        <th >Programming</th>
                        <th >Intended Use</th>
                        <th style="text-align:left">Standards</th>
                    </tr>


                    <xsl:for-each select="programming/languages/language[name=$selectedLanguage]">
                        <tr>
                            <td><xsl:value-of select="name"/></td>
                            <td><xsl:variable name="intendedUseID" select="purpose/intendedUse/@id" />
                                <xsl:for-each select="/programming/purpose/intendedUse[@id=$intendedUseID]" >
                                    <xsl:value-of select="." /> <br  />
                                </xsl:for-each></td>
                            <td>   <xsl:variable name="standardID" select="standards/standard/@id" />
                                <xsl:for-each select="/programming/standards/standard[@id=$standardID]" >
                                    <xsl:value-of select="." /> <br />
                                </xsl:for-each></td>
                        </tr>
                    </xsl:for-each>
                </table>

            </body>
        </html>
    </xsl:template>


</xsl:stylesheet>

更新:

我正在尝试将参数传递给调用模板,但它不起作用。我是否混淆了参数和调用模板的使用?我可以将它传递给变量吗?我的计划是使用&lt;xsl:if&gt; 将参数从我的 C# 应用程序传递到 XSLT

<xsl:param name="selectedLanguage">Java</xsl:param>
<xsl:param name="selectedUse">Application</xsl:param>
<xsl:param name="selectedStandard">ECMA</xsl:param>
<xsl:param name="selectedBox">languageTemp</xsl:param>

<xsl:key name="language-by-use" match="language" use="purpose/intendedUse/@id" />
<xsl:key name="language-by-standard" match="language" use="standards/standard/@id" />



<xsl:template match="/">
    <xsl:call-template name="$selectedBox"/>
</xsl:template>

【问题讨论】:

    标签: c# xml winforms xslt


    【解决方案1】:

    您发现了 XSLT 的一个奇怪的语义“特性”。通常,xsl:template 将具有match 属性或name 属性以指示何时应用它。这种情况很少见,但它可以同时具备,但这意味着任一namematch 都可以触发应用程序。

    就您而言,每个模板都有match="/"。那么,在输入时,XSLT 如何决定选择哪个模板?您希望它使用第一个,但它们都具有相同的优先级,因此 XSLT 中的平局规则是使用 last 遇到的一个。

    更新: 关于您更新的问题。是的,您可以将参数传递给命名模板,如下所示:

    <!-- the named template -->
    <xsl:template name="xyzzy">
        <xsl:param name="plugh"/>
        <!-- do something here with $plugh -->
    </xsl:template>
    
    <xsl:template match="some-node">
        <-- calling the template named xyzzy -->
        <xsl:call-template name="xyzzy">
            <xsl:with-param name="plugh" select="child-node"/>
        </xsl:call-template>
    </xsl:template>
    

    【讨论】:

    • 根据w3.org/TR/xslt#conflict 的 W3C 规范,在这种情况下,XSLT 实际上可能会发出错误信号而不是选择最后一个模板,因此最好避免使用具有相同优先级的模板。跨度>
    • @TimC - 非常正确,我应该提到这一点。
    • @bjimba 谢谢。删除火柴似乎有效。另一个问题,我正在尝试将参数传递给 ,但它一直出错,我已将代码包含在我的更新中,我是否混淆了参数和调用模板的使用?可以传入变量吗?
    猜你喜欢
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多