merging two sequences in xslt

Discussion in 'XML' started by JMan, Oct 15, 2008.

  1. JMan

    JMan Guest

    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
    JMan, Oct 15, 2008
    #1
    1. Advertising

  2. JMan

    dahu Guest

    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
    dahu, Oct 15, 2008
    #2
    1. Advertising

  3. JMan

    JMan Guest

    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 <> 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
    JMan, Oct 15, 2008
    #3
  4. 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
    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/
    Martin Honnen, Oct 15, 2008
    #4
  5. JMan

    JMan Guest

    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
    >    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/
    JMan, Oct 15, 2008
    #5
  6. JMan

    JMan Guest

    Martin!
    thanks alot!

    cheers, Johannes

    On 15 Okt., 17:02, Martin Honnen <> 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
    >    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/
    JMan, Oct 15, 2008
    #6
  7. JMan

    JMan Guest

    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 <> wrote:
    > Martin!
    > thanks alot!
    >
    > cheers, Johannes
    >
    > On 15 Okt., 17:02, Martin Honnen <> 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
    > >    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/
    JMan, Oct 15, 2008
    #7
  8. 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
    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>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Oct 15, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yannick Patois

    Merging two XML tree: xslt or dom ?

    Yannick Patois, Jul 20, 2004, in forum: XML
    Replies:
    1
    Views:
    879
    Uche Ogbuji
    Jul 23, 2004
  2. Replies:
    4
    Views:
    486
    Martin Honnen
    Nov 1, 2005
  3. FC
    Replies:
    3
    Views:
    395
    Ioannis Vranos
    Mar 4, 2005
  4. Replies:
    0
    Views:
    504
  5. Andrew McFarland

    Merging tables with XSLT

    Andrew McFarland, May 29, 2009, in forum: XML
    Replies:
    1
    Views:
    2,394
    Andrew McFarland
    May 29, 2009
Loading...

Share This Page