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 June 28th, 2011, 06:37 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default how to get siblings and parents

I know how to go from top down in a nested xsl using xsl:for-each but need to know how to traverse back up for instance here is my XML

<?xml version="1.0" encoding="UTF-8"?>
<root>
<business>
<node show="true">
<label key="">B1</label>
<link type="page">
<value>B1</value>
</link>
<node show="true">
<label key="">B1 Level two </label>
<link type="page">
<value>b1_level_two.aspx</value>
</link>
<node show="true">
<label key="">B1 level three</label>
<link type="page">
<value>b1_level_three.aspx</value>
</link>
</node>
<node show="true">
<label key="">B1 Level three X</label>
<link type="page">
<value>b1_level_theex.aspx</value>
</link>
<node show="true">
<label key="">B1 Level Four</label>
<link type="page">
<value>b1_level_four.aspx</value>
</link>
</node>
</node>
<node show="true">
<label key="">B1 Level three XX</label>
<link type="page">
<value>b1_level_theexx.aspx</value>
</link>
</node>
</node>
<node show="true">
<label key="">B1 level two X</label>
<link type="page">
<value>b1_level_twox.aspx</value>
</link>
</node>
</node>
</business>
</root>


based on this xml if
//value='b1_level_theex.aspx'

or if this is provided

//value='b1_level_four.aspx'

I need this to be printed.

<level1> B1 <level1>
<level2> B1 Level two <level2>
<level3> B1 level three <level3>
<level3> B1 Level three X<level3>

so it will only display upto 3 levels doesnt matter how many node deep the value exists. same ways if //value='B1' than I want level 1 and 2 to be printed only.

Last edited by eruditionist; June 28th, 2011 at 06:40 PM..
 
Old June 29th, 2011, 03:18 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Something along the lines of this would do the job I hope:

Code:
   <xsl:template match="/">
      <xsl:apply-templates select="//node[link/value='b1_level_four.aspx']"/>
   </xsl:template>

   <xsl:template match="node">
      <xsl:apply-templates select="ancestor::node[1]"/>
      <xsl:element name="level{count(ancestor::node)+1}">
         <xsl:value-of select="label"/>
      </xsl:element>
   </xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old June 29th, 2011, 10:19 AM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

This works but how do I exclude <level4>?

<?xml version='1.0' ?>
<level1>B1</level1><level2>B1 Level two </level2><level3>B1 Level three X</level3><level4>B1 Level Four</level4>

It should only print till level3.
 
Old June 29th, 2011, 12:15 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Then use ".." to get the parent of the node you are selecting:

Code:
<xsl:apply-templates select="//node[link/value='b1_level_four.aspx']/.."/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old June 29th, 2011, 12:25 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The following should output three levels, controled by the parameter named "depth":
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:param name="value" select="'b1_level_four.aspx'"/>
  <xsl:param name="depth" select="3"/>
  
  <xsl:output indent="yes"/>
  
   <xsl:template match="/">
      <xsl:apply-templates select="//node[link/value = $value]/ancestor::node[position() &lt;= $depth]"/>
   </xsl:template>

   <xsl:template match="node">
      <xsl:element name="level{count(ancestor::node)+1}">
         <xsl:value-of select="label"/>
      </xsl:element>
   </xsl:template>
   
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 29th, 2011, 01:13 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

given the following xml is there a way to find if the current node is a parent or grandparent node for instance if I looped through the first set of nodes.

<xsl:for-each select="root/business/node">
<xsl:choose>
<xslwhen test="check all nodes under this to see if value='b1_level_four.aspx exists'">
TRUE
</xsl:when>
<xsl:otherwise>
FALSE
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
 
Old June 29th, 2011, 01:19 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:when test="descendant::value[. = '...']">
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 29th, 2011, 03:00 PM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

You guys are awesome. Thanks. It worked





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sibling Recursion, concatenating multiple parents. FrozenGrapes XSLT 12 April 20th, 2010 08:50 AM
Simplifying XML parents [XSLT] FrozenGrapes XSLT 2 April 13th, 2010 06:02 PM
Grouping following siblings bonekrusher XSLT 5 November 19th, 2009 02:30 PM
having issue printing child values by comparing parents value eruditionist XSLT 4 January 7th, 2009 02:34 AM
Comparing siblings Chamkaur XSLT 1 June 17th, 2006 09:56 PM





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