473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1373
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.