XML to HTML table with XSL. Could someone help me please?

Discussion in 'XML' started by mslyman@gmail.com, Jun 8, 2006.

  1. Guest

    Hi,
    I could do with some help. I have this XML.

    <region>
    <region_code>567</region_code>
    <store>
    <store_code>345</store_code>
    <dept>
    <dept_code>32</dept_code>
    </dept>
    <dept>
    <dept_code>33</dept_code>
    </dept>
    </store>
    <store>
    <store_code>7</store_code>
    <dept>
    <dept_code>9</dept_code>
    </dept>
    <dept>
    <dept_code>8</dept_code>
    </dept>
    </store>
    <store>
    <!--- more depts -->
    </store>
    </region>
    <region>
    <region_code>776</region_code>
    <!--- more stores -->
    </region>

    I need this output to html as follows:
    <table>
    <tr>
    <td title="region">567</td>
    <td title="store">345</td>
    <td title="dept">34</td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td title="dept">33</td>
    </tr>
    <tr>
    <td></td>
    <td title="store">7</td>
    <td title="dept">8</td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td title="dept">9</td>
    </tr>
    <tr>
    <td title="region">776</td>
    <!-- etc -->
    </tr>
    <!-- etc -->
    </table>

    I have tried to achieve this with my limited amount of xsl knowledge of
    <xsl:for-each >, nested <xsl:for-each > and <xsl:if
    test="position()=1"> and i get close ( by using <xsl:for-each
    select="dept"> ) but it's not right. I guessing I have to
    apply-templates/recursion but not sure how.
    Please if anyone could help that would be great.

    Thanks,
    Mark
     
    , Jun 8, 2006
    #1
    1. Advertising

  2. Joe Kesselman, Jun 8, 2006
    #2
    1. Advertising

  3. On 2006-06-08, <> wrote:
    > Hi,
    > I could do with some help. I have this XML.
    >
    ><region>
    > <region_code>567</region_code>
    > <store>
    > <store_code>345</store_code>
    > <dept>
    > <dept_code>32</dept_code>
    > </dept>
    > <dept>
    > <dept_code>33</dept_code>
    > </dept>
    > </store>
    > <store>
    > <store_code>7</store_code>
    > <dept>
    > <dept_code>9</dept_code>
    > </dept>
    > <dept>
    > <dept_code>8</dept_code>
    > </dept>
    > </store>
    > <store>
    > <!--- more depts -->
    > </store>
    ></region>
    ><region>
    > <region_code>776</region_code>
    > <!--- more stores -->
    ></region>
    >
    > I need this output to html as follows:
    ><table>
    ><tr>
    ><td title="region">567</td>
    ><td title="store">345</td>
    ><td title="dept">34</td>
    ></tr>
    ><tr>
    ><td></td>
    ><td></td>
    ><td title="dept">33</td>
    ></tr>
    ><tr>
    ><td></td>
    ><td title="store">7</td>
    ><td title="dept">8</td>
    ></tr>
    ><tr>
    ><td></td>
    ><td></td>
    ><td title="dept">9</td>
    ></tr>
    ><tr>
    ><td title="region">776</td>
    > <!-- etc -->
    ></tr>
    ><!-- etc -->
    ></table>
    >
    > I have tried to achieve this with my limited amount of xsl knowledge of
    ><xsl:for-each >, nested <xsl:for-each > and <xsl:if
    > test="position()=1"> and i get close ( by using <xsl:for-each
    > select="dept"> ) but it's not right. I guessing I have to
    > apply-templates/recursion but not sure how.
    > Please if anyone could help that would be great.


    It would help if you posted what you tried that was "close" but "not
    right".

    Having nested for-each loops on region, store, and dept and generating
    a table row in the dept loop should work. To determine whether to
    output a <td title="region"> element or an empty <td> element you need
    to test whether the dept position is 1 and the store position is 1.
    In the store loop you can set a variable to the value of position() and
    test that variable in the dept loop.

    In you example xml with region=567 and store=7, dept=9 was before
    dept=8, but in the output the corresponding table row for dept=9 was
    after the table row for dept=8. Was this intentional, that is, is
    the order of table rows in the output not the same as the order in the
    xml document?
     
    A. Bolmarcich, Jun 8, 2006
    #3
  4. Hi,

    Many thanks for reply.
    Firstly order of nodes is not important for my example.

    Like you said, I did nested for-each and generated the table row in the
    dept loop however once inside "store" for-each I was repeating
    output the region_code within the region. This is something like I'm
    doing ** Note title attribute on <td> in my original post was to make
    the out more readable *** :

    <xsp:template match="region">
    <xsp:for-each select="store">
    <xsp:for-each select="dept">
    <tr>
    <xsp:choose>
    <xsp:when test="position()=1">
    <td> <xsp:value select="../../region_code"/></td>
    <td> <xsp:value select="../store_code"/></td>
    <td> <xsp:value select="dept_code"/></td>
    </xsp:when>
    <xsp:eek:therwise>
    <td> </td>
    <td> </td>
    <td> <xsp:value select="dept_code"/></td>

    </xsp:eek:therwise>

    </xsp:choose>
    </tr>
    </xsp:for-each>
    </xsp:fior-each>
    </xsp:template>
    However this produced following:
    <tr>
    <td>567</td>
    <td>345</td>
    <td>34</td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td>33</td>
    </tr>
    <tr>
    <td>567</td> <!--- loop repeats the region code and I dont what that
    -->
    <td>7</td>
    <td>8</td>
    </tr>
    <tr>
    <td></td>
    <td></td>
    <td>9</td>
    </tr>

    However having read your post, I think can prevent repeat of region
    code output within store, by creating a variable for position() of
    region for-each loop. That make sense I think. But it's 1am here and my
    bed is calling. Will try tomorrow.

    Thanks to you (and everyone) for your posts again,

    Regards

    mark
    A. Bolmarcich wrote:
    > On 2006-06-08, <> wrote:
    > > Hi,
    > > I could do with some help. I have this XML.
    > >
    > ><region>
    > > <region_code>567</region_code>
    > > <store>
    > > <store_code>345</store_code>
    > > <dept>
    > > <dept_code>32</dept_code>
    > > </dept>
    > > <dept>
    > > <dept_code>33</dept_code>
    > > </dept>
    > > </store>
    > > <store>
    > > <store_code>7</store_code>
    > > <dept>
    > > <dept_code>9</dept_code>
    > > </dept>
    > > <dept>
    > > <dept_code>8</dept_code>
    > > </dept>
    > > </store>
    > > <store>
    > > <!--- more depts -->
    > > </store>
    > ></region>
    > ><region>
    > > <region_code>776</region_code>
    > > <!--- more stores -->
    > ></region>
    > >
    > > I need this output to html as follows:
    > ><table>
    > ><tr>
    > ><td title="region">567</td>
    > ><td title="store">345</td>
    > ><td title="dept">34</td>
    > ></tr>
    > ><tr>
    > ><td></td>
    > ><td></td>
    > ><td title="dept">33</td>
    > ></tr>
    > ><tr>
    > ><td></td>
    > ><td title="store">7</td>
    > ><td title="dept">8</td>
    > ></tr>
    > ><tr>
    > ><td></td>
    > ><td></td>
    > ><td title="dept">9</td>
    > ></tr>
    > ><tr>
    > ><td title="region">776</td>
    > > <!-- etc -->
    > ></tr>
    > ><!-- etc -->
    > ></table>
    > >
    > > I have tried to achieve this with my limited amount of xsl knowledge of
    > ><xsl:for-each >, nested <xsl:for-each > and <xsl:if
    > > test="position()=1"> and i get close ( by using <xsl:for-each
    > > select="dept"> ) but it's not right. I guessing I have to
    > > apply-templates/recursion but not sure how.
    > > Please if anyone could help that would be great.

    >
    > It would help if you posted what you tried that was "close" but "not
    > right".
    >
    > Having nested for-each loops on region, store, and dept and generating
    > a table row in the dept loop should work. To determine whether to
    > output a <td title="region"> element or an empty <td> element you need
    > to test whether the dept position is 1 and the store position is 1.
    > In the store loop you can set a variable to the value of position() and
    > test that variable in the dept loop.
    >
    > In you example xml with region=567 and store=7, dept=9 was before
    > dept=8, but in the output the corresponding table row for dept=9 was
    > after the table row for dept=8. Was this intentional, that is, is
    > the order of table rows in the output not the same as the order in the
    > xml document?
     
    Stephen Slyman, Jun 9, 2006
    #4
    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. Curious

    If Someone Could Help, Please?

    Curious, Apr 17, 2004, in forum: HTML
    Replies:
    2
    Views:
    316
    Whitecrest
    Apr 17, 2004
  2. Jerry
    Replies:
    21
    Views:
    794
    Luigi Donatello Asero
    May 21, 2006
  3. shapper
    Replies:
    0
    Views:
    351
    shapper
    Dec 18, 2007
  4. shapper
    Replies:
    0
    Views:
    309
    shapper
    Mar 2, 2008
  5. shapper
    Replies:
    0
    Views:
    349
    shapper
    Mar 4, 2008
Loading...

Share This Page