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 22nd, 2010, 11:54 AM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default issue traversing from 3 level down to 3 level up in xml

Here is the xml I am using

<?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>
</first>
</root>


I am able to search through all attributes
<xsl:value-of select="//@title = 'Facility'"/>


but once I get hold of this how do I go up to root level to print the tree for example if @title = 'Facility' the print this

Enterprise - Solutions - Healthcare - Facility
 
Old September 22nd, 2010, 12:17 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Use the ancestor axis e.g.
Code:
<xsl:template match="/">
  <xsl:apply-templates select="//*[@title = 'Facility']"/>
</xsl:template>

<xsl:template match="*[@title]">
  <xsl:value-of select="ancestor-or-self::*/@title" separator=" - "/>
</xsl:template>
The xsl:value-of separator is XSLT 2.0, if you want XSLT 1.0 then use for-each select="ancestor-or-self::*/@title" and output each value.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 22nd, 2010, 12:32 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

The above code only prints Enterprise instead of

Enterprise - Solutions - Healthcare - Facility.
 
Old September 22nd, 2010, 01:32 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Your result does not make sense, unless you used a stylesheet with version="2.0" and run it with an XSLT 1.0 processor or you used a stylesheet with version="1.0" and run it with an XSLT 2.0 processor supporting backwards compatible processing.
When I use Saxon 9, an XSLT 2.0 processor, with the input
Code:
<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>
  </first>
</root>
the stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:template match="/">
  <xsl:apply-templates select="//*[@title = 'Facility']"/>
</xsl:template>

<xsl:template match="*[@title]">
  <xsl:value-of select="ancestor-or-self::*/@title" separator=" - "/>
</xsl:template>

</xsl:stylesheet>
the output is
Code:
Enterprise - Solutions - Healthcare - Facility
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
xml invalid top level from ASP write XML(solution) g000we XML 0 August 9th, 2006 03:56 AM
LOG-IN and LEVEL cli PHP How-To 2 May 29th, 2005 04:30 PM
access level taoufik Beginning PHP 8 November 15th, 2004 04:29 AM
Protection level noman77 .NET Framework 2.0 0 October 23rd, 2004 07:33 AM
2-Level Menu mahulda ASP.NET 1.0 and 1.1 Basics 1 April 11th, 2004 09:29 PM





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