Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Thu, 27 Mar 2008 11:55:23 -0400,    group: microsoft.public.xml        back       


Possible using XPath?   
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
date: Thu, 27 Mar 2008 11:55:23 -0400   author:   Christoph Boget

Re: Possible using XPath?   
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/
date: Thu, 27 Mar 2008 16:59:34 +0100   author:   Martin Honnen

Re: Possible using XPath?   
>> 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
date: Thu, 27 Mar 2008 12:18:40 -0400   author:   Christoph Boget

Re: Possible using XPath?   
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/
date: Thu, 27 Mar 2008 17:26:01 +0100   author:   Martin Honnen

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us