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 July 21st, 2003, 02:31 AM
Registered User
 
Join Date: Jul 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem using <xsl:attribute>

hi!

i'm quite new with XSLT. here's my problem.

xslt code:
<xsl:template match="tag">
<newtag>
<xsl:attribute name="id">
<xsl:value-of select="@attribute"/>
</xsl:attribute>
<xsl:apply-templates/>
</newtag>
</xsl:template>


source:
<tag attribute="xyz"/>
<tag/>


output:
<newtag id="xyz"/>
<newtag id=""/>


correct output:
<newtag id="xyz"/>
<newtag/>


what modification/s should i do with the xslt to get the correct output?

thanks!
 
Old July 21st, 2003, 03:08 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you only want the attribute 'id' to appear if its value is not an empty string then you can use
Code:
<xsl:if>
or
Code:
<xsl:choose>
:
Code:
<xsl:template match="tag">
<newtag>
<xsl:if test="@attribute != ''">
<xsl:attribute name="id">
<xsl:value-of select="@attribute"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</newtag>
</xsl:template>
Code:
<xsl:choose>
is for when you need more than two options, where in traditional languages you would use an if then else if or a switch (C/JavaScript) or select (VB) statement.


--

Joe
 
Old July 21st, 2003, 03:13 AM
Registered User
 
Join Date: Jul 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks Joe! :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance for <xsl:import> and <xsl:include> vikkiefd XSLT 2 April 16th, 2008 08:06 AM
<xsl:attribute> vs {} berna_isct XSLT 1 April 6th, 2006 03:30 PM
<xsl:for-each> with a variable select="attribute" BeneathClouds XSLT 1 August 1st, 2005 03:36 AM
name attribute for <xsl:element> rushman XSLT 2 June 9th, 2005 09:04 AM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM





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