xslt 1.0 中每 3 项进行分组

发布于 2024-08-24 10:15:34 字数 1140 浏览 8 评论 0原文

我无法找到一种对 xslt 1.0 项目进行分组的方法。 我有一个类似于下面的源 xml:

<client name="client A">
    <project name = "project A1"/>
    <project name = "project A2"/>
    <project name = "project A3"/>
    <project name = "project A4"/>
</client>
<client name="client B">
    <project name = "project B1"/>
    <project name = "project B2"/>
</client>
<client name="client C">
    <project name = "project C1"/>
    <project name = "project C2"/>
    <project name = "project C3"/>
</client>

我想选择所有项目,对它们进行排序,然后将每 3 个项目分组到一个边界中,如下例所示:

<boundle>
  <project name="project A1">
  <project name="project A2">
  <project name="project A3">
</boundle>
<boundle>
  <project name="project A4">
  <project name="project B1">
  <project name="project B2">
</boundle>
<boundle>
  <project name="project C1">
  <project name="project C2">
  <project name="project C3">
</boundle>

目前,为了这样做,我正在使用打开一个边界标签并稍后关闭它。 你能想出更好的解决办法吗?

I'm having troubles to figure out a way to group items xslt 1.0.
I have a source xml similar to the one below:

<client name="client A">
    <project name = "project A1"/>
    <project name = "project A2"/>
    <project name = "project A3"/>
    <project name = "project A4"/>
</client>
<client name="client B">
    <project name = "project B1"/>
    <project name = "project B2"/>
</client>
<client name="client C">
    <project name = "project C1"/>
    <project name = "project C2"/>
    <project name = "project C3"/>
</client>

I'd like to select all projects, sort them and then group every 3 project in one boundle as in the example below:

<boundle>
  <project name="project A1">
  <project name="project A2">
  <project name="project A3">
</boundle>
<boundle>
  <project name="project A4">
  <project name="project B1">
  <project name="project B2">
</boundle>
<boundle>
  <project name="project C1">
  <project name="project C2">
  <project name="project C3">
</boundle>

Currently to do so I'm using to open a boundle tag and close it later.
Can you think about any better solution?

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

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

发布评论

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

评论(2

骄兵必败 2024-08-31 10:15:34

无需分组。

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

<xsl:variable name="allProjects" select="/client/project" />

<xsl:template match="/">
  <xsl:apply-templates select="$allProjects" mode="counted" />
</xsl:template>

<xsl:template match="project" mode="counted">
  <xsl:if test="position() mod $perGroup = 1">
    <xsl:variable name="pos" select="position()" />
    <boundle>
      <xsl:copy-of select="$allProjects[
        position() >= $pos and position() < ($pos + $perGroup)
      ]" />
    </boundle>
  </xsl:template>
</xsl:template>

No grouping necessary.

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

<xsl:variable name="allProjects" select="/client/project" />

<xsl:template match="/">
  <xsl:apply-templates select="$allProjects" mode="counted" />
</xsl:template>

<xsl:template match="project" mode="counted">
  <xsl:if test="position() mod $perGroup = 1">
    <xsl:variable name="pos" select="position()" />
    <boundle>
      <xsl:copy-of select="$allProjects[
        position() >= $pos and position() < ($pos + $perGroup)
      ]" />
    </boundle>
  </xsl:template>
</xsl:template>
征﹌骨岁月お 2024-08-31 10:15:34

我可以推荐以下链接 http://www.jenitennison.com/xslt/grouping/这帮助我弄清楚如何根据 XML 文档中的内容将不同数量的元素分组为某些类别。

I can recommend the following link http://www.jenitennison.com/xslt/grouping/ which helped me figuring out how to group various number of elements into some categories based on the contents in the XML document.

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