tags:

views:

249

answers:

2

Good afternoon, gentlemen. Please help us solve the problem, I do not have enough brains to solve it by myself.

There is a table, you must define "simple" it or "complicated". Table "simple" if each row contain only two column, otherwise it is "complicated" table. How to do this means xslt.

+1  A: 

I am sorry that you took the time. I came up with a solution. Here is the answer, if someone need.

<xsl:template match="TABLE">
<xsl:variable name="tableClass">
    <xsl:choose>
        <xsl:when test="count(TBODY/TR[count(child::*) = 2]) = count(TBODY/TR)">
            simple
        </xsl:when>
        <xsl:otherwise>
            complicated
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
<table class="{$tableClass}">
    <xsl:apply-templates select="CAPTION"/>
    <xsl:apply-templates select="TBODY"/>
    and so on.......
</table>

Kalinin
`<xsl:when test="not(TBODY/TR[count(*) > 2])">`
Tomalak
I am sorry, but your proposal does not fit, the number of columns must be equal to 2, instead of <= 2. But all the details. Thank you.
Kalinin
A: 

Given two XML documents, A:

<base>
  <row>
    <col1 value='x'/>
    <col2/>
  </row>
  <row>
    <col1 value='y'/>
    <col2/>
  </row>
  <row>
    <col1 value='z'/>
    <col2/>
  </row>
</base>

and B:

<base>
  <row>
    <col1/>
    <col2/>
    <col3/>
  </row>

  <row>
    <col1/>
    <col2/>
  </row>
</base>

this xsl will tell if it is "simple" or "complex" based on the number of child elements under each toplevel row element:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="text" encoding = "iso-8859-1"/>

  <!-- is the xml simple? -->
  <!-- simple, in this case, means each row has 2 or fewer columns -->


  <xsl:variable name="maxColCount">
     <xsl:for-each select="//base/row">
       <xsl:sort data-type="number" order="descending"/>
       <xsl:if test="position()=1"><xsl:value-of select="count(./*)"/></xsl:if>
     </xsl:for-each>
   </xsl:variable>


  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="$maxColCount > 2">complex</xsl:when>
      <xsl:otherwise>simple</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

The result is: A is simple, and B is complex.

Cheeso
You forgot to add select="count(./*)" to you string <xsl:sort data-type="number" order="descending"/>. Without it, the sorting is not working.
Kalinin