Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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
  #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



  #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
  #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

  #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>
  #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
  #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


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





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