completely lost with XSL ...

D

Dhurandhar

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
 
D

Dhurandhar

Dhurandhar said:
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

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 ...
 
D

Dhurandhar

Dhurandhar said:
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 ...

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?
 
T

TechBookReport

Dhurandhar said:
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?
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
 
G

Greg R. Broderick

[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
 
D

Dhurandhar

Thanks for the answer and a pointer to the appropriate group.
Will be watchful next time.

Thanks,
[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
 
S

Simon Brooke

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 said:
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>

<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top