XSLT 1.0 计算属性中具有相同值的元素,并显示它

发布于 2024-09-04 05:42:27 字数 461 浏览 4 评论 0原文

我有一个变量,其中包含:

<col p1="Newman" p2="Paul"/>
...
<col p1="Newman" p2="Paolo"/>
<col p1="Newman" p2="Paul"/>

我将输出一个表,其中第一列是 p2 的值,第二列是它出现的次数。对于 p2 的每个值,我应该只有一行。

<table>
<tr><td>p2</td><td>num</td></tr>
<tr><td>Pault</td><td>2</td>
...
<tr><td>Paolo</td><td>1</td>
</table>

I have a variable containing:

<col p1="Newman" p2="Paul"/>
...
<col p1="Newman" p2="Paolo"/>
<col p1="Newman" p2="Paul"/>

i wold in output a table with in the first column the value of p2 and in the second the number of time it appear. For each value of p2 should I have only a row.

<table>
<tr><td>p2</td><td>num</td></tr>
<tr><td>Pault</td><td>2</td>
...
<tr><td>Paolo</td><td>1</td>
</table>

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

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

发布评论

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

评论(1

许你一世情深 2024-09-11 05:42:27

使用 XSL 密钥即可轻松完成。

<!-- index all <col> elements by their @p2 attribute -->
<xsl:key name="kColByFirstname" match="col" use="@p2" />

<xsl:template match="/RootElement">
  <table>
    <tr>
      <td>p2</td><td>num</td>
    </tr>
    <!-- select the respective first <col> of each group -->
    <xsl:apply-templates select="col[
      generate-id() = generate-id(key('kColByFirstname', @p2)[1])
    ]" />
  </table>
</xsl:template>

<xsl:template match="col">
  <tr>
    <!-- output the current value of @p2 and its group count -->
    <td><xsl:value-of select="@p2"/></td>
    <td><xsl:value-of select="count(key('kColByFirstname', @p2))" /></td>
  </tr>
</xsl:template>

Easy with an XSL key.

<!-- index all <col> elements by their @p2 attribute -->
<xsl:key name="kColByFirstname" match="col" use="@p2" />

<xsl:template match="/RootElement">
  <table>
    <tr>
      <td>p2</td><td>num</td>
    </tr>
    <!-- select the respective first <col> of each group -->
    <xsl:apply-templates select="col[
      generate-id() = generate-id(key('kColByFirstname', @p2)[1])
    ]" />
  </table>
</xsl:template>

<xsl:template match="col">
  <tr>
    <!-- output the current value of @p2 and its group count -->
    <td><xsl:value-of select="@p2"/></td>
    <td><xsl:value-of select="count(key('kColByFirstname', @p2))" /></td>
  </tr>
</xsl:template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文