[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] xsl:copy-reposting


Subject: RE: [xsl] xsl:copy-reposting
From: "Andrew Welch" <awelch@xxxxxxxxxxxxxxx>
Date: Fri, 9 Aug 2002 14:56:34 +0100

Hi,

Ive been off-list for a few days so I haven't been following this
thread.  This will hopefully do what you need, all in one pass.

with this data:
<students>
	<student id = "101">
		<name>A</name>
		<address>XXX</address>
	</student>
	<student id = "201">
		<name>B</name>
		<address>YYY</address>
	</student>
	<student id = "301">
		<name>C</name>
		<address>XXX</address>
	</student>
	<student id = "401">
		<name>E</name>
		<address>YYY</address>
	</student>
</students>


and this stylesheet:
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="students">
   <xsl:for-each select="student">
    <xsl:choose>
      <xsl:when test="address='YYY'">
      <xsl:copy-of select="."/>
      <xsl:apply-templates select="." mode="change"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
</xsl:template>

<xsl:template match="student|name|@*" mode="change">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="change"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="address" mode="change">
  <address>Changed-<xsl:value-of select="."/></address>
</xsl:template>

</xsl:stylesheet>

you get this result:
<student id="101">
    <name>A</name>

    <address>XXX</address>
</student>

<student id="201">
    <name>B</name>

    <address>YYY</address>
</student>

<student id="201">
    <name>B</name>

    <address>Changed-YYY</address>
</student>

<student id="301">
    <name>C</name>

    <address>XXX</address>
</student>

<student id="401">
    <name>E</name>

    <address>YYY</address>
</student>

<student id="401">
    <name>E</name>

    <address>Changed-YYY</address>
</student>

which is what you were after :)

cheers
andrew

> -----Original Message-----
> From: Joeri Belis [mailto:joeri.belis@xxxxxxxxxxxx]
> Sent: 08 August 2002 09:46
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Cc: subbu@xxxxxxxxxxxx
> Subject: Re: [xsl] xsl:copy-reposting
> 
> 
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> 
> <xsl:output method="xml" indent="yes"/>
> <xsl:strip-space elements="*"/>
> 
> <xsl:template match="/">
>     <xsl:apply-templates select="students"/>
> </xsl:template>
> <!-- ********************************************* -->
> 
> <xsl:template match="students">
>     <xsl:apply-templates select="student"/>
> </xsl:template>
> <!-- ********************************************* -->
> 
> <xsl:template match="student">
>   <xsl:copy>
>     <xsl:copy-of select="@*"/>
>     <xsl:copy-of select="*"/>
>   </xsl:copy>
>   <xsl:if test="address = 'YYY'">
>    <xsl:copy>
>      <xsl:copy-of select="@*"/>
>      <xsl:copy-of select="*[name() != 'address']"/>
>      <xsl:apply-templates select="address"/>
>    </xsl:copy>
>   </xsl:if>
> </xsl:template>
> 
> <!-- ********************************************* -->
> 
> <xsl:template match="address">
>   <xsl:copy>
>     <xsl:copy-of select="@*"/>
>      <xsl:value-of select="'changed-YYYY'"/>
>   </xsl:copy>
> </xsl:template>
> 
> <!-- ********************************************* -->
> <!-- ********************************************* -->
> 
> </xsl:stylesheet>
> 
> ----- Original Message -----
> From: <subbu@xxxxxxxxxxxx>
> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> Sent: Thursday, August 08, 2002 10:19 AM
> Subject: [xsl] xsl:copy-reposting
> 
> 
> > hello people.. Sorry for reposting...
> > following the suggestion given by Jeff and Mike,
> > I decided to go for the two-phase transformation..
> > I am stuck up in the very first phase :)
> > The following is my doubt.
> > I have the following xml with me..
> > <?xml version="1.0"?>
> > <students>
> > <student id = "101">
> > <name>A</name>
> > <address>XXX</address>
> > </student>
> > <student id = "201">
> > <name>B</name>
> > <address>YYY</address>
> > </student>
> > <student id = "301">
> > <name>C</name>
> > <address>XXX</address>
> > </student>
> > <student id = "401">
> > <name>E</name>
> > <address>YYY<address>
> > </student>
> > </students>
> >
> > now what i have to do is .. convert this into an xml in 
> such a way that if
> > address is 'YYY' then add the same node again with a 
> changed address ..
> > so what i want is after the
> >
> > <student id = "201">
> > <name>B</name>
> > <address>YYY</address>
> > </student>
> >
> > i want another Student Element Set which looks like
> >
> > <student id = "201">
> > <name>B</name>
> > <address>Changed-YYY</address>
> > </student>
> >
> > I looked at the archives came close to the solution but 
> dint get what i
> > wanted  :(...
> > the final transformed XML would look like ..
> >
> > <students>
> > <student id = "101">
> > <name>A</name>
> > <address>XXX</address>
> > </student>
> > <student id = "201">
> > <name>B</name>
> > <address>YYY</address>
> > </student>
> > <student id = "201">
> > <name>B</name>
> > <address>Changed-YYY</address>
> > </student>
> > <student id = "301">
> > <name>C</name>
> > <address>XXX</address>
> > </student>
> > <student id = "401">
> > <name>E</name>
> > <address>YYY<address>
> > </student>
> >         <student id = "401">
> > <name>E</name>
> > <address>Changed-YYY<address>
> > </student>
> > </students>
> >
> > The Changed-YYY is something i read from a template passing YYY as
> parameter.
> > its a paramater ( $Changed-YYY)
> >
> > I am sorry to trouble you .. but wud be happy if someone 
> suggests me a way
> to
> > come out of it
> > Thanx  a ton!!
> >
> >
> >
> >
> >
> > --------------------------------------------------------------
> > Sent with "Me-Mail", Boltblue's FREE mobile messaging service.
> > http://www.boltblue.com
> >
> >
> >  XSL-List info and archive:  
http://www.mulberrytech.com/xsl/xsl-list
>
>
>



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list





---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.381 / Virus Database: 214 - Release Date: 02/08/2002
 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.381 / Virus Database: 214 - Release Date: 02/08/2002
 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords
xml