XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 24th, 2004, 11:33 AM
DEfusion DEfusion is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 288 DEfusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 26 m 45 sec
Reputation Power: 6
XPath Ancestors

Say I have the following XML :

Code:
<root>
	<section name="section1">
		<section name="section1b" />
		<section name="section1c">
			<section name="section1d" />
		</section>
	</section>
</root>


I want to get all the ancestors of a section and the section I've tried the following but I just get the section1d back or nothing at all. I've tried alot of combinations but with no luck.

//*[@name = 'section1d']

ancestor-or-self::section[@name = 'section1d']

The idea is I will never know how deep a given section will be, I will only have the name to find it. I want back from the xPath the section it matches & all of it's ancestors - so in this example case I need it to return section1, section1b &section1d, when I'm looking for section1d.

Any help is greatly appreciated.

-D

Reply With Quote
  #2  
Old August 25th, 2004, 02:44 AM
fpmurphy fpmurphy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: USA
Posts: 305 fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 7 h 17 m 54 sec
Reputation Power: 7
Looks like you are trying to locate BOTH ancestors and the siblings of ancestors. Note that the element "root" is also an ancestor!

Here is one way of achieving what you want:

Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />

<xsl:template match="/">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="section[@name = 'section1d']">
    <xsl:for-each select="ancestor::*">
          <xsl:for-each select="preceding-sibling::*">
             Output PS: <xsl:value-of select="@name"/>
          </xsl:for-each>
       <xsl:if test="not(name()='root')">
       Output: <xsl:value-of select="@name"/>
       </xsl:if>
       <xsl:for-each select="following-sibling::*">
           Output FS: <xsl:value-of select="@name"/>
       </xsl:for-each>
    </xsl:for-each>
       Output: <xsl:value-of select="@name"/>

</xsl:template>

</xsl:stylesheet>

Reply With Quote
  #3  
Old August 25th, 2004, 04:33 AM
DEfusion DEfusion is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 288 DEfusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 26 m 45 sec
Reputation Power: 6
Is there anyway of doing that with a single or set of xPaths, I'm not really looking to use XSL, I'm wanting to use it programatically within my scripting (coldfusion).

-D

Reply With Quote
  #4  
Old August 25th, 2004, 10:21 PM
fpmurphy fpmurphy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: USA
Posts: 305 fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 7 h 17 m 54 sec
Reputation Power: 7
The short answer - no, not in XPath 1.0

Reply With Quote
  #5  
Old August 27th, 2004, 08:28 AM
DEfusion DEfusion is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 288 DEfusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 26 m 45 sec
Reputation Power: 6
Ok how about making the XSLT create another temporary xml file, I can hold that in memory anyway.

-D

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XPath Ancestors


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT