Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 14th, 2008, 01:28 AM
Authorized User
 
Join Date: Mar 2008
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pickingup sub elements from its parent

The following is my scenario:
Actually I want to pickup a set of child elements of its parent.These set of child elements may occur multiple times also within that parent, but they are not covered with any more container element.

The following is my input xml:
<?xml version = "1.0" encoding = "utf-8"?>
<DataSet>
 <FirstData>
   <DataDesc></DataDesc>
   <DataType></DataType>
   <Item></Item>
   <Description></Description>
   <Item></Item>
   <Description></Description>
 </FirstData>
</DataSet>

The output should be like this:
<?xml version = "1.0" encoding = "utf-8"?>
<DataSet>
 <FirstData>
  <DataDesc></DataDesc>
  <DataType></DataType>
  <Data no="1">
   <Item></Item>
   <Description></Description>
  </Data>
  <Data no="2">
   <Item></Item>
   <Description></Description>
  </Data>
 </FirstData>
</DataSet>

How to achieve this output. Please help me to resolve this. Thanks in advance for your help.

Regards,
Viki



Reply With Quote
  #2 (permalink)  
Old March 14th, 2008, 03:12 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

So long as Item and Description always come together as a pair, you can do

<xsl:template match="FirstData">
  <xsl:copy>
    <xsl:apply-templates select="DataDesc | DataType | Item"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Item">
<Data no="{position()}">
  <xsl:copy-of select=".|following-sibling::Description[1]"/>
</Data>
</xsl:template>

If the structure is more complex, for example an Item followed by several Descriptions, then you have a positional grouping problem - easy in XSLT 2.0 with xsl:for-each-group, but quite challenging in XSLT 1.0.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old March 14th, 2008, 09:31 AM
Authorized User
 
Join Date: Mar 2008
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the response. Here the "Description" element may or may not come. So at that time, it will pickup the "Description" element available after the next "Item" element. Then how I can handle to avoid this.

Regards,
Viki

Reply With Quote
  #4 (permalink)  
Old March 14th, 2008, 09:34 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Try
Code:
<xsl:template match="Item">
<Data no="{position()}">
  <xsl:copy-of select=".|following-sibling::*[1][self::Description]"/>
</Data>
</xsl:template>
Reply With Quote
  #5 (permalink)  
Old March 14th, 2008, 10:28 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

following-sibling[1][self::Description]

will select the next sibling element only if it is a Description element.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old March 14th, 2008, 10:29 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, for

 following-sibling[1][self::Description]

read

 following-sibling::*[1][self::Description]

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
parent/ancestor nmnm XSLT 1 April 24th, 2007 05:25 AM
getting the parent gantait XSLT 7 March 3rd, 2007 10:25 AM
Parent - Child Combo babloo81 JSP Basics 0 April 27th, 2005 01:46 PM
parent::* debo_sbg XSLT 5 April 1st, 2005 02:45 PM
Need help with creating a Parent Category seanmayhew BOOK: ASP.NET Website Programming Problem-Design-Solution 2 November 13th, 2004 06:20 PM



All times are GMT -4. The time now is 07:37 AM.


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