Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 22nd, 2010, 11:54 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
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
Reply With Quote
  #2 (permalink)  
Old September 22nd, 2010, 12:17 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
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
Reply With Quote
  #3 (permalink)  
Old September 22nd, 2010, 12:32 PM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

The above code only prints Enterprise instead of

Enterprise - Solutions - Healthcare - Facility.
Reply With Quote
  #4 (permalink)  
Old September 22nd, 2010, 01:32 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:37 AM.


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