Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2010, 10:15 AM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default How to print parent's parent node when 3rd or 4th level @ provided?

<?xml version="1.0" encoding="UTF-8"?>
<root>
<first name="level1" title="Small Business" alt="Small Business" link="/small-business.jsp">
<second name="level2" title="Products and Services" link="/products/small-business.jsp">
<three name="level3" title="Pickup" link="/business-commerical-pick-up/index.jsp"/>
<three name="level3" title="Dropoff" link="/dropoff.jsp"/>
</second>
</first>
<first title="Enterprise" link="/enterprise.jsp">
<second title="Solutions" link="/enterprise/index.jsp">
<three title="Constructions" link="/construction/index.jsp">
<forth title="C Solutions" link="/solutions/index.jsp"/>
</three>
<three title="Healthcare" link="/healthcare/index.jsp">
<forth title="Facility" link="/healthcare-facility-solutions/index.jsp"/>
<forth title="Community" link="/community-solutions.jsp"/>
</three>
</second>
<second title="Tractor" link="/enterprise/test.jsp"/>
</first>
</root>

Based on this xml if any forth or third level @title is provided how do I display all the second levels and only the third level expanded
based on the value provided for instance

if @title = 'community'
than display

2) Products and Services
2) Solutions
3) Constructions
3) Healthcare
2) Tractor

if @title = 'Healthcare' than same result should be displayed as above


if @title = 'Solutions' than this should be the output.
2) Products and Services
2) Solutions
2) Tractor

if @title = 'Pickup' than this should be the output.
2) Products and Services
3) Pickup
3) Dropoff
2) Solutions
2) Tractor
 
Old September 24th, 2010, 01:32 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a sample stylesheet that takes a parameter for the title:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:param name="title" select="'Community'"/>
  
  <xsl:template match="second">
    <xsl:text>2) </xsl:text>
    <xsl:value-of select="@title"/>
    <xsl:text>
</xsl:text>
    <xsl:apply-templates select="three[current()[three[@title = $title] | three/forth[@title = $title]]]"/>
  </xsl:template>
  
  <xsl:template match="three">
    <xsl:text>3) </xsl:text>
    <xsl:value-of select="@title"/>
    <xsl:text>
</xsl:text>
  </xsl:template>

</xsl:stylesheet>
When I run that with Saxon against the input
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <first name="level1" title="Small Business" alt="Small Business" link="/small-business.jsp">
    <second name="level2" title="Products and Services" link="/products/small-business.jsp">
      <three name="level3" title="Pickup" link="/business-commerical-pick-up/index.jsp"/>
      <three name="level3" title="Dropoff" link="/dropoff.jsp"/>
    </second>
  </first>
  <first title="Enterprise" link="/enterprise.jsp">
    <second title="Solutions" link="/enterprise/index.jsp">
      <three title="Constructions" link="/construction/index.jsp">
        <forth title="C Solutions" link="/solutions/index.jsp"/>
      </three>
      <three title="Healthcare" link="/healthcare/index.jsp">
        <forth title="Facility" link="/healthcare-facility-solutions/index.jsp"/>
        <forth title="Community" link="/community-solutions.jsp"/>
      </three>
    </second>
    <second title="Tractor" link="/enterprise/test.jsp"/>
  </first>
</root>
the output is
Code:
2) Products and Services
2) Solutions
3) Constructions
3) Healthcare
2) Tractor
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 24th, 2010, 04:09 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks that worked :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying data as a Parent Node with Left Node and Right Node Manoj Bisht Visual Basic 2008 Professionals 0 April 2nd, 2009 02:34 AM
XSLT Going up a level from current node. lafilip XSLT 4 February 23rd, 2007 03:06 PM
Compare parent's attribute with grand parent's one scubaduba XSLT 3 November 29th, 2004 12:26 PM
Forcing a form in the parent's parent to submit Snib Javascript 11 June 22nd, 2004 11:48 PM
Saving the parent node Doyle Bradely XML 2 December 23rd, 2003 07:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.