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 March 14th, 2006, 02:31 AM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to add an attribute

I have this element for example:

<catalog id="xxx" name="yyy">

..and I want to add the the attribute category for example:

<catalog id="xxx" name="yyy" category="zzz">

Currently I have this code and it works but is there a better way?

<xsl:template match="catalog /@*">
   <xsl:attribute name="category">zzzz</xsl:attribute>
   <xsl:for-each select=".">
      <xsl:copy-of select="."/>
   </xsl:for-each>
</xsl:template>



 
Old March 14th, 2006, 05:14 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If your element has 10 existing attributes then you're calling this template 10 times, and adding the category element 10 times, and then relying on the duplicates being eliminated. That's a bit of a roundabout way of doing it. If there are no existing attributes, your code won't be invoked at all. Also "<xsl:for-each select=".">" is a no-op.

Do the processing at the element level:

<xsl:template match="catalog">
  <...>
    <xsl:copy-of select="@*"/>
    <xsl:attribute name="category">zzzz</xsl:attribute>
  </...>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 14th, 2006, 11:51 PM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This doesn't seem to work...what it does is it adds the attrbutes to the parent element of catalog and removes all the children including catalog.

<xsl:template match="catalog">
   <xsl:copy-of select="@*"/>
   <xsl:attribute name="category">zzzz</xsl:attribute>
</xsl:template>

 
Old March 15th, 2006, 05:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You probably want to copy the element as well

<xsl:template match="catalog">
<xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:attribute name="category">zzzz</xsl:attribute>
</xsl:copy>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 15th, 2006, 09:43 PM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help.....Hmmm . . this still doesn't work. It copies the catalog element and adds the attributes but it ignores any other element...for example:

<catalog id="xxx" name="yyy">
   <cd first="1" second="2">
      <title>Empire Burlesque</title>
      <artist>Bob Dylan</artist>
      <country>USA</country>
      <price>10.90</price>
      <year>1985</year>
   </cd>
   <cd>
      <title>Hide your heart</title>
      <artist>Bonnie Tyler</artist>
      <country>UK</country>
      <price>9.90</price>
      <year>1988</year>
      <month fyear="1990">1988</month>
   </cd>
</catalog>

the output of the code you gave will be:

<catalog id="xxx" name="yyy" category="zzzz"/>

 
Old March 16th, 2006, 06:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I was trying to answer the question you asked, not to give you a complete working stylesheet.

Do a search for "XSLT identity template", or look for it in your favourite Wrox textbook.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add an attribute to each element bartnowa XSLT 3 November 8th, 2007 12:12 PM
How to add attribute to unbounded element 2BOrNot2B XML 4 January 16th, 2007 03:42 PM
XSLT, add attribute ik XSLT 6 August 22nd, 2006 11:03 AM
Attribute.add and Refresh Problem Ardvisoor ASP.NET 2.0 Professional 3 July 25th, 2006 12:10 PM
Add an attribute to existing table peter_budo SQL Language 9 April 24th, 2005 01:35 PM





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