在XSL中,格式化输出XML数据的有关问题,高手
在XSL中,格式化输出XML数据的问题,急!!!在线等!!高手请指教!
XML:
<?xml version= "1.0 " encoding= "gb2312 "?>
<subject>
<class>
<classid> 1 </classid>
<text> 数据一 </text>
</class>
<class>
<classid> 2 </classid>
<text> 数据二 </text>
</class>
<class>
<classid> 3 </classid>
<text> 数据三 </text>
</class>
<class>
<classid> 4 </classid>
<text> 数据四 </text>
</class>
<class>
<classid> 5> </classid>
<text> 数据五 </text>
</class>
</subject>
XSL:
…… ??
-------------------------------------------
我想将XML中class节点的数据显示在表格的一行两列中,如下:
-----------------------------------
| class[1] | class[4] |
| class[2] | class[5] |
| class[3] | |
-----------------------------------
class节点的数目有可能是偶数,也有可能是奇数。如果是偶数,就平均显示,如果是奇数,就让最中间的节点数据显示在第一列的最后,第二列显示余下数据,就如上表所示。
我思考了一下,这肯定需要得到class节点的数目,奈何我是初学,这些都不懂,而且比较急需要解决这个问题,查资料一时也查不到。请各位高手帮我解决一下,写出XSL来。。。。
谢谢。。
------解决方案--------------------
横排有现成的,竖排麻烦
http://dotnet.aspx.cc/article/yawo3qgm-xd53-4d3d-oybr-blsbx5bngaym/read.aspx
------解决方案--------------------
<?xml version= '1.0 ' encoding= 'gb2312 '?>
<xsl:stylesheet version= "1.0 "
xmlns= "http://www.w3.org/TR/REC-html40 "
xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:template match= "/ ">
<table border = "1 ">
<xsl:for-each select= "//class ">
<xsl:if test= "(position() mod 2 = 1) ">
<xsl:variable name= "n " select= "position() "/>
<tr> <td> <xsl:value-of select= ". "/> </td>
<td> <xsl:value-of select= "//class[$n+1] " /> </td> </tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
------解决方案--------------------
优化后:
<?xml version= '1.0 ' encoding= 'gb2312 '?>
<xsl:stylesheet version= "1.0 "
xmlns= "http://www.w3.org/TR/REC-html40 "
xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:template match= "/ ">
<table border = "1 ">
<xsl:apply-templates select= "//class[position() mod 2 = 1] " />
XML:
<?xml version= "1.0 " encoding= "gb2312 "?>
<subject>
<class>
<classid> 1 </classid>
<text> 数据一 </text>
</class>
<class>
<classid> 2 </classid>
<text> 数据二 </text>
</class>
<class>
<classid> 3 </classid>
<text> 数据三 </text>
</class>
<class>
<classid> 4 </classid>
<text> 数据四 </text>
</class>
<class>
<classid> 5> </classid>
<text> 数据五 </text>
</class>
</subject>
XSL:
…… ??
-------------------------------------------
我想将XML中class节点的数据显示在表格的一行两列中,如下:
-----------------------------------
| class[1] | class[4] |
| class[2] | class[5] |
| class[3] | |
-----------------------------------
class节点的数目有可能是偶数,也有可能是奇数。如果是偶数,就平均显示,如果是奇数,就让最中间的节点数据显示在第一列的最后,第二列显示余下数据,就如上表所示。
我思考了一下,这肯定需要得到class节点的数目,奈何我是初学,这些都不懂,而且比较急需要解决这个问题,查资料一时也查不到。请各位高手帮我解决一下,写出XSL来。。。。
谢谢。。
------解决方案--------------------
横排有现成的,竖排麻烦
http://dotnet.aspx.cc/article/yawo3qgm-xd53-4d3d-oybr-blsbx5bngaym/read.aspx
------解决方案--------------------
<?xml version= '1.0 ' encoding= 'gb2312 '?>
<xsl:stylesheet version= "1.0 "
xmlns= "http://www.w3.org/TR/REC-html40 "
xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:template match= "/ ">
<table border = "1 ">
<xsl:for-each select= "//class ">
<xsl:if test= "(position() mod 2 = 1) ">
<xsl:variable name= "n " select= "position() "/>
<tr> <td> <xsl:value-of select= ". "/> </td>
<td> <xsl:value-of select= "//class[$n+1] " /> </td> </tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
------解决方案--------------------
优化后:
<?xml version= '1.0 ' encoding= 'gb2312 '?>
<xsl:stylesheet version= "1.0 "
xmlns= "http://www.w3.org/TR/REC-html40 "
xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:template match= "/ ">
<table border = "1 ">
<xsl:apply-templates select= "//class[position() mod 2 = 1] " />