How can I detect muliple rows in dataset using XSL ?

Hi

I have the follwing dataset(Tbl1 and Tbl2 with two rows).I have xsl file(given below) to restructure the xml file. Tbl2 may have more than one row and the dataset may change time to time(Tbl2 will be 1 or more rows)

If Dataset with 1 row of Tbl2 I can handle xsl file to manage it.

But If Dataset with more than 1 row in Tbl2 (please look at the xml file below)
How can I write the code(xsl)to detect the rows and place the data in between
the elements in the output(look at output xml file)

Scenario

Dataset

<Dataset>
<Tbl1>
<A>aaa</A>
<B>bbb</B>
<C>ccc</C>
<D>ddd</D>
<E>eee</E>
</Tbl1>
<Tbl2>
<1>one</1>
<2>two</2>
<3>three</3>
<4>four</4>
</Tbl2>
<Tbl2>
<1>one</1>
<2>two</2>
<3>three</3>
<4>four</4>
</Tbl2>
</Dataset>

Xsl file

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="DataSet">

<Body>
<Retail>
<book><xsl:value-of select="Tbl1/A"/></book>
<Pen><xsl:value-of select="Tbl1/B"/></Pen>
<Retail>
<House>
<Room>
<cup><xsl:value-of select="Tbl1/C"/></cup>
<Tea><xsl:value-of select="Tbl1/D"/></Tea>
<Nut><xsl:value-of select="Tbl1/E"/></Nut>
</Room>
</House>
<Body>

Output xml

<Body>
<Retail>
<book>aaa</book>
<Pen>bbb</Pen>
<Retail>
<House>
<Room>
<cup>ccc</cup>
<Tea>ddd</Tea>
<Nut>eee</Nut>
</Room>
I need rows of Tbl2 here (the code should scan the dataset(xml) for available rows and place as below.
<TAB2>
<sam>one</sam>
<pet>two</pet>
<dan>three</dan>
<lin>four
</lin>
</TAB2>
<TAB2>
<sam>one</sam>
<pet>two</pet>
<dan>three</dan>
<lin>four
</lin>
</TAB2>
If the dataset with 3 rows then another set of <TAB2> (Tbl2 in dataset) should be below

<House>
</Body

Advance thanks




Answer this question

How can I detect muliple rows in dataset using XSL ?

  • a.s.viswa

    Hi Dim

    Its ok. I thought It is only an example to make things easy.Sorry! Sorry! Please consider the changes below.

    Just I need only the code how to achive the result as I explaind in my posting.

    </Tbl2>
    <Ones>one</Ones>
    <Twos>two</Twos>
    <Threes>three</Threes>
    <Fours>four</Fours>
    </Tbl2>

    Thanks



  • Jassim Rahma

    Vaish wrote:

    Hi Dim

    The below one is xml generated from dataset

    Dataset

    < xml version="1.0" standalone="yes" >
    <DataSet xmlns=http://tempuri.org/DataSet1.xsd>
    <Tbl1>
    <A>aaa</A>
    <B>bbb</B>
    <C>ccc</C>
    <D>ddd</D>
    <E>eee</E>
    </Tbl1>
    <Tbl2> -------------------------------<TAB2>(Tbl2 become TAB2 in the output xml below)
    <1>one</1> This indicates 2 rows generated from dataset (table have 2 rows)
    <2>two</2> So I am looking for XSL code to transform this dataset and ouput as
    <3>three</3> the xml shown below.As you know the dataset will change depends on the
    <4>four</4> data in the table.
    </Tbl2> So If the dataset with 1 or more rows (<Tbl2>...</Tbl2> and <Tbl2>...</Tbl2>)
    <Tbl2> How can I write xsl to look for the one displayed here and put it as in
    <1>one</1> the xml file below.
    <2>two</2> In short - How to write xsl to transform if more than one elments
    <3>three</3> (same named) exist in the source xml
    <4>four</4>
    </Tbl2>------------------------------<TAB2>(Tbl2 become TAB2 in the output xml below)
    </DataSet>


    Output xml file

    <Body>
    <Retail>
    <book>aaa</book>
    <Pen>bbb</Pen>
    <Retail>
    <House>
    <Room>
    <cup>ccc</cup>
    <Tea>ddd</Tea>
    <Nut>eee</Nut>
    </Room>
    I need rows of Tbl2 here (the code should scan the dataset(xml) for available rows and place as below.
    <TAB2>
    <sam>one</sam>
    <pet>two</pet>
    <dan>three</dan>
    <lin>four
    </lin>
    </TAB2>
    <TAB2>
    <sam>one</sam>
    <pet>two</pet>
    <dan>three</dan>
    <lin>four
    </lin>
    </TAB2>
    If the dataset with 3 rows then another set of <TAB2> (Tbl2 in dataset) should be below

    <House>
    </Body


    Thanks

    Sorry, but that is not xml!

    <Tbl2>
    <1>one</1>
    <2>two</2>
    <3>three</3>
    <4>four</4>
    </Tbl2>

    The name of an xml element cannot start with a digit.

    Cheers,
    Dimitre Novatchev


  • Lavee

    Hi Dim

    Can you please help me with my new posting below

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1099403&SiteID=1&mode=1

    Thanks you



  • Little_Dice

    <Tbl2>
    <1>one</1>
    <2>two</2>
    <3>three</3>
    <4>four</4>
    </Tbl2>
    <Tbl2>
    <1>one</1>
    <2>two</2>
    <3>three</3>
    <4>four</4>
    </Tbl2>

    This is not well-formed xml. Did you mean something else

    Cheers,
    Dimitre Novatchev


  • llzamboni

    Below is one possible solution. There isn't anything difficult or special in your problem. This most probably means that you need to acquire some basic, fundamental knowledge of XSLT. Reading a good book on XSLT will help a lot.

    This transformation:

    <xsl:stylesheet version="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:df="http://tempuri.org/DataSet1.xsd"

    >

    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:strip-space elements="*"/>

    <xsl:variable name="vrtfNames">

    <n>sam</n>

    <n>pet</n>

    <n>dan</n>

    <n>lin</n>

    </xsl:variable>

    <xsl:variable name="vNames"

    select="document('')/*/xsl:variable[@name='vrtfNames']"/>

    <xsl:template match="/">

    <Body>

    <xsl:apply-templates/>

    </Body>

    </xsl:template>

    <xsl:template match="df:Tbl1" >

    <Retail>

    <xsl:apply-templates/>

    </Retail>

    </xsl:template>

    <xsl:template match="df:Tbl1/*" priority="-1"/>

    <xsl:template match="df:Tbl1/df:A">

    <book><xsl:value-of select="."/></book>

    </xsl:template>

    <xsl:template match="df:Tbl1/df:B">

    <Pen><xsl:value-of select="."/></Pen>

    </xsl:template>

    <xsl:template match="df:Tbl2">

    <TAB2>

    <xsl:apply-templates/>

    </TAB2>

    </xsl:template>

    <xsl:template match="df:Tbl2/*">

    <xsl:variable name="vPos" select="position()"/>

    <xsl:element name="{$vNames/*[$vPos]}">

    <xsl:value-of select="."/>

    </xsl:element>

    </xsl:template>

    </xsl:stylesheet>

    when applied on the provided xml document:

    <DataSet xmlns="http://tempuri.org/DataSet1.xsd">

    <Tbl1>

    <A>aaa</A>

    <B>bbb</B>

    <C>ccc</C>

    <D>ddd</D>

    <E>eee</E>

    </Tbl1>

    <Tbl2>

    <a1>one</a1>

    <a2>two</a2>

    <a3>three</a3>

    <a4>four</a4>

    </Tbl2>

    <Tbl2>

    <a1>one</a1>

    <a2>two</a2>

    <a3>three</a3>

    <a4>four</a4>

    </Tbl2>

    </DataSet>

    produces the wanted result:

    <Body xmlns:df="http://tempuri.org/DataSet1.xsd">

    <Retail>

    <book>aaa</book>

    <Pen>bbb</Pen>

    </Retail>

    <TAB2>

    <sam>one</sam>

    <pet>two</pet>

    <dan>three</dan>

    <lin>four</lin>

    </TAB2>

    <TAB2>

    <sam>one</sam>

    <pet>two</pet>

    <dan>three</dan>

    <lin>four</lin>

    </TAB2>

    </Body>

    Cheers,
    Dimitre Novatchev


  • Ryn

    Hi Dimitre

    I realy appreciate and thanks a lot for your help. I will try your solution.

    vaish



  • furjaw

    Hi Dim

    The below one is  xml generated from dataset

    Dataset

    < xml version="1.0" standalone="yes" > 
    <DataSet xmlns=http://tempuri.org/DataSet1.xsd>
     <Tbl1>
      <A>aaa</A>
      <B>bbb</B>
      <C>ccc</C>
      <D>ddd</D>
      <E>eee</E>
     </Tbl1>
     <Tbl2> -------------------------------<TAB2>(Tbl2 become TAB2 in the output xml below)
       <1>one</1>                               This indicates 2 rows generated from dataset (table have 2 rows)
       <2>two</2>                                So I am looking for XSL code to  transform this dataset and ouput as 
       <3>three</3>                             the xml shown below.As you know the dataset will change depends on the 
       <4>four</4>                               data in the table.
     </Tbl2>                                         So If the dataset with 1 or more rows (<Tbl2>...</Tbl2> and <Tbl2>...</Tbl2>)
     <Tbl2>                                          How can I write xsl to look for the one displayed here and put it as in
       <1>one</1>                               the xml file below.
       <2>two</2>                               In short - How to write xsl to transform if more than one elments
       <3>three</3>                                            (same named) exist in the source xml
       <4>four</4>
     </Tbl2>------------------------------<TAB2>(Tbl2 become TAB2 in the output xml below)
    </DataSet>


    Output xml file

    <Body>
      <Retail>
         <book>aaa</book>
         <Pen>bbb</Pen>
      <Retail>
     <House>
        <Room>
           <cup>ccc</cup>
           <Tea>ddd</Tea>
           <Nut>eee</Nut>
         </Room>
        I need rows of Tbl2 here (the code should scan the dataset(xml) for available rows and place as below.
           <TAB2>
             <sam>one</sam>
             <pet>two</pet>
             <dan>three</dan>
             <lin>four
    </lin>
          </TAB2>
          <TAB2>
             <sam>one</sam>
             <pet>two</pet>
             <dan>three</dan>
             <lin>four
    </lin>
          </TAB2>
    If the dataset with 3 rows then another set of <TAB2> (Tbl2 in dataset) should be below

     
        <House>
    </Body


     

    Thanks



  • How can I detect muliple rows in dataset using XSL ?