Reputation: 7707
I have got below 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="yes">
<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 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" Group="yes"/>
<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" Group="yes"/>
</node>
</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>
Now I want to write a XSLT which will check for Group="Yes" and will render the below outputted 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="yes">
<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 Miles"/>
<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>
</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 title="Group">
<node>
<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">
<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" Group="yes"/>
<node title="060. Skysurfers" id="tcm:233-38887-4" url="/Skywards/skysurfers/skysurfers.aspx" indexpage="tcm:233-194326-64" compTitle="Skysurfers" Group="yes"/>
</node>
</node>
</node>
</navigation>
In Above outputted xml you can see there is an extra node added with title group as well as the child node which were having Group="yes" are removed from top and got added below.
Please suggest!!
Upvotes: 1
Views: 1884
Reputation:
I will try do above issue with below solution:
<?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 title="Group">
<node>
<xsl:apply-templates select="node()" mode="group"></xsl:apply-templates>
</node>
</node>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template match="node" mode="group">
<xsl:choose>
<xsl:when test="@Group = 'yes'">
<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>
Upvotes: 1
Reputation: 2998
An XSLT 2.0 solution. I'm not sure I totally understood the precise goal and therefore it works for this specific case but may have to be adapt if I misunderstood some requirements :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"></xsl:output>
<xsl:template match="/">
<xsl:apply-templates></xsl:apply-templates>
</xsl:template>
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node[@Group='yes' and not(exists(node()))]"/>
<xsl:template match="node[@Group='yes' and exists(node())]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="node[not(@Group='yes')]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node[@Group='yes' and exists(node())]" mode="groupOnly">
<xsl:copy>
<xsl:apply-templates select="@*[not(name()='Group')]"/>
<xsl:copy-of select="node[@Group='yes']"/>
</xsl:copy>
</xsl:template>
<xsl:template match="navigation">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<node title="Group">
<node>
<xsl:apply-templates select="//node[@Group='yes' and exists(node())]" mode="groupOnly"/>
</node>
</node>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The idea is to get trough the whole tree, element by element, it's the business of the match="*|@*"
template and then to have specific template for each element that requires it.
Then I use different mode for the different cases : the default mode for the copy of node in the pre-existing node. The groupOnly
mode for the node I copy in the new node element.
Upvotes: 0