Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Parsing comma-separated values with XSL?

Reply
Thread Tools

Parsing comma-separated values with XSL?

 
 
RogerTBrick
Guest
Posts: n/a
 
      03-04-2005
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.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-04-2005


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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:mp="http://example.com/2005/03/map1">

<xslaram 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/
 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      03-04-2005

> 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
 
Reply With Quote
 
RogerTBrick
Guest
Posts: n/a
 
      03-07-2005
David Carlisle <> wrote in message news:<>...
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What libraries should I use for MIME parsing, XML parsing, and MySQL ? John Levine Ruby 0 02-02-2012 11:15 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57