merging two sequences in xslt

J

JMan

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
 
J

JMan

Well, to use fn:string join i would still need to first merge the two
sequences together wouldn't i?

cheers, Johannes
 
M

Martin Honnen

JMan said:
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
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="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>
 
J

JMan

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 said:
Hi,
i have some xml that looks like this:

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
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="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>
 
J

JMan

Martin!
thanks alot!

cheers, Johannes

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

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
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="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>
 
J

JMan

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

Martin!
thanks alot!

cheers, Johannes

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="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/
 
M

Martin Honnen

JMan said:
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
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top