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 September 6th, 2010, 08:52 PM
Registered User
 
Join Date: Sep 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Added attribute to specific element

I am somewhat new to XSLT, and I have a situation where depending on certain conditions I want to convert XML like:

<SomeElement>
<ChildElement/>
<ChildElement/>
<ChildElement/>
...
</SomeElement>

to:

<SomeElement value="something">
<ChildElement/>
<ChildElement/>
<ChildElement/>
...
</SomeElement>


I don't want to specify the child elements in the template that modifies the element, as these may change.

What kind of template with perform this type of transformation?
 
Old September 7th, 2010, 02:18 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Sounds like you need to start with some basic tutorials. Try http://www.w3schools.com/xsl/ for some basic guidance.

Once you've given some things a go and if you still can't work out what to do then come back and show us what you've tried.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old September 7th, 2010, 03:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Use the "modified identity transform" design pattern. Use a default template rule that copies elements unchanged (the "identity template"):

Code:
<xsl:template match="*">
  <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>
and another that changes the elements you want to change:

Code:
<xsl:template match="SomeElement">
  <xsl:copy>
     <xsl:attribute name="newatt" select="'somevalue'"/>
     <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 7th, 2010, 11:59 AM
Registered User
 
Join Date: Sep 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks mhkay,

I was able to get my transform working.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find attribute for specific element? QuadFather XML 1 April 7th, 2010 12:41 PM
Finding if an attribute has a specific value mphare XSLT 1 February 2nd, 2009 06:57 PM
sort on specific attribute kgoldvas XSLT 2 July 17th, 2007 09:42 AM
Access specific Element and its Child elements! suersh79 XML 3 November 22nd, 2006 12:58 AM
attribute xmlns added in included xsl Kabe XSLT 6 November 23rd, 2004 12:24 PM





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