思归在么,再帮我看看呀
关键词:col book name,content,book book name,name content,name,book,else var attr,attr,style
newmung:
上次的问题呀在控制列数的时候,当前元素占多少列放在子节点中了 <col>4</col> <book> <name>标题a</name> <content>内容a</content> </book> <book> <name>标题b</name> <content>内容b</content> </book> <book> <name>标题c</name> <content>内容c</content> </book> <book> <style colspan="2"></style> <name>标题d</name> <content>内容d</content> </book> <book> <style colspan="3"></style> <name>标题e</name> <content>内容e</content> </book> <book> <name>标题f</name> <content>内容f</content> </book> <book> <name>标题g</name> <content>内容g</content> </book> 我将对应脚本改成 var sttrnod = nodeList[j].selectSingleNode("style"); if (typeof(sttrnod) != "object") //var attr = sttrnod.attributes.getNamedItem("colspan").text; var attr = sttrnod.getAttribute("colspan"); else var attr=1; 结果它就不能再换行了 感觉是attr没有得到值,是这样的吗? 我假如直接用nodeList[j].selectSingleNode("style").getAttribute("colspan"); 在没有设置<style>节点的地方要报错
saucer:
try <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:whatever" exclude-result-prefixes="msxsl user"> <msxsl:script language="jscript" implements-prefix="user"> <![CDATA[ var g_nRowCount=0; var g_nColsCount=0; var oRows=null; function init(nodeList, nColsPerRow) { g_nRowCount =nodeList.length; g_nColsCount =nColsPerRow; oRows=new Array(g_nRowCount); for (var i=0; i < g_nRowCount; i++) { oRows[i] = new Array(3); } var nRow=0, j=0; var nCount=0,nStart,nEnd; while (j<g_nRowCount) { nStart = j; nEnd = nStart; nCount = 0; var nOldCount; do { nOldCount = nCount; var nInc = 1; var style = nodeList[j].selectSingleNode("style"); if (style) { var attr = style.getAttribute("colspan"); if (attr) nInc = parseInt(attr); } nCount += nInc; if (nCount <= g_nColsCount) { nEnd = j; j++; nOldCount = nCount; } else { if (nStart == j) nOldCount = nCount; break; } } while (j < g_nRowCount); oRows[nRow][0] = nStart+1; oRows[nRow][1] = nEnd+1; oRows[nRow][2] = nOldCount; nRow++; j = nEnd + 1; } return nRow; } function getValue(nRow, nCol) { if ((0 <= nRow) && (nRow< g_nRowCount) && oRows[nRow][nCol] != null) return oRows[nRow][nCol]; else return -1; } function getStart(nRow) { nRow--; return getValue(nRow,0); } function getEnd(nRow) { nRow--; return getValue(nRow,1); } function getWidth(nRow) { nRow--; return getValue(nRow,2); } ]]> </msxsl:script> <xsl:template match="/"> <xsl:variable name="ColsPerRow" select="number(books/col)" /> <html> <body> <table border="1"> <xsl:variable name="RowCount" select="user:init(books/book, $ColsPerRow)" /> <xsl:for-each select="books/book[position() <= $RowCount]"> <xsl:variable name="start" select="user:getStart(position())" /> <xsl:variable name="end" select="user:getEnd(position())" /> <!--***<xsl:value-of select="$start" />:<xsl:value-of select="$end" />***--> <xsl:if test="$start != -1"> <tr> <xsl:for-each select="/books/book[position() >= $start and position() <= $end]"> <td> <xsl:if test="style"><xsl:attribute name="colspan"><xsl:value-of select="style/@colspan" /></xsl:attribute></xsl:if> <xsl:value-of select="name" />:<xsl:value-of select="content" /></td> </xsl:for-each> <xsl:variable name="CurWidth" select="user:getWidth(position())" /> <xsl:if test="$CurWidth < $ColsPerRow"> <xsl:for-each select="(document('')//namespace::*)[position() <= $ColsPerRow - $CurWidth]"> <td><xsl:text> </xsl:text></td> </xsl:for-each> </xsl:if> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
newmung:
nOldCount 有什麽作用? if(style) if(attr) 假如style存在返回true,假如attr不存在返回FLASE? CurWidth是得到每一行结束的ID号吗? 下面用到NAMESPACE,有什麽用么?能给我说说吗? 哪个脚本有点注释就好了 <xsl:for-each select="(document('')//namespace::*)[position() <= $ColsPerRow - $CurWidth]">
saucer:
var style = nodeList[j].selectSingleNode("style"); if (style) //假如"style"节点不存在,style变量的值是null,该判断语句不真 { var attr = style.getAttribute("colspan"); if(attr) //假如"colspan"属性不存在,attr变量的值是null,该判断语句不真 $CurWidth是用来获取当前行的表格单元个数 <xsl:for-each select="(document('')//namespace::*)[position() <= $ColsPerRow - $CurWidth]"> <td><xsl:text> </xsl:text></td> </xsl:for-each> 假如$CurWidth小于所设表格单元的总个数,则在其后添加只含 的表格单元,我在这里偷懒了,没有用递归的方法添加表格单元,而是用一个循环语句,document('')//namespace::* 是获取本XSLT样式表中所有的namespace节点,估计其数量大于您所设的表格单元总个数,:-)
saucer:
nOldCount是在计算中用来记住当前行的表格单元个数