Click here to Skip to main content
15,897,273 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:
XML
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<item>
<title>this is title</title>
<description>about us</description>
<category/>
<link>www.google.co.in</link>
</item>
<item>
<title>Home</title>
<description>Home page</description>
<category>web<category/>
<link>www.yahoo.com</link>
</item>
</channel>
</rss>


Output xml:
<pre lang="XML">
<root version="2.0" xmlns:test="http://search.yahoo.com/mrss/">
<test:item title="this is title" description="about us" link="www.google.co.in" />
<test:item this is title="Home" description="Home page" category="web" link="www.yahoo.com" />
</root>

xslt :
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="http://search.yahoo.com/mrss/">
<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:attribute>
</xsl:if>

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

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

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

</xsl:element>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Posted
Updated 27-Dec-11 23:05pm
v2

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