在XSLT中的节点集上分组 [英] Grouping on a Node-Set in XSLT

查看:67
本文介绍了在XSLT中的节点集上分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道XSLT的Muenchian分组方法,但是我看到的所有实现都依赖于单个节点作为要分组的值.在这种情况下,我想对一个节点集进行分组.在下面的输入中,我想对输出/输出零件参考进行分组.

I'm aware of the Muenchian grouping method for XSLT, but all implementations I've seen rely on a single node as the value to group on. In this case I'd like to group on a node-set. In the input below I'd like to group on outputs/output part ref.

我尝试构造

<xsl:key name="refsKey" match="/processes/process" use="outputs/output_part_ref"/>

当然,outputs/output_part_ref与第一个节点匹配,而与节点集不匹配.

Of course outputs/output_part_ref matches the first node and doesn't match the node-set.

输入

<?xml version="1.0" encoding="UTF-8"?>
<processes>
    <process>
        <name>ProcessA</name>
        <input_qty>1200</input_qty>
        <outputs>
            <output_part_ref>1</output_part_ref>
            <output_part_ref>2</output_part_ref>
            <output_part_ref>3</output_part_ref>
        </outputs>
    </process>
    <process>
        <name>ProcessB</name>
        <input_qty>1300</input_qty>
        <outputs>
            <output_part_ref>1</output_part_ref>
            <output_part_ref>2</output_part_ref>
            <output_part_ref>3</output_part_ref>
        </outputs>
    </process>
    <process>
        <name>ProcessC</name>
        <input_qty>770</input_qty>
        <outputs>
            <output_part_ref>1</output_part_ref>
            <output_part_ref>2</output_part_ref>
        </outputs>
    </process>
</processes>

示例输出

<html>
...
<table>
    <tr>
        <td>2500</td>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>770</td>
        <td>1</td>
        <td>2</td>
    </tr>
</table>
...
</html>

推荐答案

如果构成密钥的元素数量不固定,那么我同意Michael,我们需要首先计算密钥,并在XSLT中使用exsl:node-set或类似的方法1.0:

If the number of elements forming the key is not fixed then I agree with Michael, we need to compute the key first and use exsl:node-set or similar in XSLT 1.0:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl">

<xsl:output method="html" indent="yes"/>

<xsl:key name="refsKey" match="process" use="key"/>

<xsl:variable name="rtf1">
  <xsl:apply-templates select="processes/process" mode="key"/>
</xsl:variable>

<xsl:template match="process" mode="key">
  <xsl:copy>
    <key>
      <xsl:for-each select="outputs/output_part_ref">
        <xsl:sort select="." data-type="number"/>
        <xsl:if test="position() > 1">|</xsl:if>
        <xsl:value-of select="."/>
      </xsl:for-each>
    </key>
    <xsl:copy-of select="node()"/>
  </xsl:copy>
</xsl:template>

<xsl:variable name="ns1" select="exsl:node-set($rtf1)/process"/>

<xsl:template match="/">
  <html>
    <body>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

<xsl:template match="processes">
  <table>
    <xsl:apply-templates select="$ns1[generate-id() = generate-id(key('refsKey', key)[1])]"/>
  </table>
</xsl:template>

<xsl:template match="process">
  <tr>
    <td><xsl:value-of select="sum(key('refsKey', key)/input_qty)"/></td>
    <xsl:for-each select="outputs/output_part_ref">
      <td>
        <xsl:value-of select="."/>
      </td>
    </xsl:for-each>
  </tr>
</xsl:template>

</xsl:stylesheet>

这篇关于在XSLT中的节点集上分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆