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 November 3rd, 2005, 08:11 AM
Registered User
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copying Source Node attributes to output node


I am trying to apply an XSLT stylesheet to convert a source XML Doc into a HTML Checkbox TAG. The source XML can have a number of attributes. Hence i want to loop thry the sourcenode's attributes and add it to the output HTML <INPUT> TAG's attributes.
The source XML
<?xml version="1.0" encoding="iso-8859-1"?>
<ww:checkbox name='' value='' required='no' selected='no' message='the field is not selected'/>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ww='http://www.fitpro.com'>
<xsl:output method="html"/>
 <xsl:template match="/">
<xsl:template match="ww:checkbox">
    <xsl:element name="input">
        <xsl:attribute name='type'>checkbox</xsl:attribute>
        <xsl:attribute name='name'>
            <xsl:value-of select="@name"/>
        <xsl:attribute name='value'>
            <xsl:value-of select="@value"/>
        <xsl:for-each select="attribute::*">
            <xsl:attribute name="name(.)"><xsl:value-of select="."/></xsl:attribute>

=<input xmlns:ww="http://www.fitpro.com" type="checkbox" name="checkbox1" value="agree"> But i want all the other attributes sych as required, selected, message etc to appear in the output. Is that possible?
Old November 3rd, 2005, 08:46 AM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

I corrected two errors:

(a) added a namespace declaration in the source XML

(b) changed <xsl:attribute name="name(.)"> to <xsl:attribute name="{name(.)}">

and it gave the required output. Without the curly braces the attribute has an invalid name, and the XSLT 1.0 spec allows silent recovery from that error by simply ignoring the attribute. I expect that's what happened to you.

However you can achieve the result more easily by writing the template body as:

<input><xsl:copy-of select="@*"/></input>

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Old November 3rd, 2005, 09:46 AM
Registered User
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks for that. I was seeing this curly brackets in some samepl code but did not figure out its importance. That was very very helpful. I am trying to develop an XML/XSLT driven Presentation layer framework components in CFC for some CFMX based applications and integrate it into a fusebox framework. My next step is to go a grab a copy of your XSLT and XPath Books..

Many thanks
Sathish Kumar

Similar Threads
Thread Thread Starter Forum Replies Last Post
changing output for one node in XSLT shane_carty XSLT 3 February 27th, 2008 05:28 AM
How to delete a xml node with all attributes and s vishnu108mishra XML 2 November 14th, 2007 04:19 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Use of Attributes prop.ty for a OOXML node Gianni Giaccaglini Excel VBA 1 September 24th, 2007 05:05 AM
Child Node Output Neal XSLT 0 June 7th, 2006 03:14 AM

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