Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2011, 12:52 AM
Registered User
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Creating Dynamic attributes to elements

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="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:


<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 :

<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>
 
Old December 28th, 2011, 03:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Use template rules like this:

Code:
<xsl:template match="item">
<test:item><xsl:apply-templates/></test:item>
</xsl:template>

<xsl:template match="item/*">
<xsl:attribute name="{name()}">
  <xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 28th, 2011, 05:13 AM
Registered User
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Smile thanks for ur reply,

Hi mhkay,

thanks for ur reply,

i tried with below xslt code, but it's not giving correct result. pls help.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:test="http://search.yahoo.com/mrss/">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" media-type="text/xml; charset=UTF-8" />

<xsl:template match="item">
<test:item>
<xsl:apply-templates/>
</test:item>
</xsl:template>

<xsl:template match="item/*">
<root version="2.0" xmlns:test="http://search.yahoo.com/mrss/">
<xsl:for-each select="rss/channel/item">
<xsl:element name="test:item">

<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>

</xsl:element>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>

giving below result.

<test:item xmlns:test="http://search.yahoo.com/mrss/">
<root version="2.0" />
<root version="2.0" />
.....

thanks.
 
Old December 28th, 2011, 08:22 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You don't need any of your for-each, just use the suggested templates and one template creating the root element if needed and perhaps one suppressing the processing of empty elements:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:test="http://search.yahoo.com/mrss/"
  version="1.0">
  
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
  
<xsl:template match="/rss">
  <rss version="{@version}">
    <xsl:apply-templates/>
  </rss>
</xsl:template>

<xsl:template match="item">
  <test:item>
    <xsl:apply-templates/>
  </test:item>
</xsl:template>

<xsl:template match="item/*[normalize-space()]">
  <xsl:attribute name="{name()}">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="item/*[not(normalize-space())]"/>

</xsl:stylesheet>
That stylesheet transforms the input
Code:
<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>
into
Code:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:test="http://search.yahoo.com/mrss/" version="2.0">
   <test:item title="this is title" description="about us" link="www.google.co.in"/>
   <test:item title="Home" description="Home page" category="web" link="www.yahoo.com"/>
</rss>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Elements (EMPTY) and Attributes #Required Ian ORourke XML 10 July 14th, 2016 10:07 PM
Creating Dynamic Elements with Values from Other Element values chilly XSLT 18 February 24th, 2011 10:31 AM
sequenced elements to attributes zkent XSLT 0 April 19th, 2006 08:50 AM
Elements and attributes Morrislgn XSLT 1 June 20th, 2005 11:13 AM
Elements w same name and their attributes supafly XSLT 1 May 30th, 2005 06:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.