XSLT: Whole table columns count
For each table in a document, I'd like to define it "simple" or "complicated". It should be "simple" if each row contains at most开发者_JAVA技巧 two column, otherwise it should be "complicated".
How can I do this with xslt?
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>
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">
<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.
精彩评论