Re: [xsl] Rename attributes and nodes maybe using variables

Subject: Re: [xsl] Rename attributes and nodes maybe using variables
From: Mukul Gandhi <mukulw3@xxxxxxxxx>
Date: Wed, 9 Jul 2003 07:17:33 -0700 (PDT)
I was trying to solve the problem you gave.. and ended
up with a stylesheet below.. i have'nt written further
unless you tell us whether you can relax certain
conditions..

<?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:template match="COLLECTION">
    <COLLECTION>
	<xsl:apply-templates select="PHOTOS"/>
	<xsl:apply-templates select="REGIONSET"/>
    </COLLECTION>
   </xsl:template>
   <xsl:template match="PHOTOS">
     <PHOTOS>
       <xsl:for-each select="*">
	 <xsl:variable name="nam_e">
	   <xsl:value-of select="@label | @name"/>
	 </xsl:variable>
	 <xsl:variable name="ur_l">
	   <xsl:value-of select="@place | @url"/>
	 </xsl:variable>
	 <xsl:variable name="old_name">
	   <xsl:value-of select="name(.)"/>
	 </xsl:variable>
	 <xsl:choose>
	   <xsl:when test="@label and @place">
	  <IMAGE name="{$nam_e}" url="{$ur_l}"
oldname="{$old_name}" old_atr_name="label"
old_atr_url="place" />
	  </xsl:when>
	  <xsl:when test="@label and @url">
	    <IMAGE name="{$nam_e}" url="{$ur_l}"
oldname="{$old_name}" old_atr_name="label"
old_atr_url="url"/>
	  </xsl:when>
	  <xsl:when test="@name and @place">
	    <IMAGE name="{$nam_e}" url="{$ur_l}"
oldname="{$old_name}" old_atr_name="name"
old_atr_url="place"/>
 	  </xsl:when>
	  <xsl:when test="@name and @url">
	    <IMAGE name="{$nam_e}" url="{$ur_l}"
oldname="{$old_name}" old_atr_name="name"
old_atr_url="url"/>
	  </xsl:when>
	</xsl:choose>
     </xsl:for-each>
  </PHOTOS>
</xsl:template>
	
<xsl:template match="REGIONSET">

</xsl:template>

</xsl:stylesheet>

you have mentioned in your requirement, that there is
strict order of attributes.. this seems to be not
possible with XSLT.. XSLT does'nt recognize attributes
by position and as Jeni pointed, accessing attributes
by position will not guarantee a predictable result
with XSLT and is not uniform accross XSLT processors..

but, *a solution can be*, in this statement i have
written 
<xsl:value-of select="@label | @name"/>   (attribute
names are 'label' and 'name')

you can keep adding or ( | ) expressions as you come
to know a new attribute name.. you will need to keep
changing your stylesheet for this to work.. i guess a
new attribute name will not come so frequently, so
maintaining your stylesheet like this may work for
you..

the reverse transform will be similar to like this..

Regards,
Mukul



--- Kai Lienemann <Xenum81@xxxxxx> wrote:
> Hello again,
> 
> I'm not really fit in writing stylesheets but I need
> two for my programme!
> The one must form the XML-File so, my plugin can
> work with them, and the
> second has to undo the transformation of the first.
> 
> Lets describe the first:
> 
> In the XML-File are nodes with the name "IMAGE" and
> the first attribute
> should reflect the name and the second attribute the
> location of the image.
> The name of these attribute is not fix, but in the
> result the furst
> attribute should be named "name" and the second
> "url" and the old name
> should be stored in attribute so in the second
> transformation these
> attributes get there old name.
> In other parts of the File are Nodes with the
> node-name REGIONSET and the
> attribute "image". But in this attribute "image" I
> need not the name, but
> the location where I can find the file.
> So this attribute has to be replaced with the url of
> the node IMAGE where
> the value of the "image" and "name" are the same.
> (Maybe saving the url of
> the images in variables with the name of the image
> should be helpful) In the
> second stylesheet I want to undo this replacing and
> so the original name
> should be stored in the attribute "oldvalue". There
> can be more than one
> IMAGE-node and in more than one  REGIONSET-nodes can
> be used the same image.
> But at the first step of the transformation nodes
> like PHOTO, BILDCHEN, IMG,
> FOTO should be renamed with the name IMAGE and the
> old name should be stored
> in the attribute "oldname" (I asked this some mails
> before but in this case
> it is more difficult for me so I can't use it). All
> other nodes and
> attributes should be copied and not changed!
> 
> Input-Example:
> 
> <Collection>
>     <PHOTOS>
>         <FOTO label="test1"
> place="/vol/pictures/test1.jpg"/>
>         <PHOTO name="test2"
> url="/vol/pictures/test2.jpg"/>
>         <BILDCHEN name="test3"
> url="/vol/pictures/test3.jpg"/>
>         <IMG name="test4"
> url="/vol/pictures/test4.jpg"/>
>         <IMAGE name="test5"
> url="/vol/pictures/test5.jpg"/>
>     </PHOTOS>
>     <REGIONSET image="test2">
>         <RECT x="10" y="20" z="50" h="70">
>         Cup
>         </RECT>
>         <RECT x="20" y="30" z="60" h="20">
>         Sword
>         </RECT>
>     </REGIONSET>
> </COLLECTION>
> 
> 
> Output Example:
> 
> <Collection>
>     <PHOTOS>
>         <IMAGE name="test1"
> url="/vol/pictures/test1.jpg" oldname="FOTO"
> old_atr_name="label" old _atr_url="location"/>
>         <IMAGE  name="test2"
> url="/vol/pictures/test2.jpg"oldname="PHOTO"/>
>         <IMAGE  name="test3"
> url="/vol/pictures/test3.jpg"
> oldname="BILDCHEN"/>
>         <IMAGE  name="test4"
> url="/vol/pictures/test4.jpg" oldname="IMG"/>
>         <IMAGE  name="test5"
> url="/vol/pictures/test5.jpg"/>
>     </PHOTOS>
>     <REGIONSET image="/vol/pictures/test2.jpg"
> oldvalue="test2">
>         <RECT x="10" y="20" z="50" h="70">
>         Cup
>         </RECT>
>         <RECT x="20" y="30" z="60" h="20">
>         Sword
>         </RECT>
>     </REGIONSET>
> </COLLECTION>
> 
> 
> The second stylesheet:
> 
> The second should undo all, the first stylesheet
> did. So rename ever changed
> node and attribute with the original stored name and
> value and delete the
> new created attributes.
> 
> I think it is really hard to write these stylesheets
> and I don't know, if it
> is possible! So would be nice if anybody could help
> me or give some hints so
> I can solve this problem. I dont believe I could
> manage to solve this
> problem alone so help would be very welcome!!!!
> 
> 
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

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


Current Thread