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 December 22nd, 2006, 04:29 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default How to find a node which is between two instances

Hi,

The following is my input xml file.

<root>
   ...
   ...
   <table>
     <tr>
        <td>name</td>
         <td>no</td>
     </tr>
   </table>
    <address>Address of the customer </address>
.....
 <table>
     <tr>
        <td>Customer Name</td>
         <td>Customer Id</td>
     </tr>
   </table>
   <address>Address of the new customer</address>
.......

<table>
     <tr>
        <td>Customer Name</td>
         <td>Customer Id</td>
     </tr>
   </table>

</root>
..........

output file

<table>
     <tr>
        <td>name</td>
         <td>no</td>
     </tr>
    <tr> Address of the customer</tr>
</table>
 .....
<table>
     <tr>
        <td>Customer Name</td>
         <td>Customer Id</td>
     </tr>
<tr> Address of the new customer</tr>
  </table>
.....
<table>
     <tr>
        <td>Customer Name</td>
         <td>Customer Id</td>
     </tr>
</table>

My requirement is that if there is any <address> tag in between two table tags, I have to create the contents of the <address> as the row to the previous table element. I don't know how to proceed on this.

Thanks in advance,
Raja

 
Old December 22nd, 2006, 05:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Something like this:

<xsl:template match="table">
<table>
  <xsl:copy-of select="child::node()"/>
  <xsl:if test="following-sibling::*[1][self::address]">
    <tr><xsl:copy-of select="following-sibling::*[1]"/></tr>
  </xsl:if>
</table>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 22nd, 2006, 07:27 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default

kay,

 Thanks. Sorry for not clearly mentioning the scenario. <address> tag may not be immediate sibling of <table>. Anyway It is a sibling only. but it may not be immediate one.

 
Old December 22nd, 2006, 07:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you want any address that comes before the next table, use

following-sibling::*[self::address|self:table][1][self::address]

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Missing Node (Node data) pashworth XSLT 3 January 29th, 2007 12:39 PM
SiteMapDataSource won't find my root node... thenoseknows ASP.NET 2.0 Professional 0 October 31st, 2006 04:22 PM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM





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