473,385 Members | 1,472 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.

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 1763


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:stylesheet
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="document('')/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="document('things.xml')/
things/text[position()=current()/@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="document('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
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...
19
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,...
8
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...
26
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...
2
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...
15
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()....
8
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...
1
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...
9
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...
4
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.