tags:

views:

51

answers:

1

I have a problem. I get the data from xml then transform it with xslt.

Let us say I have a xml file:

<?xml version="1.0"?>
 <root>
   <row id="1" fname="Dan" lname="Wahlin">
    <address type="home">
       <street>1234 Anywhere St.</street>
       <city>AnyTown</city>
       <zip>85789</zip>
     </address>
     <address type="business">
       <street>1234 LottaWork Ave.</street>
       <city>AnyTown</city>
       <zip>85786</zip>
     </address>
  </row>
   <row id="2" fname="Elaine" lname="Wahlin">
    <address type="home">
      <street>1234 Anywhere St.</street>
      <city>AnyTown</city>
       <zip>85789</zip>
     </address>
    <address type="business">
      <street>1233 Books Way</street>
       <city>AnyTown</city>
       <zip>85784</zip>
    </address>
  </row>
 </root>

And this stylesheet:

<?xml version="1.0" ?>  
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   version="1.0">  
<xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="no"/> 
   <xsl:template match="/">
     <root> 
        <xsl:apply-templates/> 
      </root> 
    </xsl:template>
    <xsl:template match="row"> 
          <row> 
            <xsl:attribute name="id"> 
                    <xsl:value-of select="id"/> 
               </xsl:attribute> 
                <xsl:attribute name="fname"> 
                     <xsl:value-of select="name/fname"/> 
                </xsl:attribute> 
                <xsl:attribute name="lname"> 
                     <xsl:value-of select="name/lname"/> 
              </xsl:attribute> 
              <xsl:for-each select="address"> 
                   <xsl:copy-of select="."/> 
               </xsl:for-each>           </row> 
      </xsl:template> 
 </xsl:stylesheet

How can limit this to 3 records, then after 3 records it create a tr tag?

For example:

<table>
<tr>
  <td>Address1</td>
  <td>Address2</td>
  <td>Address3</td>
</tr>
<tr>
   <td>Address4</td>
   <td>Address5</td>
   <td>Address6</td>
</tr>
</table
A: 

try something like

<xsl:for-each select="PATH">
<xsl:variable name="pos" select="position() mod 3" />

</xsl:for-each>

then you can work with

<xsl:if test="$pos = 0">
</xsl:if>

and

<xsl:if test="$pos != 0">
</xsl:if>

if $pos = 0 means that you reached the 3rd row

Here are some good resources to learn more about XSLT and XPath http://w3schools.com/xsl/default.asp http://w3schools.com/xpath/default.asp

Christina Mayers
hi cristina thanks for this, ,by the way what is "Path[position]<3]"
I don't know how your XML looks like - so I don't know the path to the nodes you need. Eg "/root/Adresses" and [position() < 4] selects only the first 3 records.
Christina Mayers
yes i got it, but this only limits into 3 records to display,i want to display the remaining records let us say ihave a table then in every 3 records it create a tr tag, meaning to say i have 3 "td" tags per "tr" tag
sorry, I got your question wrong, will update the answer accordingly :)
Christina Mayers
thank's chistina, I appreciated your help