472,954 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

How to simplify?

Hi

I've got the following XSLT that works but I'm trying to figure out how to
simplify it. There is only one Image, so I don't need the for-each-loop.
Anyone?

<xsl:for-each select="Data[@Name='Image']/img">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="class">right</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:for-each>

The code adds a class attribute named right to the given img-tag.
Jun 27 '08 #1
3 1349
Hvid Hat wrote:
I've got the following XSLT that works but I'm trying to figure out how to
simplify it. There is only one Image, so I don't need the for-each-loop.
Anyone?

<xsl:for-each select="Data[@Name='Image']/img">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="class">right</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:for-each>
xsl:copy needs a context node so I don't think you can get rid of the
xsl:for-each as that sets up the 'img' element as the context node.
If that stylesheet creates HTML or XHTML then you could drop the
xsl:apply-templates as 'img' elements in HTML or XHTML are empty.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
On 07-06-2008 14:27:36, Martin Honnen wrote:
xsl:copy needs a context node so I don't think you can get rid of the
xsl:for-each as that sets up the 'img' element as the context node.
If that stylesheet creates HTML or XHTML then you could drop the
xsl:apply-templates as 'img' elements in HTML or XHTML are empty.
Too bad. The for-each kinda tortures my eyes when knowing there's only one
element :) Thanks for the tip about removing apply-templates.

I've just sent another message to the group about something related. Maybe
you could answer it? Is it somehow possible to write a generic template that
adds a given attribute name with a given attribute value to a give node? E.g.
if I had a node, e.g. <node id="1"/>, then I'd like to call a template with
this node and attribute name/value like class/blue and the result would be
<node id="1" class="blue" />.

Otherwise I'm thinking about doing it with C# code in the XSLT but I'd like
to avoid C# if possible.
Jun 27 '08 #3
Hvid Hat wrote:
On 07-06-2008 14:27:36, Martin Honnen wrote:
>xsl:copy needs a context node so I don't think you can get rid of the
xsl:for-each as that sets up the 'img' element as the context node.
If that stylesheet creates HTML or XHTML then you could drop the
xsl:apply-templates as 'img' elements in HTML or XHTML are empty.

Too bad. The for-each kinda tortures my eyes when knowing there's only one
element :) Thanks for the tip about removing apply-templates.

I've just sent another message to the group about something related. Maybe
you could answer it? Is it somehow possible to write a generic template that
adds a given attribute name with a given attribute value to a give node? E.g.
if I had a node, e.g. <node id="1"/>, then I'd like to call a template with
this node and attribute name/value like class/blue and the result would be
<node id="1" class="blue" />.

Otherwise I'm thinking about doing it with C# code in the XSLT but I'd like
to avoid C# if possible.
<xsl:template match="*"can be used to catch all nodes not otherwise
specified in your XSL[T] document.

Otherwise write a named template that does what you need, and use
xsl:call-template at the relevant point in the processing of all your
other templates.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Alan | last post by:
I have many OR in the if statement, any method to simplify? if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......) // .... thank you
29
by: Flzw | last post by:
Alright, here is a simple function I coded, won't be hard understanding what it does, and YES, I know, you will probably tell me it's awful code, I would like to know how to write it better, maybe...
0
by: Stylus Studio | last post by:
Stylus Studio 6 XML Enterprise Edition Now Integrates with TigerLogic XDMS XQuery and Native XML Database Bedford, MA, -- Stylus Studio ( http://www.stylusstudio.com ), the industry-leading...
0
by: Stylus Studio | last post by:
DataDirect XQuery(TM) is the First Embeddable Component for XQuery That is Modeled after the XQuery API for Java(TM) (XQJ) BEDFORD, Mass.--Sept. 20, 2005--DataDirect Technologies...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
3
by: Bob Bedford | last post by:
hello I'm looking for some functions or objects allowing to select-insert-update-delete from any table in a mysql database without the need to create a new query every time. Example: ...
6
by: jsceballos | last post by:
Hello. I'm writing a proxy class, i.e: a class whose methods mostly delegate their functionality to other class object. Most of the methods (which are quite a lot) defined in the class would end...
6
by: Patrick | last post by:
Hi All, Kind of new to this. What I have below works, but I am wondering if there is a way to make it more efficient/simpler instead of having to write an if, tr, td, blah for each datatype. How...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
1
AmLegacy
by: AmLegacy | last post by:
I'm having a hard time figuring out how to simplify the fractions. Can anyone look at this code and see if you can see something I don't. //This is the fraction adding function void add_fractions...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.