1

所以我有一个客户的这些数据(见下文),我想创建一个可以有任意数量层的项目符号列表。我还希望它在项目可以被多次列出的方面是动态的。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>

4

2 回答 2

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>

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

于 2011-03-06T19:40:25.087 回答
1

我认为您不需要将 item 的子项包裹在另一个 item 中<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>

请注意:当 atag有多个父级时,此解决方案可以正常工作。

说明

  1. 钥匙:

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

从其兄弟姐妹的第一个(唯一)孩子的handle属性的字符串值中生成所有“孩子” 。这也涵盖了没有任何兄弟元素的元素(在这种情况下,键值是空字符串)。itemparentstagparents

.2. 具有相同名称的第二个键:

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

通过其兄弟姐妹的孩子的任何 属性tag的值匹配 a 。这使得可以列出所有“父母”,而不仅仅是第一个。 handleitemparentstag

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

于 2011-03-06T20:56:46.047 回答