Groups | Blog | Home
all groups > dotnet xml > january 2005 >

dotnet xml : creating dynamic elements


guevara_81 NO[at]SPAM yahoo.com
1/18/2005 1:13:35 PM
Hello,

I have an xml doc that is formed like this

<doc>
<recSet>
<elem attrib1="Text" attrib2="FOO"><elemVal>SOME
VAL</elemVal></elem>
</recSet>
</doc>

and I want to transform it to look like this

<newDoc>
<newRecSet>
<Text>SOME VAL</Text>
</newRecSet>
</newDoc>

I can get up to the part in my XSLT to find the attribute I want, but
to get the value of the attribute and assign that, dynamically, to an
element is causing me all kinds of grief. Does anyone have any
ideas/suggestions?

Much Appreciation,
A
swapna guddanti [MSFT]
1/18/2005 4:33:57 PM
you can have a variable which selects the attribute value and then use that
variable value for the name of the element as shown below.
<?xml version="1.0" encoding="UTF-8" ?>

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

<xsl:template match="/">

<xsl:variable name="a" select="doc/recSet/elem/@attrib1"></xsl:variable>

<xsl:element name="newDoc">

<xsl:element name="newRecSet">

<xsl:element name="{$a}">

<xsl:value-of select="doc/recSet/elem/elemVal"/>

</xsl:element>

</xsl:element>

</xsl:element>

</xsl:template>

</xsl:stylesheet>


[quoted text, click to view]

guevara_81 NO[at]SPAM yahoo.com
1/19/2005 9:47:03 AM
Thank you, that was exactly what I was looking for.
Many thanks again,
A
AddThis Social Bookmark Button