Как сгруппировать все узлы на основе атрибута с помощью XSLT - PullRequest
0 голосов
/ 30 ноября 2011

Ниже приведен 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, который будет группировать узлы на основе атрибута «Группа» с их значением и добавлять их в отдельный узел так, чтобымой вывод будет как указано ниже:

<?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 голосов
/ 30 ноября 2011

Эта таблица стилей должна помочь:

<?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, но как только вы его получите, он станет отличным инструментом.Здесь есть подробное объяснение здесь .

РЕДАКТИРОВАТЬ: вы можете написать второй шаблон, как этот, если вы найдете его более разборчивым:

<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>
...