Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > merging two sequences in xslt

Reply
Thread Tools

merging two sequences in xslt

 
 
JMan
Guest
Posts: n/a
 
      10-15-2008
Hi,
i have some xml that looks like this:

<parent>
<a>1,2,3,4,5,6,7,8</a>
<b>a,b,c,d,e,f,g,h</b>
</parent>

what i need i this:
<parent>
<ab>a 1 b 2 c 3 d 4 ...</ab>
</parent>

How can i do this? I thought of tokenizing the values of <a> and <b>
into sequences, but i dont know how to merge them together.
Does anybody has a solution to this problem?

cheers, Johannes
 
Reply With Quote
 
 
 
 
dahu
Guest
Posts: n/a
 
      10-15-2008
May be using fn:string-join ?

http://www.xqueryfunctions.com/xq/fn_string-join.html

JMan wrote:
> Hi,
> i have some xml that looks like this:
>
> <parent>
> <a>1,2,3,4,5,6,7,8</a>
> <b>a,b,c,d,e,f,g,h</b>
> </parent>
>
> what i need i this:
> <parent>
> <ab>a 1 b 2 c 3 d 4 ...</ab>
> </parent>
>
> How can i do this? I thought of tokenizing the values of <a> and <b>
> into sequences, but i dont know how to merge them together.
> Does anybody has a solution to this problem?
>
> cheers, Johannes

 
Reply With Quote
 
 
 
 
JMan
Guest
Posts: n/a
 
      10-15-2008
Well, to use fn:string join i would still need to first merge the two
sequences together wouldn't i?

cheers, Johannes

On 15 Okt., 16:42, dahu <(E-Mail Removed)> wrote:
> May be using fn:string-join ?
>
> http://www.xqueryfunctions.com/xq/fn_string-join.html
>
> JMan wrote:
> > Hi,
> > i have some xml that looks like this:

>
> > <parent>
> > <a>1,2,3,4,5,6,7,8</a>
> > <b>a,b,c,d,e,f,g,h</b>
> > </parent>

>
> > what i need i this:
> > <parent>
> > <ab>a 1 b 2 c 3 d 4 ...</ab>
> > </parent>

>
> > How can i do this? I thought of tokenizing the values of <a> and <b>
> > into sequences, but i dont know how to merge them together.
> > Does anybody has a solution to this problem?

>
> > cheers, Johannes


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      10-15-2008
JMan wrote:
> Hi,
> i have some xml that looks like this:
>
> <parent>
> <a>1,2,3,4,5,6,7,8</a>
> <b>a,b,c,d,e,f,g,h</b>
> </parent>
>
> what i need i this:
> <parent>
> <ab>a 1 b 2 c 3 d 4 ...</ab>
> </parent>
>
> How can i do this? I thought of tokenizing the values of <a> and <b>
> into sequences, but i dont know how to merge them together.



<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnss="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">

<xsl:template match="parent">
<xsl:copy>
<xsl:variable name="l1" as="xs:string*" select="tokenize(a, ',')"/>
<xsl:variable name="l2" as="xs:string*" select="tokenize(b, ',')"/>
<ab>
<xsl:value-of select="for $p in 1 to count($l1) return
($l2[$p], $l1[$p])" separator=" "/>
</ab>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>



--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
JMan
Guest
Posts: n/a
 
      10-15-2008
Hi Martin,

thanks for your reply. Its actually not yet working for me. l1 and l2
are of count==1 so what i get is pretty much what i put in. There
seems to be a problem with assiging the sequences to the veriables.
Can you fix it?

thanks, Johannes

> JMan wrote:
> > Hi,
> > i have some xml that looks like this:

>
> > <parent>
> > <a>1,2,3,4,5,6,7,8</a>
> > <b>a,b,c,d,e,f,g,h</b>
> > </parent>

>
> > what i need i this:
> > <parent>
> > <ab>a 1 b 2 c 3 d 4 ...</ab>
> > </parent>

>
> > How can i do this? I thought of tokenizing the values of <a> and <b>
> > into sequences, but i dont know how to merge them together.

>
> <xsl:stylesheet
> * *xmlnssl="http://www.w3.org/1999/XSL/Transform"
> * *xmlnss="http://www.w3.org/2001/XMLSchema"
> * *exclude-result-prefixes="xs"
> * *version="2.0">
>
> * *<xsl:template match="parent">
> * * *<xsl:copy>
> * * * *<xsl:variable name="l1" as="xs:string*" select="tokenize(a, ',')"/>
> * * * *<xsl:variable name="l2" as="xs:string*" select="tokenize(b, ',')"/>
> * * * *<ab>
> * * * * *<xsl:value-of select="for $p in 1 to count($l1) return
> ($l2[$p], $l1[$p])" separator=" "/>
> * * * *</ab>
> * * *</xsl:copy>
> * *</xsl:template>
>
> </xsl:stylesheet>
>
> --
>
> * * * * Martin Honnen
> * * * *http://JavaScript.FAQTs.com/


 
Reply With Quote
 
JMan
Guest
Posts: n/a
 
      10-15-2008
Martin!
thanks alot!

cheers, Johannes

On 15 Okt., 17:02, Martin Honnen <(E-Mail Removed)> wrote:
> JMan wrote:
> > Hi,
> > i have some xml that looks like this:

>
> > <parent>
> > <a>1,2,3,4,5,6,7,8</a>
> > <b>a,b,c,d,e,f,g,h</b>
> > </parent>

>
> > what i need i this:
> > <parent>
> > <ab>a 1 b 2 c 3 d 4 ...</ab>
> > </parent>

>
> > How can i do this? I thought of tokenizing the values of <a> and <b>
> > into sequences, but i dont know how to merge them together.

>
> <xsl:stylesheet
> * *xmlnssl="http://www.w3.org/1999/XSL/Transform"
> * *xmlnss="http://www.w3.org/2001/XMLSchema"
> * *exclude-result-prefixes="xs"
> * *version="2.0">
>
> * *<xsl:template match="parent">
> * * *<xsl:copy>
> * * * *<xsl:variable name="l1" as="xs:string*" select="tokenize(a, ',')"/>
> * * * *<xsl:variable name="l2" as="xs:string*" select="tokenize(b, ',')"/>
> * * * *<ab>
> * * * * *<xsl:value-of select="for $p in 1 to count($l1) return
> ($l2[$p], $l1[$p])" separator=" "/>
> * * * *</ab>
> * * *</xsl:copy>
> * *</xsl:template>
>
> </xsl:stylesheet>
>
> --
>
> * * * * Martin Honnen
> * * * *http://JavaScript.FAQTs.com/


 
Reply With Quote
 
JMan
Guest
Posts: n/a
 
      10-15-2008
can i change the datatypes of the lists, so that i can do something
like
<xsl:value-of select="for $p in 1 to count($l1) return ($l2[$p] div
10 , $l1[$p] div 10)" separator=" "/> ?

cheers, Johannes

On 15 Okt., 18:09, JMan <(E-Mail Removed)> wrote:
> Martin!
> thanks alot!
>
> cheers, Johannes
>
> On 15 Okt., 17:02, Martin Honnen <(E-Mail Removed)> wrote:
>
> > JMan wrote:
> > > Hi,
> > > i have some xml that looks like this:

>
> > > <parent>
> > > <a>1,2,3,4,5,6,7,8</a>
> > > <b>a,b,c,d,e,f,g,h</b>
> > > </parent>

>
> > > what i need i this:
> > > <parent>
> > > <ab>a 1 b 2 c 3 d 4 ...</ab>
> > > </parent>

>
> > > How can i do this? I thought of tokenizing the values of <a> and <b>
> > > into sequences, but i dont know how to merge them together.

>
> > <xsl:stylesheet
> > * *xmlnssl="http://www.w3.org/1999/XSL/Transform"
> > * *xmlnss="http://www.w3.org/2001/XMLSchema"
> > * *exclude-result-prefixes="xs"
> > * *version="2.0">

>
> > * *<xsl:template match="parent">
> > * * *<xsl:copy>
> > * * * *<xsl:variable name="l1" as="xs:string*" select="tokenize(a, ',')"/>
> > * * * *<xsl:variable name="l2" as="xs:string*" select="tokenize(b, ',')"/>
> > * * * *<ab>
> > * * * * *<xsl:value-of select="for $p in 1 to count($l1) return
> > ($l2[$p], $l1[$p])" separator=" "/>
> > * * * *</ab>
> > * * *</xsl:copy>
> > * *</xsl:template>

>
> > </xsl:stylesheet>

>
> > --

>
> > * * * * Martin Honnen
> > * * * *http://JavaScript.FAQTs.com/


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      10-15-2008
JMan wrote:
> can i change the datatypes of the lists, so that i can do something
> like
> <xsl:value-of select="for $p in 1 to count($l1) return ($l2[$p] div
> 10 , $l1[$p] div 10)" separator=" "/> ?


I don't understand how you expect the values a,b,c and so on to be
treated as numbers.

However let's assume you have different input data

<parent>
<a>1,2,3,4,5,6,7,8</a>
<b>10,20,30,40,50,60,70,80</b>
</parent>

then you can of course convert the string sequence the tokenize function
gives you to a number sequence:

<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnss="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">

<xsl:template match="parent">
<xsl:copy>
<xsl:variable name="l1" as="xs:double*" select="for $item in
tokenize(a, ',') return xs:double($item)"/>
<xsl:variable name="l2" as="xs:double*" select="for $item in
tokenize(b, ',') return xs:double($item)"/>
<ab>
<xsl:value-of select="for $p in 1 to count($l1) return ($l2[$p]
div 10, $l1[$p] div 10)" separator=" "/>
</ab>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
--

Martin Honnen
http://JavaScript.FAQTs.com/
 
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
Merging tables with XSLT Andrew McFarland XML 1 05-29-2009 03:27 PM
XSLT sorting / merging / summing david.paik@huscointl.com XML 0 09-20-2006 02:12 PM
xslt merging question rottyguy70@yahoo.com XML 4 11-01-2005 05:14 PM
how to compare two number sequences FC C++ 3 03-04-2005 05:10 PM
Merging two XML tree: xslt or dom ? Yannick Patois XML 1 07-23-2004 07:17 PM



Advertisments