|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 §ion1d, when I'm looking for section1d. Any help is greatly appreciated. -D |
|
#2
|
|||
|
|||
|
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>
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
The short answer - no, not in XPath 1.0
|
|
#5
|
|||
|
|||
|
Ok how about making the XSLT create another temporary xml file, I can hold that in memory anyway.
-D |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XPath Ancestors |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|