How to do Grouping on the basis of attribute as well as removing the childs nodes from current xml.

Hi Experts,

I have got below attached full xml as a output.

 NavigationOutput.xml

Here is the part of above attached XML, to express the logic to be implemented.

<node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" imageSrcOff="/Preview/companyglobal/english/images/skywards_tcm233-343463.gif" imageSrcSelected="/Preview/companyglobal/english/images/skywards_active_tcm233-343465.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" accessKey="S" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
			<node title="010. Membership Tiers" id="tcm:233-38882-4" url="/Skywards/membership_tiers/membership_tiers.aspx" indexpage="tcm:233-194317-64" compTitle="Membership Tiers"/>
			<node title="020. Earning Miles" id="tcm:233-38883-4" url="/Skywards/earning_miles/earning_miles.aspx" indexpage="tcm:233-194318-64" compTitle="Earning Skywards Miles"/>
			<node title="030. Travel Rewards" id="tcm:233-38884-4" url="/Skywards/travel_rewards/travel_rewards.aspx" indexpage="tcm:233-194320-64" compTitle="Travel Rewards"/>
			<node title="040. Journey Management Tools" id="tcm:233-38885-4" url="/Skywards/journey_management_tools/journey_management.aspx" indexpage="tcm:233-194322-64" compTitle="Journey Management "/>
			<node title="050. Personal Travel Coordinator" id="tcm:233-38886-4" url="/Skywards/personal_travel_coordinator/personal_travel_coordinator.aspx" indexpage="tcm:233-194324-64" compTitle="Personal Travel Coordinator"/>
			<node title="060. Skysurfers" id="tcm:233-38887-4" url="/Skywards/skysurfers/skysurfers.aspx" indexpage="tcm:233-194326-64" compTitle="Skysurfers"/>
			<node title="070. Become a Skywards member" id="tcm:233-56534-4" url="http://www.skywards.com/join_now_personal.aspx" indexpage="tcm:233-305043-64" linktype="external" compTitle="Become a Skywards member"/>
			<node title="080. The company High Street" id="tcm:233-59871-4" url="/Skywards/the_company_high_street/the_company_high_street.aspx" indexpage="tcm:233-346057-64" compTitle="The company High Street"/>
			<node title="090. Log In" id="tcm:233-92637-4" url="https://www.skywards.com/index.aspx" indexpage="tcm:233-636665-64" linktype="external" compTitle="Log In"/>
			<node title="100. Login" id="tcm:233-93287-4" url="/Skywards/skywardLogin/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
			<node title="110. Logout" id="tcm:233-93288-4" url="/Skywards/skywardLogout/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
		</node>

Open in new window



In above part XML, you can see that I have got an attribute with name "GroupCode", Now I want to group it as well as I want to remove the child nodes ("100. Login" and "110. Logout") from top xml and will be added in the grouped part below, however the parent node will exist on the top xml. Below will be output after being transformed using xslt 1.0.
<node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" imageSrcOff="/Preview/companyglobal/english/images/skywards_tcm233-343463.gif" imageSrcSelected="/Preview/companyglobal/english/images/skywards_active_tcm233-343465.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" accessKey="S" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
  <node title="010. Membership Tiers" id="tcm:233-38882-4" url="/Skywards/membership_tiers/membership_tiers.aspx" indexpage="tcm:233-194317-64" compTitle="Membership Tiers"/>
  <node title="020. Earning Miles" id="tcm:233-38883-4" url="/Skywards/earning_miles/earning_miles.aspx" indexpage="tcm:233-194318-64" compTitle="Earning Skywards Miles"/>
  <node title="030. Travel Rewards" id="tcm:233-38884-4" url="/Skywards/travel_rewards/travel_rewards.aspx" indexpage="tcm:233-194320-64" compTitle="Travel Rewards"/>
  <node title="040. Journey Management Tools" id="tcm:233-38885-4" url="/Skywards/journey_management_tools/journey_management.aspx" indexpage="tcm:233-194322-64" compTitle="Journey Management "/>
  <node title="050. Personal Travel Coordinator" id="tcm:233-38886-4" url="/Skywards/personal_travel_coordinator/personal_travel_coordinator.aspx" indexpage="tcm:233-194324-64" compTitle="Personal Travel Coordinator"/>
  <node title="060. Skysurfers" id="tcm:233-38887-4" url="/Skywards/skysurfers/skysurfers.aspx" indexpage="tcm:233-194326-64" compTitle="Skysurfers"/>
  <node title="070. Become a Skywards member" id="tcm:233-56534-4" url="http://www.skywards.com/join_now_personal.aspx" indexpage="tcm:233-305043-64" linktype="external" compTitle="Become a Skywards member"/>
  <node title="080. The company High Street" id="tcm:233-59871-4" url="/Skywards/the_company_high_street/the_company_high_street.aspx" indexpage="tcm:233-346057-64" compTitle="The company High Street"/>
  <node title="090. Log In" id="tcm:233-92637-4" url="https://www.skywards.com/index.aspx" indexpage="tcm:233-636665-64" linktype="external" compTitle="Log In"/>
</node>
<node GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
  <nodes>
    <node title="035. Business Rewards" id="tcm:233-58061-4" url="/business_rewards/business_rewards.aspx" indexpage="tcm:233-358352-64" compTitle="Business Rewards" imageSrcOn="/Preview/companyglobal/english/images/business_rewards_highlight_tcm233-324549.gif" imageSrcOff="/Preview/companyglobal/english/images/business_rewards_tcm233-324548.gif" imageSrcSelected="/Preview/companyglobal/english/images/business_rewards_active_tcm233-324551.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/business_rewards_highlight_tcm233-324549.gif" authentication="sme-guest" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
    <node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" imageSrcOff="/Preview/companyglobal/english/images/skywards_tcm233-343463.gif" imageSrcSelected="/Preview/companyglobal/english/images/skywards_active_tcm233-343465.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" accessKey="S" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
      <node title="100. Login" id="tcm:233-93287-4" url="/Skywards/skywardLogin/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
      <node title="110. Logout" id="tcm:233-93288-4" url="/Skywards/skywardLogout/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
    </node>
  </nodes>
</node>

Open in new window


Below is XSLT which I am trying to write but not able to get the actual required result.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
            <xsl:if test="self::navigation">
                <node GroupTitle="{@GroupTitle}" GroupCode="{@GroupCode}" GroupID="{@GroupID}">
                    <nodes>
                        <xsl:apply-templates select="node()" mode="group"></xsl:apply-templates>
                    </nodes>
                </node>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="node" mode="group">
        <xsl:choose>
            <xsl:when test="@GroupCode !=''">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates select="node()" mode="group"/>
                </xsl:copy>            
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="node()" mode="group"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="*" mode="group"/>
</xsl:stylesheet>

Open in new window


The output from above XSLT is as below:
<node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" imageSrcOff="/Preview/companyglobal/english/images/skywards_tcm233-343463.gif" imageSrcSelected="/Preview/companyglobal/english/images/skywards_active_tcm233-343465.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" accessKey="S" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
  <node title="010. Membership Tiers" id="tcm:233-38882-4" url="/Skywards/membership_tiers/membership_tiers.aspx" indexpage="tcm:233-194317-64" compTitle="Membership Tiers"/>
  <node title="020. Earning Miles" id="tcm:233-38883-4" url="/Skywards/earning_miles/earning_miles.aspx" indexpage="tcm:233-194318-64" compTitle="Earning Skywards Miles"/>
  <node title="030. Travel Rewards" id="tcm:233-38884-4" url="/Skywards/travel_rewards/travel_rewards.aspx" indexpage="tcm:233-194320-64" compTitle="Travel Rewards"/>
  <node title="040. Journey Management Tools" id="tcm:233-38885-4" url="/Skywards/journey_management_tools/journey_management.aspx" indexpage="tcm:233-194322-64" compTitle="Journey Management "/>
  <node title="050. Personal Travel Coordinator" id="tcm:233-38886-4" url="/Skywards/personal_travel_coordinator/personal_travel_coordinator.aspx" indexpage="tcm:233-194324-64" compTitle="Personal Travel Coordinator"/>
  <node title="060. Skysurfers" id="tcm:233-38887-4" url="/Skywards/skysurfers/skysurfers.aspx" indexpage="tcm:233-194326-64" compTitle="Skysurfers"/>
  <node title="070. Become a Skywards member" id="tcm:233-56534-4" url="http://www.skywards.com/join_now_personal.aspx" indexpage="tcm:233-305043-64" linktype="external" compTitle="Become a Skywards member"/>
  <node title="080. The company High Street" id="tcm:233-59871-4" url="/Skywards/the_company_high_street/the_company_high_street.aspx" indexpage="tcm:233-346057-64" compTitle="The company High Street"/>
  <node title="090. Log In" id="tcm:233-92637-4" url="https://www.skywards.com/index.aspx" indexpage="tcm:233-636665-64" linktype="external" compTitle="Log In"/>
  <node title="100. Login" id="tcm:233-93287-4" url="/Skywards/skywardLogin/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
  <node title="110. Logout" id="tcm:233-93288-4" url="/Skywards/skywardLogout/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
</node>
<node GroupTitle="" GroupCode="" GroupID="">
  <nodes>
    <node title="035. Business Rewards" id="tcm:233-58061-4" url="/business_rewards/business_rewards.aspx" indexpage="tcm:233-358352-64" compTitle="Business Rewards" imageSrcOn="/Preview/companyglobal/english/images/business_rewards_highlight_tcm233-324549.gif" imageSrcOff="/Preview/companyglobal/english/images/business_rewards_tcm233-324548.gif" imageSrcSelected="/Preview/companyglobal/english/images/business_rewards_active_tcm233-324551.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/business_rewards_highlight_tcm233-324549.gif" authentication="sme-guest" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
    <node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" imageSrcOff="/Preview/companyglobal/english/images/skywards_tcm233-343463.gif" imageSrcSelected="/Preview/companyglobal/english/images/skywards_active_tcm233-343465.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" accessKey="S" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
      <node title="100. Login" id="tcm:233-93287-4" url="/Skywards/skywardLogin/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
      <node title="110. Logout" id="tcm:233-93288-4" url="/Skywards/skywardLogout/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
    </node>
  </nodes>
</node>

Open in new window


My XSLT is not able to get value here <node GroupTitle="" GroupCode="" GroupID="">
as well as I am not able to remove the Child grouped Items from the top XML.

Please suggest!!
tia_kamakshiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
mmh, I have some thoughts, but I don't understand the logic.
Since 040. Skywards is in the list and 100 and 101 children are not,
I believe that teh discrimator is that you want to remove from the common processing,
those elements that are in a group and have no children that are not in a group.
Is that a correct assumption?

By only sending part of the XML (we are missing the navigation element, so selff:navigation does nothing)
we don't see the grouping mechanism work in the XML subset we have

If my assumption is right then all you need to do is add a template
    <xsl:template match="*[normalize-space(@GroupCode) and not(*[not(normalize-space(@GroupCode))])]"/>
0
tia_kamakshiAuthor Commented:
Hi Gertone,

I am clear what you are trying to say here:

I believe that teh discrimator is that you want to remove from the common processing,
those elements that are in a group and have no children that are not in a group.


Can you please add your logic in my XSLT or give me full XSLT so that I can test it and confirm.

Please suggest!!

Thanks
0
Gertone (Geert Bormans)Information ArchitectCommented:
I did,

next to your templates you have to add the one template I sent you

    <xsl:template match="*[normalize-space(@GroupCode) and not(*[not(normalize-space(@GroupCode))])]"/>

that is all there is to it

I think in order to know for sure, you need to send the full source XML, since I don't see a navigation, that is why the grouping doesn't work on this snippet alone
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

tia_kamakshiAuthor Commented:
Hi Gertone if you see on top where I have asked the question, I have embed the full navigation, please let me know if you require it again.

in my xslt I have called three places the templates, where I need to put your template and also why I am not able to get values here in my xslt

<node GroupTitle="{@GroupTitle}" GroupCode="{@GroupCode}" GroupID="{@GroupID}">

above code is returning blank values to me.

Please suggest!!
0
tia_kamakshiAuthor Commented:
Ok I am able to get my required result by adding your template, but two problems still exists as I am still getting

<node GroupTitle="" GroupCode="" GroupID="">
		<nodes>
			<node title="035. Business Rewards" id="tcm:233-58061-4" url="/business_rewards/business_rewards.aspx" indexpage="tcm:233-358352-64" compTitle="Business Rewards" imageSrcOn="/Preview/companyglobal/english/images/business_rewards_highlight_tcm233-324549.gif" imageSrcOff="/Preview/companyglobal/english/images/business_rewards_tcm233-324548.gif" imageSrcSelected="/Preview/companyglobal/english/images/business_rewards_active_tcm233-324551.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/business_rewards_highlight_tcm233-324549.gif" authentication="sme-guest" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
			<node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" imageSrcOff="/Preview/companyglobal/english/images/skywards_tcm233-343463.gif" imageSrcSelected="/Preview/companyglobal/english/images/skywards_active_tcm233-343465.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" accessKey="S" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
				<node title="100. Login" id="tcm:233-93287-4" url="/Skywards/skywardLogin/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
				<node title="110. Logout" id="tcm:233-93288-4" url="/Skywards/skywardLogout/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
			</node>
		</nodes>
	</node>

Open in new window


You can see there is blank values in the top node. And second thing is that, how can I remove any attribute if that is not required while doing the grouping.

Thanks.
0
Gertone (Geert Bormans)Information ArchitectCommented:
OK, I did not spot the full XML attached,
got it now

navigation has no such attributes, that is why you get empty values

You simply make one node for grouping at the end.
What is your grouping logic?
in other words, what do you want to appear in the group?
0
tia_kamakshiAuthor Commented:
Hi Gertone,

Above XSLT code I have only got with me and below is the output which I am looking and grouping will be on the basis of attribute @GroupCode. So my output will be as below:
	<node GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
		<nodes>
			<node title="035. Business Rewards" id="tcm:233-58061-4" url="/business_rewards/business_rewards.aspx" indexpage="tcm:233-358352-64" compTitle="Business Rewards" imageSrcOn="/Preview/companyglobal/english/images/business_rewards_highlight_tcm233-324549.gif" imageSrcOff="/Preview/companyglobal/english/images/business_rewards_tcm233-324548.gif" imageSrcSelected="/Preview/companyglobal/english/images/business_rewards_active_tcm233-324551.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/business_rewards_highlight_tcm233-324549.gif" authentication="sme-guest" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
				<node title="046. Business Rewards Awards Calculator" id="tcm:233-88592-4" url="/SME.aspx&amp;NavigateTo=MilesCalculator" linktype="ibe" compTitle="Business Rewards Awards Calculator" localTitle="Business Rewards Awards Calculator" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
				<node title="090. Travel Activity (User)" id="tcm:233-58080-4" url="/SME.aspx&amp;NavigateTo=TravelActivityU" linktype="ibe" compTitle="Travel Activity" linksection="SME" authentication="sme-user" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
			</node>
			<node title="040. Skywards" id="tcm:233-38448-4" url="/Skywards/skywards.aspx" indexpage="tcm:233-192262-64" compTitle="Skywards" imageSrcOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" imageSrcOff="/Preview/companyglobal/english/images/skywards_tcm233-343463.gif" imageSrcSelected="/Preview/companyglobal/english/images/skywards_active_tcm233-343465.gif" imageSrcSelectedOn="/Preview/companyglobal/english/images/skywards_highlight_tcm233-343464.gif" accessKey="S" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296">
				<node title="100. Login" id="tcm:233-93287-4" url="/Skywards/skywardLogin/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
				<node title="110. Logout" id="tcm:233-93288-4" url="/Skywards/skywardLogout/" GroupTitle="Skywards and Business Rewards" GroupCode="SB" GroupID="tcm:233-358296"/>
			</node>
		</nodes>
	</node>

Open in new window


but the problem is that I am getting blank <node GroupTitle="" GroupCode="" GroupID="">

And after this can you please give me logic, if I want to remove particular attribute while I am doing the grouping.
0
Gertone (Geert Bormans)Information ArchitectCommented:
do you need only one group?
it appears that this XML has only one group,
you could do like below than

I remember I gave you a grouping solution in an earlier question,
you have cut out the grouping aspect of it
So I assume you no longer need that and rely on the fact that you only have one group

It also has a logic for removing group attributes
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
            <xsl:if test="self::navigation">
                <node GroupTitle="{//@GroupTitle[1]}" GroupCode="{//@GroupCode[1]}" GroupID="{//@GroupID[1]}">
                    <nodes>
                        <xsl:apply-templates select="node()" mode="group"></xsl:apply-templates>
                    </nodes>
                </node>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="*[normalize-space(@GroupCode) and not(*[not(normalize-space(@GroupCode))])]"/>
    
    <xsl:template match="node[normalize-space(@GroupCode)]" mode="group">
        <xsl:copy>
            <xsl:apply-templates select="@*" mode="group"/>
            <xsl:apply-templates select="node()" mode="group"/>
        </xsl:copy>            
    </xsl:template>
    
    <xsl:template match="node" mode="group">
        <xsl:apply-templates select="node()" mode="group"/>
    </xsl:template>
    
    <xsl:template match="*" mode="group"/>
    
    <xsl:template  match="@*" mode="group">
        <xsl:attribute name="{name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
    <!-- node attributes to be deleted have an empty template each -->
    <xsl:template match="@GroupTitle" mode="group"/>
    <xsl:template match="@GroupCode" mode="group"/>
    <xsl:template match="@GroupID" mode="group"/>
</xsl:stylesheet>

Open in new window

0
tia_kamakshiAuthor Commented:
Hi Gertone,

Many thanks for your code, it worked for me!!, but really going above my head.

Can you please write your comments or if you can help me to understand your code.

Thanks.
0
tia_kamakshiAuthor Commented:
Hi Gertone,

One more problem, the grouping is not for only one group.

Grouping can be multiple, so there can be lots of Groupcodes in the xml.

Please suggest how to get it as above code is working for one group and the code provided by you for grouping, how can I merge it here.

Please suggest!!
0
tia_kamakshiAuthor Commented:
Hi Gertone,

I modified your above xslt code as below for multiple grouping, please suggest if any modifcation is required.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  <xsl:variable name="linkURL" select="'/system/ASPX/Link.aspx?ID='"/>
  <xsl:key name="nodesByGroupsCode" match="*[node[not(@GroupCode)]]" use="@GroupCode" />
  <xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()"/>
      <xsl:if test="self::navigation">
      <xsl:for-each select="//node[generate-id() = generate-id(key('nodesByGroupsCode', @GroupCode)[1])]">       
        <node GroupTitle="{@GroupTitle}" GroupCode="{@GroupCode}" Url="{$linkURL}{@GroupID}">       
          <nodes>    
           <xsl:for-each select="key('nodesByGroupsCode', @GroupCode)"> 
          <xsl:copy> 
           <xsl:apply-templates select="@*" mode="group"/>         
             <xsl:apply-templates select="node()" mode="group"></xsl:apply-templates>       
           </xsl:copy>      
            </xsl:for-each>
          </nodes>            
        </node>    
        </xsl:for-each>    
      </xsl:if>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*[normalize-space(@GroupCode) and not(*[not(normalize-space(@GroupCode))])]"/>

  <xsl:template match="node[normalize-space(@GroupCode)]" mode="group">    
    <xsl:copy>         
      <xsl:apply-templates select="@*" mode="group"/>      
    </xsl:copy>       
  </xsl:template>

  <xsl:template match="*" mode="group"/>

  <xsl:template  match="@*" mode="group">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  
  <!-- node attributes to be deleted have an empty template each -->
  <xsl:template match="@GroupTitle" mode="group"/>
  <xsl:template match="@GroupCode" mode="group"/>
  <xsl:template match="@GroupID" mode="group"/>

</xsl:stylesheet>

Open in new window


0
Gertone (Geert Bormans)Information ArchitectCommented:
Strange,
I posted some code this morning, it did not seem to come through

I added comments,
you need to understand the apply-templates mechanism

- apply-templates pushes out a selection of nodes
- the templates evaluate the nodes and the best match wins
- more specific match takes precedence over les specific match

If your code works, stick to yours
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    <xsl:key name="node" match="node" use="@GroupID"/>
    <!-- standaard template catches all in default mode -->
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/> 
            <!-- create groups when current context is the navigation element -->
            <!-- use muenchian for that -->
            <xsl:if test="self::navigation">
                <xsl:for-each select="node/node[generate-id() = generate-id(key('node', @GroupID)[1])]">
                    <xsl:variable name="this-group-id" select="@GroupID"/>
                    <node GroupTitle="{@GroupTitle}" GroupCode="{@GroupCode}" GroupID="{//@GroupID}">
                        <nodes>
                            <!-- push out all the nodes that have this particular group ID -->
                            <xsl:apply-templates select="ancestor::navigation/node/node[@GroupID = $this-group-id]" mode="group"></xsl:apply-templates>
                        </nodes>
                    </node>
                </xsl:for-each>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
    
    <!-- in default mode this specific match takes precedence over the more general match="*" -->
    <!-- this template deletes all node that have a group id and no children that don't have a group id -->
    <xsl:template match="*[normalize-space(@GroupCode) and not(*[not(normalize-space(@GroupCode))])]"/>
    
    <!-- in mode group: copy all node that have a group id -->
    <xsl:template match="node[normalize-space(@GroupCode)]" mode="group">
        <xsl:copy>
            <xsl:apply-templates select="@*" mode="group"/>
            <xsl:apply-templates select="node()" mode="group"/>
        </xsl:copy>            
    </xsl:template>
    
    <!-- in mode group, just process the content of the node elements -->
    <!-- since we have a more specific match with @GroupId, this means only the node that don't have a group id -->
    <xsl:template match="node" mode="group">
        <xsl:apply-templates select="node()" mode="group"/>
    </xsl:template>
    
    <!-- group mode: remove all other elements (not specified in higher up templates) -->
    <xsl:template match="*" mode="group"/>
    
    <!-- in mode group, copy all attributes (except those that have a more specif template match with other instructions -->
    <xsl:template  match="@*" mode="group">
        <xsl:attribute name="{name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
    <!-- node attributes to be deleted have an empty template each -->
    <xsl:template match="@GroupTitle" mode="group"/>
    <xsl:template match="@GroupCode" mode="group"/>
    <xsl:template match="@GroupID" mode="group"/>
</xsl:stylesheet>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tia_kamakshiAuthor Commented:
Thanks Gertone for you reply!!

I am going with your code with slight changes, one quick question can we also remove these attributes(GroupTitle,GroupCode,GroupID) from top whole XML as we have removed them in while doing grouping.

I mean when the grouping is done, all these attributes can be removed from whole xml.

Thanks
0
Gertone (Geert Bormans)Information ArchitectCommented:
replace every          
 <xsl:copy-of select="@*"/>
with
            <xsl:apply-templates select="@*" mode="group"/>

and it will work
0
Gertone (Geert Bormans)Information ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.