RE: [xsl] Reading two xmls and changing the attribute names in the first xml conditionally

Subject: RE: [xsl] Reading two xmls and changing the attribute names in the first xml conditionally
From: "Lakshmi narayana" <lchintala@xxxxxxxxxxxx>
Date: Wed, 6 Jul 2005 15:22:31 +0500
Hi,
I have one more question regarding this problem only.
If I don't have the Type attribute for the first xml, is it possible to add
the type
attribute to the nodes based on the node name like...if node name is A, then
type="AGroup".
Then after adding the node, I have to map the attributes based on the second
xml.

I mean to say, dynamically I have to add Type attribute to the nodes in the
first xml and
then map the attributes based on the second xml.

-----Original Message-----
From: Joris Gillis [mailto:roac@xxxxxxxxxx]
Sent: Wednesday, July 06, 2005 2:06 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Reading two xmls and changing the attribute names in
the first xml conditionally


Hi,

Tempore 10:34:27, die 07/06/2005 AD, hinc in
xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Lakshmi narayana
<lchintala@xxxxxxxxxxxx>:

> 6) Can any one send me the xsl code for this scenario.
consider that the scond file is named 'second.xml' and the stylesheet is
processed on the first file.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
     <xsl:output indent="yes" method="xml"/>

<xsl:key name="newName" match="destination"
use="concat(local-name(../..),../source/@name)"/>

<xsl:template match="node()">
	<xsl:copy>
		<xsl:apply-templates select="@*">
			<xsl:with-param name="Type" select="@Type"/>
		</xsl:apply-templates>
		<xsl:apply-templates select="node()"/>
	</xsl:copy>
</xsl:template>

<xsl:template match="@*">
<xsl:param name="Type"/>
<xsl:variable name="name" select="local-name()"/>
<xsl:variable name="newname">
	<xsl:for-each select="document('second.xml')">
		<xsl:value-of select="key('newName',concat($Type,$name))/@name"/>
	</xsl:for-each>
</xsl:variable>
<xsl:attribute name="{$newname}"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>

<xsl:template match="@Type"><xsl:copy/></xsl:template>

</xsl:stylesheet>


will produce:

<A1 A-Element-Name="A1" A-sequence="1" A-Description="optional"
Type="AGroup">
	<B1 B-Element-Name="B1" B-sequence="101" B-order="first" Type="BGroup"/>
	<B2>
		<C1 C-Element-Name="C1" C-sequence="201" C-order="Sec" Type="CGroup"/>
		<C2 C-Element-Name="C2" C-sequence="202" C-order="Sec" Type="CGroup">
			<C3/>
			<D1 D-Element-Name="D1" D-Min="1" D-Max="1" D-Reference="Trr"
D-Predicate="" Type="DGroup">
				<C2 C-Element-Name="C2" C-sequence="202" C-order="Sec" Type="CGroup"/>
			</D1>
		</C2>
	</B2>
</A1>


regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Spread the wiki (http://www.wikipedia.org)

Current Thread