Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 10th, 2006, 01:11 AM
Registered User
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT for Variable Child Nodes

Ok, I'm tried really hard to figure out this. I'm developing a php aplication and decided to try to make a XSLT based datagrid.

I'm generating my XML from a Script, this means, The XSL must read n colluns

A xml like this
Code:
  <?xml version="1.0" encoding="utf-8" ?> 
- <grid>
- <iten>
  <id>1</id> 
  <nome>Jo?o Amaro Lagedo</nome> 
  <tel_residencial>2425-3352</tel_residencial> 
  <tel_comercial>5551-5545</tel_comercial> 
  <data_nascimento>2006-09-09</data_nascimento> 
  </iten>
+ <iten>
  <id>2</id> 
  <nome>Neyde De Almeida Ferreida</nome> 
  <tel_residencial>555-58585</tel_residencial> 
  <tel_comercial /> 
  <data_nascimento>2006-09-09</data_nascimento> 
  </iten>
- <iten>
  <id>3</id> 
  <nome>Nilma Almeida Ferreira</nome> 
  <tel_residencial>256-1248</tel_residencial> 
  <tel_comercial /> 
  <data_nascimento>2006-09-09</data_nascimento> 
  </iten>
  </grid>
and XSL

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:template match="/">
          <html>
          <body>
            <h2>My Itens</h2> 
            <table border="1">
              <tr bgcolor="#9acd32">
                <th align="left">id</th> 
                <th align="left">nome</th> 
                <th align="left">tel_residencial</th>             
                <th align="left">tel_comercial</th> 
                <th align="left">data_nascimento</th>                         
              </tr>
              <xsl:for-each select="grid/iten">
              <tr>
 /* Its here... I need to loop these nodes, instead of calling 
them by their names, so I can use a xslt file for any xml in my
 system */
                <td><xsl:value-of select="id" /></td>
                <td><xsl:value-of select="nome" /></td>
                <td><xsl:value-of select="tel_residencial" /></td>
                <td><xsl:value-of select="tel_comercial" /></td>
                <td><xsl:value-of select="data_nascimento" /></td>

              </tr>
              </xsl:for-each>
          </table>
          </body>
          </html>
        </xsl:template>        
</xsl:stylesheet>
Is there anyway a xsl can deal with dynamic xls ? I mean I need to add more nodes or remove, help ;) ?


EDIT: Nevermind... I found the solution in a very old post


Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:template match="/">
          <html>
          <body>
            <h2>My Itens</h2> 
            <table border="1">
              <tr bgcolor="#9acd32">
                <th align="left">id</th> 
                <th align="left">nome</th> 
                <th align="left">tel_residencial</th>             
                <th align="left">tel_comercial</th> 
                <th align="left">data_nascimento</th>                         
              </tr>
              <xsl:for-each select="grid/iten">
              <tr>
                  <xsl:for-each select="descendant::*">
                    <td>
                        <xsl:value-of select="."/>
                    </td>
                </xsl:for-each>
              </tr>
              </xsl:for-each>
          </table>
          </body>
          </html>
        </xsl:template>        
</xsl:stylesheet>
 
Old September 10th, 2006, 02:53 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well you'd need to loop through the first block of iten child elements using their names to create the headers. For this you will need to take advantage of templates with a specific mode. Possibly you want to transform to add capitals and spaces, e.g. data_nascimento => Data Nascimento. There are some example templates that do this on the web.

You then need to loop through all the iten elements to output the data, using a template with a different mode from that above.

Some of the problems here show why the structure you have as your source is not ideal. A better one would be to use something like:
Code:
<grid xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <iten>
    <row friendlyName="id" sourceName="id" dataType="xs:integer">1</row>
<row friendlyName="Data Nascimento" sourceName="data_nascimento" dataType="xs:dateTime"></row>

  </row>
</grid>
This gives you all the information you need for display and formatting of the columns.

--

Joe (Microsoft MVP - XML)
 
Old September 10th, 2006, 03:00 AM
Registered User
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow, that's really refreshing, that estructure probally solves many of my problems,
REally thanks ;)






Similar Threads
Thread Thread Starter Forum Replies Last Post
not able to print out the child nodes eruditionist XSLT 7 October 30th, 2008 10:33 AM
Appending child nodes to a RDF/OWL file using xslt sesath XSLT 2 May 10th, 2007 04:37 AM
Retrieveal of child nodes.... nathilson21 Classic ASP XML 0 May 7th, 2007 04:54 AM
Trying to group child nodes aalbetski XSLT 3 November 18th, 2006 01:29 PM
counting child nodes Tomi XSLT 1 September 6th, 2006 03:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.