如何使用XSLT基于属性对所有节点进行分组

时间:2011-11-30 10:36:03

标签: xml xslt xslt-1.0

我得到了以下给定的XML输出:

    <?xml version="1.0"?>
    <navigation path="/english">
      <resources>
        <Copyright>© 2009 mysite. All Rights Reserved.</Copyright>
      </resources>
      <node title="010. Terms and Conditions" id="tcm:233-242834-64" url="termscondition.aspx" compTitle="Terms and Conditions"/>
        <node title="Root" id="tcm:233-38288-4" url="/">
            <node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/english/Images/skywards_on_tcm233-191728.gif" imageSrcOff="/english/Images/skywards_off_tcm233-191727.gif" imageSrcSelected="/english/Images/skywards_sel_tcm233-191730.gif" imageSrcSelectedOn="/english/Images/skywards_on2_tcm233-191729.gif" accessKey="S" Group="SB">   
                <node title="010. Membership Tiers" id="tcm:481-38882-4" url="/Skywards/membership_tiers/membership_tiers.aspx" indexpage="tcm:481-194317-64" compTitle="Membership Tiers"/>
                <node title="020. Earning Miles" id="tcm:481-38883-4" url="/Skywards/earning_miles/earning_miles.aspx" indexpage="tcm:481-194318-64" compTitle="Earning Skywards Miles"/>
                <node title="030. Travel Rewards" id="tcm:481-38884-4" url="/Skywards/travel_rewards/travel_rewards.aspx" indexpage="tcm:481-194320-64" compTitle="Travel Rewards"/>                 
            </node>
            <node title="H030. Search" id="tcm:233-38483-4" url="/search/search.aspx" indexpage="tcm:233-214259-64" compTitle="Search" localTitle="Search" accessKey="3" Group="SB"/>
            <node title="H010. Homepage" id="tcm:233-192367-64" url="/index.aspx" localTitle="Home" accessKey="0" Group="Home"/>
            <node title="H020. Skyward Homepage" id="tcm:233-143367-64" url="/index.aspx" localTitle="Home" accessKey="0" Group="Home"/>
        </node>
        <node title="footer">
            <node title="F010. 000. Sitemap" id="tcm:233-192374-64" url="/sitetools/sitemap.aspx" accessKey="4" compTitle="Sitemap"/>
            <node title="F060. Accessibility" id="tcm:233-192369-64" url="/sitetools/accessibility.aspx" compTitle="Accessibility Statement"/>
            <node title="F030. Operational Updates" id="tcm:233-192370-64" url="/sitetools/operational_updates.aspx" compTitle="Operational Updates"/>
            <node title="F050. Privacy Policy" id="tcm:233-192371-64" url="/sitetools/privacy_policy.aspx" compTitle="Privacy Policy"/>
            <node title="F040. Terms &amp; Conditions" id="tcm:233-192449-64" url="/sitetools/terms_and_conditions.aspx" compTitle="Terms &amp; Conditions"/>
        </node>
    </navigation>

现在想编写一个xslt,它将基于属性“Group”的节点与它们的值进行分组,并将它们添加到单独的节点中,以便我的输出如下所示:

<?xml version="1.0"?>
<navigation path="/english">
  <resources>
    <Copyright>© 2009 mysite. All Rights Reserved.</Copyright>
  </resources>
  <node title="010. Terms and Conditions" id="tcm:233-242834-64" url="termscondition.aspx" compTitle="Terms and Conditions"/>
    <node title="Root" id="tcm:233-38288-4" url="/">
        <node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/english/Images/skywards_on_tcm233-191728.gif" imageSrcOff="/english/Images/skywards_off_tcm233-191727.gif" imageSrcSelected="/english/Images/skywards_sel_tcm233-191730.gif" imageSrcSelectedOn="/english/Images/skywards_on2_tcm233-191729.gif" accessKey="S" Group="SB">   
        </node>
        <node title="H030. Search" id="tcm:233-38483-4" url="/search/search.aspx" indexpage="tcm:233-214259-64" compTitle="Search" localTitle="Search" accessKey="3" Group="SB"/>
        <node title="H010. Homepage" id="tcm:233-192367-64" url="/index.aspx" localTitle="Home" accessKey="0" Group="Home"/>
        <node title="H020. Skyward Homepage" id="tcm:233-143367-64" url="/index.aspx" localTitle="Home" accessKey="0" Group="Home"/>
    </node>
    <node title="footer">
        <node title="F010. 000. Sitemap" id="tcm:233-192374-64" url="/sitetools/sitemap.aspx" accessKey="4" compTitle="Sitemap"/>
        <node title="F060. Accessibility" id="tcm:233-192369-64" url="/sitetools/accessibility.aspx" compTitle="Accessibility Statement"/>
        <node title="F030. Operational Updates" id="tcm:233-192370-64" url="/sitetools/operational_updates.aspx" compTitle="Operational Updates"/>
        <node title="F050. Privacy Policy" id="tcm:233-192371-64" url="/sitetools/privacy_policy.aspx" compTitle="Privacy Policy"/>
        <node title="F040. Terms &amp; Conditions" id="tcm:233-192449-64" url="/sitetools/terms_and_conditions.aspx" compTitle="Terms &amp; Conditions"/>
    </node>
    <node GroupTitle="SB">
        <nodes>
            <node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/english/Images/skywards_on_tcm233-191728.gif" imageSrcOff="/english/Images/skywards_off_tcm233-191727.gif" imageSrcSelected="/english/Images/skywards_sel_tcm233-191730.gif" imageSrcSelectedOn="/english/Images/skywards_on2_tcm233-191729.gif" accessKey="S" Group="SB">   
            </node>
            <node title="H030. Search" id="tcm:233-38483-4" url="/search/search.aspx" indexpage="tcm:233-214259-64" compTitle="Search" localTitle="Search" accessKey="3" Group="SB"/>
        </nodes> 
    </node>   
    <node GroupTitle="Home">
        <nodes>
            <node title="H010. Homepage" id="tcm:233-192367-64" url="/index.aspx" localTitle="Home" accessKey="0" Group="Home"/>
            <node title="H020. Skyward Homepage" id="tcm:233-143367-64" url="/index.aspx" localTitle="Home" accessKey="0" Group="Home"/>
        </nodes> 
    </node>     
</navigation>

请建议!!

1 个答案:

答案 0 :(得分:0)

这个样式表可以解决这个问题:

<?xml version="1.0" encoding="UTF-8"?>
<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"/>

    <!-- Key for Group attributes -->
    <xsl:key name="nodesByGroups" match="node[@Group]" use="@Group" />

    <xsl:template match="/*">

        <!-- Making shallow copy of root element -->
        <xsl:copy>

            <!-- Making deep copy of all child elements, as well as any root element attributes -->
            <xsl:copy-of select="*|@*" />

            <!-- Calling the template for grouping on the root element -->
            <xsl:call-template name="group" />

        </xsl:copy>

    </xsl:template>

    <xsl:template name="group">

        <!-- Apply the technique known as Muenchian grouping -->
        <xsl:for-each select="//node[generate-id() = generate-id(key('nodesByGroups', @Group)[1])]/@Group">
            <xsl:variable name="group" select="." />
            <xsl:element name="node">
                <xsl:attribute name="GroupTitle"><xsl:value-of select="$group" /></xsl:attribute>
                <xsl:element name="nodes">
                    <xsl:copy-of select="/*//node[@Group = $group]" />
                </xsl:element>
            </xsl:element>
        </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>

第二个模板使用了一种称为Muenchian grouping的技术。如果您不熟悉XSLT和XPath,最初可能会有点复杂,但一旦得到它,它就会成为一个很棒的工具。对它有一个彻底的解释here

编辑:你可以写下第二个这样的模板,如果你觉得它更清晰:

<xsl:template name="group">

    <!-- Apply the technique known as Muenchian grouping -->
    <xsl:for-each select="//node[generate-id() = generate-id(key('nodesByGroups', @Group)[1])]/@Group">
        <xsl:variable name="group" select="." />
        <node GroupTitle="{$group}">
            <nodes>
                <xsl:copy-of select="/*//node[@Group = $group]" />
            </nodes>
        </node>
    </xsl:for-each>

</xsl:template>

编辑2 :好的,我希望这能满足您的需求:

<?xml version="1.0" encoding="UTF-8"?>
<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"/>

    <!-- Key for Group attributes -->
    <xsl:key name="nodesByGroups" match="node[@Group]" use="@Group" />

    <xsl:template match="/*">

        <!-- Making shallow copy of root element -->
        <xsl:copy>

            <!-- Apply templates on all the child elements/attributes as appropriate -->
            <xsl:apply-templates select="node()|@*" />

            <!-- Calling the template for grouping on the root element -->
            <xsl:call-template name="group" />

        </xsl:copy>

    </xsl:template>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node[@Group]">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template name="group">

        <!-- Apply the technique known as Muenchian grouping -->
        <xsl:for-each select="//node[generate-id() = generate-id(key('nodesByGroups', @Group)[1])]/@Group">
            <xsl:variable name="group" select="." />
            <xsl:element name="node">
                <xsl:attribute name="GroupTitle"><xsl:value-of select="$group" /></xsl:attribute>
                <xsl:element name="nodes">
                    <xsl:apply-templates select="/*//node[@Group = $group]" />
                </xsl:element>
            </xsl:element>
        </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>