Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 25th, 2010, 05:41 PM
Registered User
 
Join Date: Feb 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How can I acheive this logic?

I'm a relative newbie to XSL. I want to do some logic in XSL that's super easy in any other language, but I just can't seem to find a way to do it in XSL. Please help!

I want to make a template that retrieves a field value in a certain translation. I have a list of available translations. A caller can pass into a template which field they want the value of, along with a list of languages in preference order. The template should then go through the list of languages in order, and when the template finds that that translation exists, it returns that value and stops looking.

At first I thought easy - I'll just do a for each loop and then break after I find a language. But no, Google tells me that XSL processes things in parallel (well, at least non-procedurally), so there's no such thing as a break in XSL.

Then I thought no problem - I'll just create a variable called 'found' or something, set it to false at first, make the loop ensure that it's false, then set it to true when I match a language. But no, Google tells me that XSL is a declarative language, and a variable always has the same value within a given scope. (Hmm, not much of a 'variable' in my opinion, but I digress)

So...how can I do this in XSL? Surely this is possible somehow.
Reply With Quote
  #2 (permalink)  
Old February 25th, 2010, 07:21 PM
mhkay's Avatar
Wrox Author
Points: 18,252, Level: 58
Points: 18,252, Level: 58 Points: 18,252, Level: 58 Points: 18,252, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,929
Thanks: 0
Thanked 281 Times in 276 Posts
Default

You need to stop thinking that you have to tell the computer what to do and in what order to do it. In XSLT, you just have to describe your transformation.

Typically, if you want to find the first language that's present in some supplied list of languages, all you need to write is

$languages/language[. = $requiredLanguages][1]

No need to write a loop or break out of it: just tell it you want the first matching language.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old February 25th, 2010, 09:24 PM
Registered User
 
Join Date: Feb 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thankyou very much for your response mhkay.

Unfortunately I don't really understand the solution you're describing. Maybe it would be clearer if I were more specific.

I have an array of languages that looks like:
Code:
<lang>
  <value>fr-fr</value>
  <value>es-es</value>
  <value>en-gb</value>
</lang>
which I'm storing in a variable called $lang.

I then have the data object which looks like:
Code:
<data>
  <id>1</id>
  <priority>0</priority>
  <Translation>
    <en-gb>
      <id>1</id>
      <title>Hello</title>
      <lang>en-gb</lang>
    </en-gb>
    <es-es>
      <id>1</id>
      <title>Hola</title>
      <lang>es-es</lang>
    </es-es>
  </Translation>
</data>
How can I write a single XPath query to get the first available translation from the Translation node which matches languages in order from $lang, retrieve the title, and only get the first one?

Thanks again!
Reply With Quote
  #4 (permalink)  
Old February 26th, 2010, 03:16 AM
mhkay's Avatar
Wrox Author
Points: 18,252, Level: 58
Points: 18,252, Level: 58 Points: 18,252, Level: 58 Points: 18,252, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,929
Thanks: 0
Thanked 281 Times in 276 Posts
Default

>Maybe it would be clearer if I were more specific.

It certainly would. Your question was phrased in such vague terms I had no choice but to be vague in my answer.

I don't think that putting data in element names is a particularly good idea, but that's incidental.

The translations which match a value from $lang are /data/Translation/*[lang=$lang//value].

The first such translation is (/data/Translation/*[lang=$lang//value])[1].

The title of this translation is (/data/Translation/*[lang=$lang//value])[1]/title

You don't even need any XSLT code for this - it's just one XPath expression!
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
help to solve the logic pradeepn XSLT 4 May 22nd, 2007 06:07 AM
And/Or Logic??? ninel SQL Server 2000 2 February 9th, 2007 10:33 AM
RECURRING LOGIC its.shashank Classic ASP Databases 1 November 22nd, 2006 07:58 AM
Mixing Data access logic and business logic polrtex BOOK: Professional Jakarta Struts 0 December 15th, 2003 06:19 PM



All times are GMT -4. The time now is 02:39 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.