Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
>Hi ,

I have been tasked for Creating Dynamic attributes to elements. Below is my code.

getting the below result, but if any new attributes comes then i need to add that attribute into my xslt everytime. So, I want to make it dynamic xslt.

Please help me on this.

Input XML:
<rss version="2.0" xmlns:media="">
<title>this is title</title>
<description>about us</description>
<description>Home page</description>

Output xml:
<pre lang="XML">
<root version="2.0" xmlns:test="">
<test:item title="this is title" description="about us" link="" />
<test:item this is title="Home" description="Home page" category="web" link="" />

xslt :
<xsl:stylesheet version="1.0" xmlns:xsl="">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" media-type="text/xml; charset=UTF-8" />
<xsl:template match='/'>
<root version="2.0" xmlns:test="">
<xsl:for-each select="rss/channel/item">
<xsl:element name="test:item">

<xsl:if test="title !=''">
<xsl:attribute name ="title">
<xsl:value-of select="title"/>

<xsl:if test="description !=''">
<xsl:attribute name ="description">
<xsl:value-of select="description"/>

<xsl:if test="category !=''">
<xsl:attribute name ="category">
<xsl:value-of select="category"/>

<xsl:if test="link !=''">
<xsl:attribute name ="link">
<xsl:value-of select="link"/>

Updated 27-Dec-11 23:05pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900