我得到了下面给出的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 & Conditions" id="tcm:233-192449-64" url="/sitetools/terms_and_conditions.aspx" compTitle="Terms & 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 & Conditions" id="tcm:233-192449-64" url="/sitetools/terms_and_conditions.aspx" compTitle="Terms & 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>
请建议! !
这个样式表应该可以做到:
<?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分组的技术。如果您不熟悉XSLT和XPath,那么一开始理解它可能有点复杂,但是一旦您掌握了它,它就会成为一个很棒的工具。这里有详细的解释。
编辑:你可以这样写第二个模板,如果你觉得它更清晰:
<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>
EDIT 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>