XML/XSL 表格式

发布于 2024-09-10 17:01:56 字数 387 浏览 2 评论 0原文

假设我有一个包含 500 个 XML 标签名称的列表。我想使用 XSLT 在表格中显示此列表。在 XSLT 表中,我希望最多有三行;我不在乎我有多少列 - 我计划将表格放入可滚动的 div 中。如果我的姓名列表增长到 1,000,我仍然只需要三行,但列的数量可以增长到所需的任何大小。

我怎样才能在 XSLT 中做到这一点?就 XSL 表而言,我熟悉 xsl:for-each,但仅此而已。

格式是这样的:

<节点1> <节点2> <名称>数据< /名称> <名称>数据< /名称> ... < /节点2> < /节点1>

Let's say I have a list of 500 names in XML tags. I want to display this list in a table using XSLT. In the XSLT table I want to have a maximum of three rows; I don't care how many columns I have - I plan to put the table into a scrollable div. If my list of names grows to 1,000 I still want only three rows, but the amount of columns can grow to whatever size it needs to.

How can I do this in XSLT? As far as XSL tables go, I'm familiar with the xsl:for-each, but that's about it.

The format is something like this:

< node1 >
< node2 >
< NAME >data< /NAME >
< NAME >data< /NAME >
...
< /node2 >
< /node1 >

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

跨年 2024-09-17 17:01:56

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pnumRows" select="3"/>

 <xsl:variable name="vnumCells" select="count(/*/*/NAME)"/>
 <xsl:variable name="vcellsPerRow"
       select="ceiling($vnumCells div $pnumRows)"/>


 <xsl:template match="node2">
  <table>
    <xsl:apply-templates select="NAME[position() mod $vcellsPerRow = 1]"/>
  </table>
 </xsl:template>

 <xsl:template match="NAME">
  <tr>
    <xsl:apply-templates mode="copy" select=
    ". | following-sibling::*[not(position() >= $vcellsPerRow)]"/>
  </tr>
 </xsl:template>

 <xsl:template match="NAME" mode="copy">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>

在具有指定格式的任何 XML 文档上执行时,如下所示

<node1>
    <node2>
        <NAME>name1</NAME>
        <NAME>name2</NAME>
        <NAME>name3</NAME>
        <NAME>name4</NAME>
        <NAME>name5</NAME>
        <NAME>name6</NAME>
        <NAME>name7</NAME>
        <NAME>name8</NAME>
        <NAME>name9</NAME>
        <NAME>name10</NAME>
    </node2>
</node1>

产生所需的结果:包含三行的表格 ,其中前两行具有相同数量的单元格,最后一行可能更短:

    <table>
   <tr>
      <td>name1</td>
      <td>name2</td>
      <td>name3</td>
      <td>name4</td>
   </tr>
   <tr>
      <td>name5</td>
      <td>name6</td>
      <td>name7</td>
      <td>name8</td>
   </tr>
   <tr>
      <td>name9</td>
      <td>name10</td>
   </tr>
</table>

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pnumRows" select="3"/>

 <xsl:variable name="vnumCells" select="count(/*/*/NAME)"/>
 <xsl:variable name="vcellsPerRow"
       select="ceiling($vnumCells div $pnumRows)"/>


 <xsl:template match="node2">
  <table>
    <xsl:apply-templates select="NAME[position() mod $vcellsPerRow = 1]"/>
  </table>
 </xsl:template>

 <xsl:template match="NAME">
  <tr>
    <xsl:apply-templates mode="copy" select=
    ". | following-sibling::*[not(position() >= $vcellsPerRow)]"/>
  </tr>
 </xsl:template>

 <xsl:template match="NAME" mode="copy">
  <td><xsl:value-of select="."/></td>
 </xsl:template>
</xsl:stylesheet>

when performed on any XML document with the specified format, such as the following:

<node1>
    <node2>
        <NAME>name1</NAME>
        <NAME>name2</NAME>
        <NAME>name3</NAME>
        <NAME>name4</NAME>
        <NAME>name5</NAME>
        <NAME>name6</NAME>
        <NAME>name7</NAME>
        <NAME>name8</NAME>
        <NAME>name9</NAME>
        <NAME>name10</NAME>
    </node2>
</node1>

produces the wanted result: a table with three rows, where the first two rows have the same number of cells and the last row may be shorter:

    <table>
   <tr>
      <td>name1</td>
      <td>name2</td>
      <td>name3</td>
      <td>name4</td>
   </tr>
   <tr>
      <td>name5</td>
      <td>name6</td>
      <td>name7</td>
      <td>name8</td>
   </tr>
   <tr>
      <td>name9</td>
      <td>name10</td>
   </tr>
</table>
如痴如狂 2024-09-17 17:01:56

如果您正在讨论使用 XSLT 将 XML 转换为 HTML 表,我建议您查看 这篇文章。他提供了一些模板,允许您执行您正在谈论的操作,但可能需要进行一些调整。

If you are talking about transforming XML into a HTML table using XSLT I'd recommend taking a look at this article. He provides some templates that will allow you to do what you are talking about, but it will probably take a little tweaking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文