0

I have an xslt style sheet that formats the data from my xml file into a table with a single column based on whether there is an image present or not. There is a fair amount of data so the column is too long though and i want to try splitting the current column into 2 equal columns. And idea how i can do this?

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="0" bordercolor="#606060" style="width:200px">
  <tr bgcolor="white" width="200px">
  </tr>
  <xsl:for-each select="NewDataSet/Manufacturer">
    <tr>
      <xsl:choose>
        <xsl:when test="Image = 'none'">
          <td bgcolor="#dfdfdf">
            <a href="Vehicles.aspx?Manufacturer={ManufacturerID}" style="text-decoration:none;" runat="server">
              <xsl:value-of select="Manufacturer"/>
            </a>
          </td>
        </xsl:when>
        <xsl:otherwise>
          <td>
            <a href="Vehicles.aspx?Manufacturer={ManufacturerID}" style="text-decoration:none;" runat="server">
              <im alt="{Manufacturer}" src="Images/Manufacturers/{Image}" style="border-width: 0px; width: 50px; "/>
            </a>
          </td>
        </xsl:otherwise>
      </xsl:choose>
    </tr>
  </xsl:for-each>
</table>
</xsl:template>

2
  • Does the order matter to you? i.e. would it matter if every second element was in the new column, as opposed to column 1 being the top half and column 2 being the bottom half? Also you seem to be putting <tr> elements inside other <tr> elements when Image='none Commented May 19, 2011 at 9:44
  • No, the order wouldnt matter.. i just need to split it into roughly 2 equal length columns.. if its the first half in one column and the 2nd half in the other its fine or every 2nd element in the 2nd column it would be 100% as well. Commented May 19, 2011 at 9:49

1 Answer 1

2

how about something like this

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <table border="0" bordercolor="#606060" style="width:200px">
    <tr bgcolor="white" width="200px">
    </tr>
    <tr>
    <xsl:for-each select="NewDataSet/Manufacturer">
        <xsl:choose>
            <xsl:when test="position() mod 2 = 0">
                <!-- so essentialy on even numbered elements process the current and next elements -->
                <td>
                    <xsl:call-template name="do-cell-content">
                        <xsl:with-param name="node" select="."/>
                    </xsl:call-template>
                </td>
                <td>
                    <xsl:call-template name="do-cell-content">
                        <xsl:with-param name="node" select="../Manufacturer[position()+1]"/>
                    </xsl:call-template>
                </td>
            <xsl:when>
        </xsl:choose>
    </xsl:for-each>
    </tr>
  </table>
</xsl:template>
</xsl:stylesheet>

I've extracted the part that writes the content of the cells and the <xsl:choose> part from your example to another xsl:template called do-cell-content

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.