Xml 将一列转换为两列?
我使用一个模板来显示我在寻找什么。XSLT对我来说是新事物 XML:Xml 将一列转换为两列?,xml,xslt,Xml,Xslt,我使用一个模板来显示我在寻找什么。XSLT对我来说是新事物 XML: 根据输入,xml输出不正确。由于没有指定输出格式,下面的XSLT只输出一个简单的HTML <xsl:template match="items"> <!-- original XML should be wrapped in a parent element `items`--> <xsl:variable name="rows" select="count(item) div 2"/&g
根据输入,xml输出不正确。由于没有指定输出格式,下面的XSLT只输出一个简单的HTML
<xsl:template match="items"> <!-- original XML should be wrapped in a parent element `items`-->
<xsl:variable name="rows" select="count(item) div 2"/>
<xsl:for-each select="item[position() <= $rows]">
<xsl:variable name="pos" select="position()"/>
<xsl:value-of select="name"/>
<xsl:value-of select="../item[$pos + $rows]"/>
<br/>
</xsl:for-each>
</xsl:template>
更新因为您只是明确指定输出格式为HTML格式,所以这种方式可能更好:
<xsl:template match="items">
<xsl:variable name="rows" select="count(item) div 2"/>
<ul id="left-column">
<xsl:for-each select="item[position() <= $rows]">
<li><xsl:value-of select="name"/></li>
</xsl:for-each>
</ul>
<ul id="right-column">
<xsl:for-each select="item[position() > $rows]">
<li><xsl:value-of select="name"/></li>
</xsl:for-each>
</ul>
</xsl:template>
为了使输出完全满足您的要求,我们需要添加CSS样式,使左栏向左浮动,右栏向右浮动,并自定义列表样式、间距等。这应该可以:
<xsl:template match="items">
<table>
<xsl:variable name="rowCount" select="ceiling(count(info) div 2)" />
<xsl:for-each select="info[position() <= $rowCount]">
<xsl:variable name="pos" select="position()" />
<td>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="../info[$pos + $rowCount]/Name"/>
</td>
</td>
</xsl:for-each>
</table>
</xsl:template>
而且xml中没有定义的节点数。Thanks是输出模式text
或html
?是的,我正在查找html。是的,我正在查找html。它有什么问题?你想给我们一些你特别想要的信息吗?那不是HTML。正如我上面所说,有无数种方法可以在HTML中生成两列。你想要什么样的HTML?AEBFOk,这很有意义。在上面添加了一个解决方案。它类似于上面Hui Zheng的第一种方法,但添加了表标记。
<xsl:template match="items">
<xsl:variable name="rows" select="count(item) div 2"/>
<ul id="left-column">
<xsl:for-each select="item[position() <= $rows]">
<li><xsl:value-of select="name"/></li>
</xsl:for-each>
</ul>
<ul id="right-column">
<xsl:for-each select="item[position() > $rows]">
<li><xsl:value-of select="name"/></li>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template match="items">
<table>
<xsl:variable name="rowCount" select="ceiling(count(info) div 2)" />
<xsl:for-each select="info[position() <= $rowCount]">
<xsl:variable name="pos" select="position()" />
<td>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="../info[$pos + $rowCount]/Name"/>
</td>
</td>
</xsl:for-each>
</table>
</xsl:template>