Subject: RE: xsl apply template is not working
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 27 Apr 2005 09:57:19 +0100
|
You seem to have a classic positional grouping problem, where you are
turning
<header/>
<detail/>
<detail/>
<detail/>
<header/>
<detail/>
<detail/>
<detail/>
into
<record>
<detail/>
<detail/>
<detail/>
</record>
<record>
<detail/>
<detail/>
<detail/>
</record>
Try a google for "XSLT positional grouping".
In XSLT 2.0 you do this with <xsl:for-each-group
group-starting-with="header"/>.
It's harder in 1.0: there are two usual approaches:
(a) a recursive walk over the sibling axis using <xsl:apply-templates
select="following-sibling::*[1]">
(b) converting it into a value-based grouping problem (see
http://www.jenitennison.com/xslt/grouping) by using
generate-id(preceding-sibling::header[1]) as the grouping key for detail
elements.
Michael Kay
http://www.saxonica.com/
> -----Original Message-----
> From: sreekanth.gangula@xxxxxxxxx
> [mailto:sreekanth.gangula@xxxxxxxxx]
> Sent: 27 April 2005 09:34
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: xsl apply template is not working
>
>
>
> Thanks you very much for the clarification.
> Is there anyway to generate the below target XML.
> Any help greatly appreciated.
>
> Thanks in Advance.
>
> Source XML:
> I have two sets of same data in one XML with a common header.
> The source
> is fixed as it is coming from the client.
> <?xml version="1.0" encoding="UTF-8"?>
> <Root>
> <Header>
> <test>header</test>
> </Header>
> <!--First set of data.............>
> <Transaction>
> <test>first occurence</test>
> </Transaction>
> <Meterpoint>
> <test>first occurence</test>
> </Meterpoint>
> <Address>
> <test>first occurence</test>
> </Address>
> <Asset>
> <test>first occurence</test>
> </Asset>
> <Appointment>
> <test>first occurence</test>
> </Appointment>
> <Name>
> <test>first occurence</test>
> </Name>
> <Address>
> <test>first occurence</test>
> </Address>
> <!--Second set of data.............>
> <Transaction>
> <test>Second occurence</test>
> </Transaction>
> <Meterpoint>
> <test>Second occurence</test>
> </Meterpoint>
> <Address>
> <test>Second occurence</test>
> </Address>
> <Asset>
> <test>Second occurence</test>
> </Asset>
> <Appointment>
> <test>Second occurence</test>
> </Appointment>
> <Name>
> <test>Second occurence</test>
> </Name>
> <Address>
> <test>Second occurence</test>
> </Address>
> </Root>
> Targer XML:
> My targer XML should look like
> <?xml version="1.0" encoding="UTF-8"?>
> <Header>
> <test>first occurence</test>
> <!--First set of data.............>
> <Transaction>
> <test>first occurence</test>
> <Meterpoint>
> <test>first occurence</test>
> <Address>
> <test>first occurence</test>
> <Asset>
> <test>first occurence</test>
> <Meter>
> <test>first
> occurence</test>
> </Meter>
> </Asset>
> </Address>
> </Meterpoint>
> <Appointment>
> <test>first occurence</test>
> </Appointment>
> <Name>
> <test>first occurence</test>
> <Address>
> <test>first occurence</test>
> </Address>
> </Name>
> <!--Second set of data.............>
> </Transaction>
> <Transaction>
> <test>Second occurence</test>
> <Meterpoint>
> <test>Second occurence</test>
> <Address>
> <test>Second occurence</test>
> <Asset>
> <test>Second occurence</test>
> <Meter>
> <test>Second
> occurence</test>
> </Meter>
> </Asset>
> </Address>
> </Meterpoint>
> <Appointment>
> <test>Second occurence</test>
> </Appointment>
> <Name>
> <test>Second occurence</test>
> <Address>
> <test>Second occurence</test>
> </Address>
> </Name>
> </Transaction>
> </Header>
>
> Thanks in Advance
> Sreekanth
>
> -----Original Message-----
> From: Michael Kay [mailto:mike@xxxxxxxxxxxx]
>
> Sent: Wednesday, April 27, 2005 1:44 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: xsl apply template is not working
>
> <xsl:for-each select="Transaction">
> <Transaction>
> <xsl:apply-templates select="Transaction"/>
>
> Within for-each, the context node is a Transaction.
>
> Your apply-templates is short for select="./child::Transaction", but a
> Transaction does not have any Transaction children.
>
> Michael Kay
> http://www.saxonica.com/
>
>
> > -----Original Message-----
> > From: sreekanth.gangula@xxxxxxxxx
> > [mailto:sreekanth.gangula@xxxxxxxxx]
> > Sent: 27 April 2005 08:13
> > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > Subject: xsl apply template is not working
> >
>
> >
>
> > Hi All,
> > My apply template is not working.
> > <xsl:apply-templates select="Header"/> is going inside the
> >
>
> > <xsl:template match="Header">
> > <xsl:copy-of select="test"/>
> > </xsl:template>
> > But the following apply templates are not going inside
> template match:
> > <xsl:apply-templates select="Transaction"/> <xsl:apply-templates
>
> > select="Meterpoint"/> <xsl:apply-templates select="Address"/> Is not
>
> > going inside the corresponding template match.
> > Could anyone please help me.
> > My Source XML:
> > <Root>
> > <Header>
> > <test>Sreekanth</test>
> > </Header>
> > <Transaction>
> > <test1>Sreekanth</test1>
> > </Transaction>
> > <Meterpoint>
> > <test>Sreekanth</test>
> > </Meterpoint>
> > <Address>
> > <test>Sreekanth</test>
> > </Address>
> > <Asset>
> > <test>Sreekanth</test>
> > </Asset>
> > <Transaction>
> > <test>Sreekanth</test>
> > </Transaction>
> > <Meterpoint>
> > <test>Sreekanth</test>
> > </Meterpoint>
> > <Address>
> > <test>Sreekanth</test>
> > </Address>
> > <Asset>
> > <test>Sreekanth</test>
> > </Asset>
> > <Transaction>
> > <test>Sreekanth</test>
> > </Transaction>
> > <Meterpoint>
> > <test>Sreekanth</test>
> > </Meterpoint>
> > <Address>
> > <test>Sreekanth</test>
> > </Address>
> > <Asset>
> > <test>Sreekanth</test>
> > </Asset>
> > </Root>
> >
>
> > My XSL:
> > <?xml version="1.0"?>
> > <xsl:stylesheet version="1.0"
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> > <xsl:output method="xml"/>
> > <xsl:template match="Root">
> > <IDOC>
> > <HEADER>
> > <xsl:apply-templates select="Header"/>
> > <xsl:for-each select="Transaction">
> > <Transaction>
> > <xsl:apply-templates
> > select="Transaction"/>
> > <MeterPoint>
> >
>
> >
>
> >
>
> > <Address>
> >
>
> >
>
> > <xsl:apply-templates select="Address"/>
> > </Address>
> > <Asset>
> >
>
> >
>
> > <xsl:apply-templates select="Asset"/>
> > </Asset>
> > </MeterPoint>
> > </Transaction>
> > </xsl:for-each>
> > </HEADER>
> > </IDOC>
> > </xsl:template>
> > <xsl:template match="Header">
> > <xsl:copy-of select="test"/>
> > </xsl:template>
> > <xsl:template match="Transaction">
> > <xsl:copy-of select="test1"/>
> > </xsl:template>
> > <xsl:template match="Meterpoint">
> > <xsl:copy-of select="test"/>
> > </xsl:template>
> > <xsl:template match="Address">
> > <xsl:copy-of select="test"/>
> > </xsl:template>
> > <xsl:template match="Asset">
> > <xsl:copy-of select="test"/>
> > </xsl:template>
> > </xsl:stylesheet>
> >
>
> >
>
> >
>
> > Confidentiality Notice
> >
>
> >
>
> > The information contained in this electronic message and any
>
> > attachments to this message are intended for the exclusive
> use of the
>
> > addressee(s) and may contain confidential or privileged information.
>
> > If you are not the intended recipient, please notify the sender at
>
> > Wipro or Mailadmin@xxxxxxxxx immediately and destroy all copies of
>
> > this message and any attachments.
>
>
>
>
> Confidentiality Notice
>
>
> The information contained in this electronic message and any
> attachments to this message are intended
> for the exclusive use of the addressee(s) and may contain
> confidential or privileged information. If
> you are not the intended recipient, please notify the sender
> at Wipro or Mailadmin@xxxxxxxxx immediately
> and destroy all copies of this message and any attachments.
|