This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: RE: merging generic elements in a parent-child relationship Part II
- From: "Matias Woloski" <woloski at sion dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Thu, 27 Jun 2002 18:21:37 -0300
- Subject: RE: RE: [xsl] merging generic elements in a parent-child relationship Part II
- Reply-to: xsl-list at lists dot mulberrytech dot com
thanks Dimitre!
But is it possible to apply a unique stylesheet to the originial xml I've
posted? Or I need to do this in two steps?
this is the original one
<root>
<Persona id="abc" idCountry="1"/>
<Persona id="abcd" idCountry="1"/>
<b id="b1" idPersona="abc"/>
<b id="b2" idPersona="abc"/>
<b id="b3" idPersona="abcd"/>
<c id="c1" idb="b1"/>
</root>
and this is my styleheet which do the original job. How do you insert the
Muenchian method here?
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="TopElement" select="'Persona'" />
<xsl:template match="root">
<root>
<xsl:apply-templates select="/*/*[name() = $TopElement]"/>
</root>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:variable name="currentTag" select="name()"/>
<xsl:copy-of select="@*"/>
<xsl:apply-templates
select="../*[@*[name()=concat('id',$currentTag)]=current()/@id]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
thanks a lot!
Matias
> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com
> [mailto:owner-xsl-list@lists.mulberrytech.com]On Behalf Of Dimitre
> Novatchev
> Sent: jueves, 27 de junio de 2002 17:26
> To: xsl-list@lists.mulberrytech.com
> Subject: Re: RE: [xsl] merging generic elements in a parent-child
> relationship Part II
>
>
> --- "Matias Woloski" <woloski at sion dot com> wrote:
>
> >
> > I've solved both problems!
> > I did this for the param problem
> > <xsl:apply-templates select="//*[name()=$TopElement]"/>
> > so this will choose all the elements whose name is stored in
> > TopElement
> > and the id problem
> > I added a variable currentTag
> > <xsl:variable name="currentTag" select="name()"/>
> > ...
> > <xsl:apply-templates
> > select="../*[@*[name()=concat('id',$currentTag)]=current()/@id]"/>
> >
> > But I have a question now...
> > I have the XML transformed like this
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <root>
> > <Persona id="abc" idCountry="1">
> > <b id="b1" idPersona="abc"></b>
> > <b id="b2" idPersona="abc"></b>
> > <c id="c1" idb="b1" idPersona="abc">
> > </c>
> > </Persona>
> > <Persona id="abcd" idCountry="1">
> > <b id="b3" idPersona="abcd">
> > </b>
> > </Persona>
> > </root>
> >
> > I want to wrap each collection of elements into other element. Like
> > this
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <root>
> > <PersonaGroup>
> > <Persona id="abc" idCountry="1">
> > <bGroup>
> > <b id="b1" idPersona="abc"></b>
> > <b id="b2" idPersona="abc"></b>
> > </bGroup>
> > <cGroup>
> > <c id="c1" idb="b1" idPersona="abc"></c>
> > </cGroup>
> > </Persona>
> > <Persona id="abcd" idCountry="1">
> > <bGroup>
> > <b id="b3" idPersona="abcd"></b>
> > </bGroup>
> > </Persona>
> > </PersonaGroup>
> > </root>
> >
> > thanks!
> >
> > ps: Dimitre thanks for answering... actually I've already solved it
> > in
> > a
> > similar way... do you know the last question?
>
>
> Yes, this is a grouping problem. Bellow I use the Muenchian method:
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:key name="kPers-Names" match="Persona/*"
> use="concat(generate-id(..), '||', name())"/>
>
> <xsl:strip-space elements="*"/>
>
> <xsl:output omit-xml-declaration="yes" indent="yes"/>
>
> <xsl:template match="/">
> <root>
> <xsl:apply-templates/>
> </root>
> </xsl:template>
>
> <xsl:template match="Persona">
> <xsl:copy>
> <xsl:copy-of select="namespace::* | @*"/>
> <xsl:for-each select="*[generate-id()
> =
> generate-id(key('kPers-Names',
> concat(generate-id(..),
> '||',
> name()
> )
> )[1])]">
>
> <xsl:element name="{name()}Group">
> <xsl:copy-of select="key('kPers-Names',
> concat(generate-id(..),
> '||',
> name()
> )
> )"/>
> </xsl:element>
> </xsl:for-each>
> </xsl:copy>
> </xsl:template>
> </xsl:stylesheet>
>
> When applied to your original source xml:
>
> <root>
> <Persona id="abc" idCountry="1">
> <b id="b1" idPersona="abc"></b>
> <b id="b2" idPersona="abc"></b>
> <c id="c1" idb="b1" idPersona="abc">
> </c>
> </Persona>
> <Persona id="abcd" idCountry="1">
> <b id="b3" idPersona="abcd">
> </b>
> </Persona>
> </root>
>
> The result of the transformation is exactly as wanted:
>
> <root>
> <Persona id="abc" idCountry="1">
> <bGroup>
> <b id="b1" idPersona="abc"/>
> <b id="b2" idPersona="abc"/>
> </bGroup>
> <cGroup>
> <c id="c1" idb="b1" idPersona="abc"/>
> </cGroup>
> </Persona>
> <Persona id="abcd" idCountry="1">
> <bGroup>
> <b id="b3" idPersona="abcd"/>
> </bGroup>
> </Persona>
> </root>
>
>
> Cheers,
> Dimitre Novatchev.
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! - Official partner of 2002 FIFA World Cup
> http://fifaworldcup.yahoo.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