尝试创建动态级联列表

时间:2011-03-06 17:44:36

标签: xslt symphony-cms

所以我有一个客户端的这个数据(见下文),我想创建一个可以有任意数量的层的项目符号列表。我还希望它在物品可以多次列出的方面是动态的。 CMS已经建立,因此TAG将要求其父母。无论TAG声称其父母在哪里,其子女都应该在其下面复制。不知道如何使用XSLT来解决这个问题,但是一个简单的例子最终应该看起来像这样..

<ul>
    <li>Missions</li>
    <li>
        <ul>
            <li>Kampala, Uganda</li>
            <li>Lima, Peru</li>
        </ul>
    </li>
</ul>

如果Kampala, Uganda也宣称其他内容为父母,则会多次出现。如果Kampala, Uganda有其他TAGS声称它是父母,那么这些孩子会在每个实例的Kampala, Uganda下级联。

<data>
<tags>
    <section id="3" handle="tags">Tags</section>
    <entry id="72">
        <parents>
            <item handle="meetings">Meetings</item>
        </parents>
        <tag handle="bible-studies">Bible studies</tag>
    </entry>
    <entry id="51">
        <parents>
            <item handle="missions">Missions</item>
        </parents>
        <tag handle="brazil">Brazil</tag>
    </entry>
    <entry id="31">
        <parents>
            <item handle="ministry">Ministry</item>
        </parents>
        <tag handle="childrens-ministry">Children's ministry</tag>
    </entry>
    <entry id="28">
        <parents>
            <item handle="ministry">Ministry</item>
        </parents>
        <tag handle="college-and-career-ministry">College and Career ministry</tag>
    </entry>
    <entry id="56">
        <parents>
            <item handle="the-islands-of-vanuatu">The Islands of Vanuatu</item>
        </parents>
        <tag handle="fanafo-christian-fellowship">Fanafo Christian Fellowship</tag>
    </entry>
    <entry id="29">
        <parents>
            <item handle="ministry">Ministry</item>
        </parents>
        <tag handle="high-school-ministry">High School ministry</tag>
    </entry>
    <entry id="48">
        <parents>
            <item handle="missions">Missions</item>
        </parents>
        <tag handle="holsbybrunn-sweden">Holsbybrunn, Sweden</tag>
    </entry>
    <entry id="22">
        <tag handle="home">Home</tag>
    </entry>
    <entry id="19">
        <tag handle="israel">Israel</tag>
    </entry>
    <entry id="30">
        <parents>
            <item handle="ministry">Ministry</item>
        </parents>
        <tag handle="junior-high-ministry">Junior High ministry</tag>
    </entry>
    <entry id="47">
        <parents>
            <item handle="missions">Missions</item>
        </parents>
        <tag handle="kampala-uganda">Kampala, Uganda</tag>
    </entry>
    <entry id="49">
        <parents>
            <item handle="missions">Missions</item>
        </parents>
        <tag handle="lima-peru">Lima, Peru</tag>
    </entry>
    <entry id="64">
        <parents>
            <item handle="" />
        </parents>
        <tag handle="meetings">Meetings</tag>
    </entry>
    <entry id="32">
        <parents>
            <item handle="ministry">Ministry</item>
        </parents>
        <tag handle="men-s-ministry">Men’s ministry</tag>
    </entry>
    <entry id="44">
        <parents>
            <item handle="" />
        </parents>
        <tag handle="ministry">Ministry</tag>
    </entry>
    <entry id="33">
        <tag handle="missions">Missions</tag>
    </entry>
    <entry id="54">
        <parents>
            <item handle="the-islands-of-vanuatu">The Islands of Vanuatu</item>
        </parents>
        <tag handle="natanara-christian-fellowship">Natanara Christian Fellowship</tag>
    </entry>
    <entry id="50">
        <parents>
            <item handle="missions">Missions</item>
        </parents>
        <tag handle="ouagadougou-burkina-faso">Ouagadougou, Burkina Faso</tag>
    </entry>
    <entry id="46">
        <parents>
            <item handle="missions">Missions</item>
        </parents>
        <tag handle="the-islands-of-vanuatu">The Islands of Vanuatu</tag>
    </entry>
    <entry id="77">
        <parents>
            <item handle="missions">Missions</item>
        </parents>
        <tag handle="villahermosa-mexico">Villahermosa, Mexico</tag>
    </entry>
    <entry id="27">
        <parents>
            <item handle="ministry">Ministry</item>
        </parents>
        <tag handle="womens-ministry">Women's ministry</tag>
    </entry>
    <entry id="73">
        <parents>
            <item handle="meetings">Meetings</item>
        </parents>
        <tag handle="worship">Worship</tag>
    </entry>
</tags>

2 个答案:

答案 0 :(得分:1)

修改:添加了多个父母关系。

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kTagByParent"
             match="tag"
             use="../parents/item/@handle"/>
    <xsl:template match="/" name="makeUL">
        <xsl:param name="pChilds"
                   select="*/*/entry[not(parents/item/@handle!='')]/tag"/>
        <ul>
            <xsl:apply-templates select="$pChilds"/>
        </ul>
    </xsl:template>
    <xsl:template match="tag">
        <xsl:variable name="vChilds" select="key('kTagByParent',@handle)"/>
        <li>
            <xsl:value-of select="@handle"/>
        </li>
        <xsl:if test="$vChilds">
            <li>
                <xsl:call-template name="makeUL">
                    <xsl:with-param name="pChilds" select="$vChilds"/>
                </xsl:call-template>
            </li>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出:

<ul>
    <li>home</li>
    <li>israel</li>
    <li>meetings</li>
    <li>
        <ul>
            <li>bible-studies</li>
            <li>worship</li>
        </ul>
    </li>
    <li>ministry</li>
    <li>
        <ul>
            <li>childrens-ministry</li>
            <li>college-and-career-ministry</li>
            <li>high-school-ministry</li>
            <li>junior-high-ministry</li>
            <li>men-s-ministry</li>
            <li>womens-ministry</li>
        </ul>
    </li>
    <li>missions</li>
    <li>
        <ul>
            <li>brazil</li>
            <li>holsbybrunn-sweden</li>
            <li>kampala-uganda</li>
            <li>lima-peru</li>
            <li>ouagadougou-burkina-faso</li>
            <li>the-islands-of-vanuatu</li>
            <li>
                <ul>
                    <li>fanafo-christian-fellowship</li>
                    <li>natanara-christian-fellowship</li>
                </ul>
            </li>
            <li>villahermosa-mexico</li>
        </ul>
    </li>
</ul>

使用http://see.weareinto.com/525q处的新提供的输入,输出:

<ul>
    <li>home</li>
    <li>israel</li>
    <li>meetings</li>
    <li>
        <ul>
            <li>bible-studies</li>
            <li>worship</li>
        </ul></li>
    <li>ministry</li>
    <li>
        <ul>
            <li>childrens-ministry</li>
            <li>college-and-career-ministry</li>
            <li>high-school-ministry</li>
            <li>junior-high-ministry</li>
            <li>
                <ul>
                    <li>the-islands-of-vanuatu</li>
                    <li>
                        <ul>
                            <li>fanafo-christian-fellowship</li>
                            <li>natanara-christian-fellowship</li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>men-s-ministry</li>
            <li>womens-ministry</li>
        </ul></li>
    <li>missions</li>
    <li>
        <ul>
            <li>brazil</li>
            <li>holsbybrunn-sweden</li>
            <li>kampala-uganda</li>
            <li>lima-peru</li>
            <li>ouagadougou-burkina-faso</li>
            <li>the-islands-of-vanuatu</li>
            <li>
                <ul>
                    <li>fanafo-christian-fellowship</li>
                    <li>natanara-christian-fellowship</li>
                </ul>
            </li>
            <li>villahermosa-mexico</li>
        </ul>
    </li>
</ul>

注意:节点设置键值,新表达式以获取根。

答案 1 :(得分:1)

我认为您不需要将物品的子项包裹在另一个<li>中。

此转换(非常类似于@Alejandro,但更简单,更短,根本不需要任何参数):

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="kChildren" match="tag"
               use="string(../parents/item/@handle)"/>

    <xsl:key name="kChildren" match="tag"
               use="../parents/item/@handle"/>

    <xsl:template match="/" name="buildChildrenList">
        <xsl:variable name="vChildren" select=
        "key('kChildren', string(@handle))"/>

        <xsl:if test="$vChildren">
            <ul>
                <xsl:apply-templates select="$vChildren"/>
            </ul>
        </xsl:if>
    </xsl:template>

    <xsl:template match="tag">
        <li>
            <xsl:value-of select="."/>
            <xsl:call-template name="buildChildrenList"/>
        </li>
    </xsl:template>
</xsl:stylesheet>

应用于引用的源XML文档时:

<data>
    <tags>
        <section id="3" handle="tags">Tags</section>
        <entry id="72">
            <parents>
                <item handle="meetings">Meetings</item>
            </parents>
            <tag handle="bible-studies">Bible studies</tag>
        </entry>
        <entry id="51">
            <parents>
                <item handle="missions">Missions</item>
            </parents>
            <tag handle="brazil">Brazil</tag>
        </entry>
        <entry id="31">
            <parents>
                <item handle="ministry">Ministry</item>
            </parents>
            <tag handle="childrens-ministry">Children's ministry</tag>
        </entry>
        <entry id="28">
            <parents>
                <item handle="ministry">Ministry</item>
            </parents>
            <tag handle="college-and-career-ministry">College and Career ministry</tag>
        </entry>
        <entry id="56">
            <parents>
                <item handle="the-islands-of-vanuatu">The Islands of Vanuatu</item>
            </parents>
            <tag handle="fanafo-christian-fellowship">Fanafo Christian Fellowship</tag>
        </entry>
        <entry id="29">
            <parents>
                <item handle="ministry">Ministry</item>
            </parents>
            <tag handle="high-school-ministry">High School ministry</tag>
        </entry>
        <entry id="48">
            <parents>
                <item handle="missions">Missions</item>
            </parents>
            <tag handle="holsbybrunn-sweden">Holsbybrunn, Sweden</tag>
        </entry>
        <entry id="22">
            <tag handle="home">Home</tag>
        </entry>
        <entry id="19">
            <tag handle="israel">Israel</tag>
        </entry>
        <entry id="30">
            <parents>
                <item handle="ministry">Ministry</item>
            </parents>
            <tag handle="junior-high-ministry">Junior High ministry</tag>
        </entry>
        <entry id="47">
            <parents>
                <item handle="missions">Missions</item>
            </parents>
            <tag handle="kampala-uganda">Kampala, Uganda</tag>
        </entry>
        <entry id="49">
            <parents>
                <item handle="missions">Missions</item>
            </parents>
            <tag handle="lima-peru">Lima, Peru</tag>
        </entry>
        <entry id="64">
            <parents>
                <item handle="" />
            </parents>
            <tag handle="meetings">Meetings</tag>
        </entry>
        <entry id="32">
            <parents>
                <item handle="ministry">Ministry</item>
            </parents>
            <tag handle="men-s-ministry">Men’s ministry</tag>
        </entry>
        <entry id="44">
            <parents>
                <item handle="" />
            </parents>
            <tag handle="ministry">Ministry</tag>
        </entry>
        <entry id="33">
            <parents>
                <item handle="" />
            </parents>
            <tag handle="missions">Missions</tag>
        </entry>
        <entry id="54">
            <parents>
                <item handle="the-islands-of-vanuatu">The Islands of Vanuatu</item>
            </parents>
            <tag handle="natanara-christian-fellowship">Natanara Christian Fellowship</tag>
        </entry>
        <entry id="50">
            <parents>
                <item handle="missions">Missions</item>
            </parents>
            <tag handle="ouagadougou-burkina-faso">Ouagadougou, Burkina Faso</tag>
        </entry>
        <entry id="46">
            <parents>
                <item handle="junior-high-ministry">Junior High ministry</item>
                <item handle="missions">Missions</item>
            </parents>
            <tag handle="the-islands-of-vanuatu">The Islands of Vanuatu</tag>
        </entry>
        <entry id="77">
            <parents>
                <item handle="missions">Missions</item>
            </parents>
            <tag handle="villahermosa-mexico">Villahermosa, Mexico</tag>
        </entry>
        <entry id="27">
            <parents>
                <item handle="ministry">Ministry</item>
            </parents>
            <tag handle="womens-ministry">Women's ministry</tag>
        </entry>
        <entry id="73">
            <parents>
                <item handle="meetings">Meetings</item>
            </parents>
            <tag handle="worship">Worship</tag>
        </entry>
    </tags>
</data>

会产生想要的正确结果:

<ul>
    <li>Home</li>
    <li>Israel</li>
    <li>Meetings
        <ul>
            <li>Bible studies</li>
            <li>Worship</li>
        </ul></li>
    <li>Ministry
        <ul>
            <li>Children's ministry</li>
            <li>College and Career ministry</li>
            <li>High School ministry</li>
            <li>Junior High ministry
                <ul>
                    <li>The Islands of Vanuatu
                        <ul>
                            <li>Fanafo Christian Fellowship</li>
                            <li>Natanara Christian Fellowship</li>
                        </ul></li>
                </ul></li>
            <li>Men’s ministry</li>
            <li>Women's ministry</li>
        </ul></li>
    <li>Missions
        <ul>
            <li>Brazil</li>
            <li>Holsbybrunn, Sweden</li>
            <li>Kampala, Uganda</li>
            <li>Lima, Peru</li>
            <li>Ouagadougou, Burkina Faso</li>
            <li>The Islands of Vanuatu
                <ul>
                    <li>Fanafo Christian Fellowship</li>
                    <li>Natanara Christian Fellowship</li>
                </ul></li>
            <li>Villahermosa, Mexico</li>
        </ul></li>
</ul>

请注意:当tag有多个父级时,此解决方案可以正常运行。

<强>解释

  1. 关键:
  2. <xsl:key name="kChildren" match="tag" use="string(../parents/item/@handle)"/>

    生产所有&#34;孩子&#34;来自handle兄弟的第一个(仅)item子项的parents属性的字符串值。这也涵盖了没有任何tag兄弟的parents元素(在这种情况下,键值为空字符串)。

    0.2。第二个具有相同名称的密钥:

    <xsl:key name="kChildren" match="tag"
               use="../parents/item/@handle"/>
    

    tag与其handle兄弟item子项的任何 parents属性的值相匹配。这样就可以为所有&#34;父母&#34;列出tag,而不仅仅是第一个。{/ p>

    正如我们在这里看到的,拥有多个具有相同名称的键的功能是一个非常强大且有用的功能。