父子数据上的xslt递归模板

Mic*_*sen 4 .net xml xslt recursion

我正在试图围绕xslt.这里有关于stackoverflow的一些问题有帮助( XSLT模板和递归 以及 XSLT for-each循环,基于变量的过滤器 )但我仍然有点困惑.我想我"把模板视为功能"(/sf/ask/35444391/)

无论如何...我的数据是

<Entities>
    <Entity ID="8" SortValue="0" Name="test" ParentID="0" />
    <Entity ID="14" SortValue="2" Name="test2" ParentID="8" />
    <Entity ID="16" SortValue="1" Name="test3" ParentID="8" />
    <Entity ID="17" SortValue="3" Name="test4" ParentID="14" />
    <Entity ID="18" SortValue="3" Name="test5" ParentID="0" />
</Entities>
Run Code Online (Sandbox Code Playgroud)

我想要的输出基本上是"树视图"

<ul>
    <li id="entity8">
        test
        <ul>
            <li id="entity16">
                test3
            </li>
            <li id="entity14">
                test2
                <ul>
                    <li id="entity17">
                        test4
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    <li id="entity18">
        test5
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我到目前为止的XSLT是错误的,它肯定"将模板视为函数"并在执行时抛出StackOverflowException(:-))

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="Entities">
        <ul>
            <li>
                <xsl:value-of select="local-name()"/>
                <xsl:apply-templates/>
            </li>
        </ul>
    </xsl:template>

    <xsl:template match="//Entities/Entity[@ParentID=0]">
        <xsl:call-template name="recursive">
            <xsl:with-param name="parentEntityID" select="0"></xsl:with-param>
        </xsl:call-template>
     </xsl:template>

    <xsl:template name="recursive">
        <xsl:param name="parentEntityID"></xsl:param>
        <xsl:variable name="counter" select="//Entities/Entity[@ParentID=$parentEntityID]"></xsl:variable>

        <xsl:if test="count($counter) > 0">
            <xsl:if test="$parentEntityID > 0">
            </xsl:if>
                <li>
                    <xsl:variable name="entityID" select="@ID"></xsl:variable>
                    <xsl:variable name="sortValue" select="@SortValue"></xsl:variable>
                    <xsl:variable name="name" select="@Name"></xsl:variable>
                    <xsl:variable name="parentID" select="@ParentID"></xsl:variable>                    

                    <a href=?ID={$entityID}&amp;ParentEntityID={$parentID}">
                        <xsl:value-of select="$name"/>
                    </a>

                    <xsl:call-template name="recursive">
                        <xsl:with-param name="parentEntityID" select="$entityID"></xsl:with-param>
                    </xsl:call-template>

                </li>           
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

我知道如何通过代码执行此操作,没问题.不过,这一次,我正在寻找xslt的解决方案,为此我们将非常感谢任何帮助.

Pau*_*her 6

虽然call-template命名模板是该语言非常有用的功能,但如果您发现自己更喜欢它,apply-templates则可能表明您仍然在考虑功能而不是模板.如果您在命名模板中执行的第一件事是选择要操作的节点集,则尤其如此.

这是您尝试做的简单版本.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:template match="/">
        <ul>
           <xsl:apply-templates select="Entities/Entity[@ParentID=0]" />
        </ul>
    </xsl:template>

    <xsl:template match="Entity">
        <li>
           <xsl:value-of select="@Name" />
           <xsl:apply-templates select="../Entity[@ParentID=current()/@ID]" />
        </li>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

请注意,不需要计数器,因为"父"的值提供了必要的上下文.

还要注意,所有Entities行为都以相同的方式,无论它们在树中的何处,它们都包含它们的@Name值,并将模板应用于Entity@ParentID与当前级别的@ID匹配的任何对象.

  • 我也没有在li元素中包含id属性,也没有对结果进行排序.我的观点是展示一个简单的小例子,而不是为他做所有迈克尔的工作. (3认同)