Let's say I have the following structure: <?xml version="1.0" encoding="UTF-8"?> <root> <child id="c1"> <child id="gc1"> <child id="ggc1"/> <child id="ggc2"/> </child> <child id="gc2"> <child id="ggc3"/> <child id="ggc4"/> </child> </child> <child id="c2"> <child id="gc3"> <child id="ggc5"/> <child id="ggc6"/> </child> <child id="gc4"> <child id="ggc7"/> <child id="ggc8"/> </child> </child> </root> By using the following XPath query //child[@id="gc3"]/child I can get the child nodes of "gc1". But what I'd really like to get is the sub branch/path going back to the root. So instead of just returning the two nodes <child id="ggc5"/> <child id="ggc6"/> I'd like to be able to return the sub branch/path <root> <child id="c2"> <child id="gc3"> <child id="ggc5"/> <child id="ggc6"/> </child> </child> </root> Is that possible? Or is this something I'd have to do programatically using the nodes returned by the XPath query? thnx, Christoph
Christoph Boget wrote: > Is that possible? Or is this something I'd have to do programatically > using the nodes returned by the XPath query? XSLT can do that (or XQuery). XPath can't do that, it just returns nodes in an existing document and once you would select the root you have all its children present too. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
>> Is that possible? Or is this something I'd have to do programatically >> using the nodes returned by the XPath query? > XSLT can do that (or XQuery). XPath can't do that, it just returns nodes > in an existing document and once you would select the root you have all > its children present too. Could you point me to a good resource where I can read how I can do this using XSLT? thnx, Christoph
Christoph Boget wrote: > Could you point me to a good resource where I can read how I can do this > using XSLT? Here is an XSLT 2.0 stylesheet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:param name="id" select="'gc3'"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="@*"> <xsl:copy/> </xsl:template> <xsl:template match="*[.//*[@id = $id]]"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="*[@id = $id or .//*[@id = $id]]"/> </xsl:copy> </xsl:template> <xsl:template match="*[@id = $id]"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet> And here is an XSLT 1.0 stylesheet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="id" select="'gc3'"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="@*"> <xsl:copy/> </xsl:template> <xsl:template match="*"> <xsl:choose> <xsl:when test=".//*[@id = $id]"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="*[@id = $id or .//*[@id = $id]]"/> </xsl:copy> </xsl:when> <xsl:when test="@id = $id"> <xsl:copy-of select="."/> </xsl:when> </xsl:choose> </xsl:template> </xsl:stylesheet> Both stylesheets take the id of the element as a parameter. As for resources, google for XSLT and XPath tutorials. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/