Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

completely lost with XSL ...

Thread view: 
Dhurandhar - 02 Nov 2006 08:42 GMT
Hmmm. I thought I will understand it easily. But no. I don't.

I just want to make a copy of a source xml into a result xml and change
the values of attributes of certain elements. How do I write the
stylesheet for this seemingly straightforward transformation?

e.g
source xml:
<aaa>
<bbb name="foo">this is bbb</bbb>
<ccc enabled="true"/>
</aaa>

required result xml should be:

<aaa>
<bbb name="bar">this is bbb</bbb>
<ccc enabled="false"/>
</aaa>

I am feeling stupid :(

Please help.

DB
Dhurandhar - 02 Nov 2006 09:13 GMT
> Hmmm. I thought I will understand it easily. But no. I don't.
>
[quoted text clipped - 21 lines]
>
> DB

Oh, it's me again.
I must say that there is no point in creating <xsl:element>'s and
<xsl:attribute>'s unless
it can be generalized. So, it is not that I cannot do this transform at
all, but I just want to
apply the similar technique with elements that might have 10
attributes. I don't want to
do something like:
<xsl:element name="ccc">
<xsl:attribute name="enabled">
 <xsl:value-of select="false()"/>
</xsl:attribute>
<xsl:attribute name="running">
 <xsl:value-of select="false()"/>
</xsl:attribute>
<xsl:attribute name="checked">
 <xsl:value-of select="true()"/>
</xsl:attribute>
<xsl:attribute name="enabled">
 <xsl:value-of select="false()"/>
</xsl:attribute>
</xsl:element>

and so on.

Looking for a more elegant solution ...
Dhurandhar - 02 Nov 2006 10:54 GMT
> > Hmmm. I thought I will understand it easily. But no. I don't.
> >
[quoted text clipped - 48 lines]
>
> Looking for a more elegant solution ...

Basically, I am looking for a solution that lets me keep everything
else as the source
document, but allows
- adding a couple sub-elements,
- changing values of existing attributes

Thus, it's a more decorated XML than the source XML. How can I do it
better?
TechBookReport - 02 Nov 2006 18:29 GMT
>>> Hmmm. I thought I will understand it easily. But no. I don't.
>>>
[quoted text clipped - 56 lines]
> Thus, it's a more decorated XML than the source XML. How can I do it
> better?

Well I'm not sure how elegant this is, but for each element you want to
change you can do this:

<xsl:copy>                    //make a copy of the element
 <xsl:copy-of select="@*/>    //copy all of the attributes
 <xsl:attribute name="change">new</xsl:attribute> //change an attribute
 <xsl:attribute name="new">new</xsl:attribute>    //add an attribute
 <xsl:element>Add new element</xsl:element>       //add an element
 <xsl:apply-templates select="next"/>            //move on down the tree
</xsl:copy>

Pan

Signature

TechBookReport Java http://www.techbookreport.com/JavaIndex.html

Greg R. Broderick - 02 Nov 2006 23:28 GMT
>> > Hmmm. I thought I will understand it easily. But no. I don't.
>> >
>> > I just want to make a copy of a source xml into a result xml and
>> > change the values of attributes of certain elements. How do I
>> > write the stylesheet for this seemingly straightforward
>> > transformation?

[remainder snipped]

IMHO, you're much more likely to receive a timely, correct and complete
answer if you ask your question(s) in a news group that deals with the
topic of XML / XSL, such as comp.text.xml, than you are in a news group
that deals with the topic of programming in the Java language.

Cheers
GRB
Dhurandhar - 03 Nov 2006 08:01 GMT
Thanks for the answer and a pointer to the appropriate group.
Will be watchful next time.

Thanks,

> >> > Hmmm. I thought I will understand it easily. But no. I don't.
> >> >
[quoted text clipped - 12 lines]
> Cheers
> GRB
Simon Brooke - 03 Nov 2006 15:13 GMT
> Hmmm. I thought I will understand it easily. But no. I don't.

You really would do better to ask this on a group where it is on topic,
such as <URL:news:comp.text.xml>. It isn't on topic here.

> I just want to make a copy of a source xml into a result xml and change
> the values of attributes of certain elements. How do I write the
[quoted text clipped - 13 lines]
> <ccc enabled="false"/>
> </aaa>

<xsl:template match="bbb[@name='foo']">
       <bbb>
               <xsl:attribute name="name">bar</xsl:attribute>
               <xsl:apply-templates/>
       </bbb>
</xsl:template>

Note: this is off the top of my head, I haven't tested it. I leave the
template for 'ccc' as an exercise for you, since it should be fairly
obvious.

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

       ;; making jokes about dyslexia isn't big, it isn't clever and
       ;; it isn't furry.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.