473,802 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing comma-separated values with XSL?

Cheers for the help last time guys. Again this is probably dead
simple but try as I might, I just can't work it out.

THe XML file I am being give contains some code value that need to be
exchanged for text in the final output (to HTML as it happens).
<root>
<thingy code="2"/>
<root>

My first solution (the one I thought would be easiest) was you use two
comma spearated lists and simply grab the correct text value based on
the index.
<xsl:variable name="codes">1, 2,3,4</xsl:variable>
<xsl:variable name="text">aaa ,bbb,ccc,ddd</xsl:variable>

But I am stumped. Is this even possible with XSL? Or is there a
better way of doing the substitution that I'm missing?

Thanks again,

J.
Jul 20 '05 #1
3 1779


RogerTBrick wrote:

THe XML file I am being give contains some code value that need to be
exchanged for text in the final output (to HTML as it happens).
<root>
<thingy code="2"/>
<root>

My first solution (the one I thought would be easiest) was you use two
comma spearated lists and simply grab the correct text value based on
the index.
<xsl:variable name="codes">1, 2,3,4</xsl:variable>
<xsl:variable name="text">aaa ,bbb,ccc,ddd</xsl:variable>

But I am stumped. Is this even possible with XSL? Or is there a
better way of doing the substitution that I'm missing?


You can build a map in the XSLT stylesheet using elements in a separate
namespace and access it as follows

<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:mp="http://example.com/2005/03/map1">

<xsl:param name="searchKey " select="1" />

<map xmlns="http://example.com/2005/03/map1">
<item>
<key>1</key>
<value>aaa</value>
</item>
<item>
<key>2</key>
<value>bbb</value>
</item>
</map>

<xsl:template match="/">
<result>
<xsl:value-of
select="documen t('')/xsl:stylesheet/mp:map/mp:item[mp:key =
$searchKey]/mp:value" />
</result>
</xsl:template>

</xsl:stylesheet>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
But I am stumped. Is this even possible with XSL?


yes but string handling isn't xslt1's strong point, It's rather better
at handling xml structure.

I'd stick

<things>
<text>aaa</text>
<text>bbb</text>
<text>ccc</text>
</things>

in things.xml then do

<xsl:template match="thingy">
<xsl:copy-of
select="documen t('things.xml')/
things/text[position()=curr ent()/@code]/node()"/>
</xsl:template>

or if your codes don't allways go 1 2 3

I'd stick
<things>
<text code="x">aaa</text>
<text code="y">bbb</text>
<text code="z">ccc</text>
</things>

in things.xml then do

<xsl:template match="thingy">
<xsl:copy-of
select="documen t('things.xml')/
things/text[@code=current()/@code]/node()"/>
</xsl:template>

so as to extract on the code attribute not on position.

If your list is long you could use a key to speed up searching for the
right replacement.

David
Jul 20 '05 #3
David Carlisle <da****@nag.co. uk> wrote in message news:<yg******* ******@penguin. nag.co.uk>...
yes but string handling isn't xslt1's strong point, It's rather better
at handling xml structure.

I'd stick

<things>
<text>aaa</text>
<text>bbb</text>
<text>ccc</text>
</things>

Ooo, you've no idea how much I wish I could do that. Thanks for the
help guys, I knew it was a filthy hack when I started, but needs must
and all that.

J.
Jul 20 '05 #4

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

Similar topics

2
17947
by: GIMME | last post by:
I can't figure an expression needed to parse a string. This problem arrises from parsing Excel csv files ... The expression must parse a string based upon comma delimiters, but if a comma appears in double quotes it should not be used for parsing. For example in the simple case we'd have : $a='hello,brave,world';
19
4012
by: ARK | last post by:
I am writing a search program in ASP(VBScript). The user can enter keywords and press submit. The user can separate the keywords by spaces and/or commas and key words may contain plain words, single quoted strings (phrases), double quoted strings (phrases). For example: Keywords: Jack, Jill, Jim, "Timothy Brown", Mary OR
8
1725
by: Darius Fatakia | last post by:
Hello, I have a file that I have opened for reading and this file contains lines with several different types of constraint information. For example, here are a few lines: length(0) = 10 Duration of task 0 is 10. needs(16,1) Operation 16 uses resource 1. before(49,9) Operation 49 must be before operation 9.
26
6882
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What is in my Watch:
2
2771
by: Trip | last post by:
Hello all, Is there anyway to store an array to MS Access 2002 without parsing the entire array row-by-row. For example, Oracle allows you to store BLOBs (binary large objects). I would like to be able to say in VB/VBA With rstName .AddNew !fieldName = ArrayName .Update
15
9561
by: VMI | last post by:
I'm parsing a comma-delimited record but I want it to do something if some of the string is between "". How can I do this? With the Excel import it does it correct. I'm using String.Split(). Basically, this is what I want to do: Use string.Split() on the whole string UNLESS the string is in between double-quotes. The part of the string in-between the "" will be ignored by String.Split Thanks.
8
4066
by: pradeepss | last post by:
Guys, I have a text file which is comma delimited and information. Each information is ended by end of line and started again with comma delimited i.e. 1,2,3,a,4 2,s,4,5,6,7,8,h 2,3,5,7,f,h,j,d,d What i need to do is pick a line and then put this in a array. then reference it like this:
1
1365
by: kellysgirl | last post by:
Im not good at parsing strings....and Ive been driving myslef nuts This is what I need to do....use an if/else statement to validate thata delimeter has been selected. These delimeters being comma, space and cr-lf. Parse text box contents----- Parse string to break out the words involving a loop and 2 pointer variable(old INdex and New INdex)Both start at 0..old index to always point to the current starting postion for the scan and...
9
1768
by: Jasper | last post by:
Hi, I have multiple data files which need parsing in realtime so high performance is *crucial*. I dont have a format definition, but from what I can see there is a hierarchy of data. Each data field is named thus <"name":(the <are mine). The data can be quoted text or unquoted text or a composite hierarcy field. Each name/data pair is terminated by a comma unless it is the last in the
4
1653
mickey0
by: mickey0 | last post by:
hello, I have to parsing a string like this: char * = "10 20 30 40"; and put its number into a vector<double> I thought to use strchr() and atof() and it's seems ok but to say the truth the line could be at times a little different: char * = "10 20 30 40 "; char * = "10,20,30,40"; char * = "10, 20,30, 40 "; char * = "10 20,30 40 "; all these above are valid line; instead this isn't': char * = "10 ...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.