[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Get and change element attributes

Subject: Re: Get and change element attributes
From: Mukul Gandhi <mukulw3@xxxxxxxxx>
Date: Sun, 14 Sep 2003 20:58:39 -0700 (PDT)
change element id
Hi Mark,
 To my best understanding, I have assumed the
following XMLs (the real XML files must be
conceptually similar..)--

source.xml
----------
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <div    id='1' Tag='aaa'>
   </div>
   <div     id='2' Tag='bbb'>
   </div>
   <div     id='3' Tag='ccc'>
   </div>
   <div     id='4' Tag='ddd'>
   </div>
   <div     id='5' Tag='eee'>
   </div>
   <div     id='6' Tag='fff'>
   </div>
</root>

Templates.xml
-------------
<?xml version="1.0" encoding="UTF-8"?>
<Templates>
    <div    id='1' Tag='stuff1'>
    </div>
    <div     id='2' Tag='stuff2'>
    </div>
    <div     id='3' Tag='stuff3'>
    </div>
    <div     id='4' Tag='stuff4'>
    </div>
    <div     id='5' Tag='stuff5'>
    </div>
    <div     id='6' Tag='stuff6'>
    </div>
</Templates>

You wish to apply a XSL to source.xml , and change the
attribute name 'Tag' to value of 'Tag' attribute in
Templates.xml file, and copy rest of XML contents to
output as it is..

Below is the complete XSL for this transformation --

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>

<xsl:variable name="Templates"
select="document('Templates.xml')"/>
	
    <xsl:template match="@* | node()">
	<xsl:copy>
	  <xsl:apply-templates select="@* | node()"/>
	</xsl:copy>
    </xsl:template>
	
    <xsl:template match="div">
	<xsl:variable name="attr" select="@id"/>
	<xsl:variable name="attrval" select="@Tag"/>
	   <xsl:variable name="newattr"
select="$Templates/Templates/div[@id =$attr]/@Tag"/>
	   <xsl:element name="div">
	     <xsl:attribute name="id"><xsl:value-of
select="@id"/>
             </xsl:attribute>
	     <xsl:attribute name="{$newattr}">
		<xsl:value-of select="$attrval" />
	     </xsl:attribute>
	   </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Regards,
Mukul


--- Mark Oliver <mark@xxxxxxxxxxxxxxxxx> wrote:
> 
> 
> 
> 
> 	1 of 1	{ SAVEDATE \@ "d/MM/yy" \* MERGEFORMAT
> }0/00/00
> 		{ FILENAME \p  \* MERGEFORMAT }Q:\Internal\Word
> Termplates\Normal.dot
> 
> Hi I'm new at XSLT and am trying to find and replace
> attributes in 
> particular elements with new values. 
> There are two XML documents in the transformation
> that I am trying to 
> write. The main input XML document contains data;
> the second XML 
> document is passed in as a parameter and contains
> output structure 
> elements.
> 
> I would like to be able to find elements in the
> parameter document 
> (Templates) that is passed into the transformation
> and select div 
> elements by id and change their Tag attributes and
> output them. 
> 
> This is how I?m adding the parameter document.
> <xsl:variable name="Templates" 
> select="document('Templates.xml')" />
> 
> Below is an example of the parameter XML document.
> 
> <Templates>
> 
> 	<div    id='1' Tag='stuff1'>
> 		<span
> 	</div>
> 
> 
> 	<div     id='2' Tag='stuff2?>
> 	</div>
> 
> 	<div     id='3' Tag='stuff3?>
> 	</div>
> 
> 	<div     id='4' Tag='stuff4?>
> 	</div>
> 
> 	<div     id='5' Tag='stuff5?>
> 	</div>
> 
> 	<div     id='6' Tag='stuff6?>
> 	</div>
> 
> </Templates>
> 
> I would really love some help with this one it has
> me stuck.
> 
> Mark.
> 
> 
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.