p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Pickingup sub elements from its parent (http://p2p.wrox.com/showthread.php?t=66629)

vikkiefd March 14th, 2008 01:28 AM

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




mhkay March 14th, 2008 03:12 AM

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

vikkiefd March 14th, 2008 09:31 AM

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


Martin Honnen March 14th, 2008 09:34 AM

Try
Code:

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


mhkay March 14th, 2008 10:28 AM

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

mhkay March 14th, 2008 10:29 AM

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


All times are GMT -4. The time now is 08:38 PM.

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