执行“分组依据"在 XPath XSL 中查询 [英] Performing a "Group By" query in XPath XSL

查看:24
本文介绍了执行“分组依据"在 XPath XSL 中查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下 XML:

<results name="queryResults">
  <int name="intfield1:[* TO 10]">11</int> 
  <int name="intfield2:[10 TO 20]">9</int> 
  <int name="intfield1:[10 TO 20]">12</int> 
</results>

我想生成这个 XML:

I would like to produce this XML:

<results>
    <field name="numberfield1">
        <value name="[* TO 10]">11</value>
        <value name="[10 TO 10]">12</value>
    </field>
    <field name="numberfield2">
        <value name="[10 TO 20]">9</value>
    </field>
</results>

我想不出如何在 XSL 中做到这一点,主要是因为我想按数字字段分组..我能想到的是:

I can't think how to do this in XSL mainly because i'm wanting to Group by the numbericfield.. All i can come up with is this:

<xsl:if test="count(results/int) &gt; 0">
    <results>
    <xsl:for-each select="results/int">
        <field>
            <xsl:attribute name="name">
                <xsl:value-of select="substring-before(@name, ':')"/></xsl:attribute>
            <value>
                <xsl:attribute name="name">
                    <xsl:value-of select="substring-after(@name, ':') "/>
                </xsl:attribute>
                <xsl:value-of select="."/>
            </value>
        </field>
    </xsl:for-each>
    </results>
</xsl:if>

但是这不会产生很好的分组列表,而是我得到了这个:

However this doesn't produce the nice grouped list instead i get this:

<results>
    <field name="numberfield1">
        <value name="[* TO 10]">11</value>
    </field>
    <field name="numberfield2">
        <value name="[10 TO 20]">9</value>
    </field>
    <field name="numberfield1">
        <value name="[10 TO 10]">12</value>
    </field>
</results>

如果有人能引导我朝着正确的方向前进..那会很棒吗?

If someone can stear me in the right direction.. That would be great?

谢谢

推荐答案

要在 XSLT 1.0 中执行此操作,您必须使用一种称为 "muenchian 分组".首先创建要分组的节点的键

To do this in XSLT 1.0, you will have to use a technique called "muenchian grouping". First create a key of the nodes on which you wish to group

<xsl:key name="intfield" match="int" use="substring-before(@name, ':')" />

接下来,遍历所有节点,但只选择相关组中碰巧排在第一位的节点

Next, you iterate it through all the nodes, but only selecting the ones that happen to be first in the relevant group

<xsl:for-each select="int[generate-id() = generate-id(key('intfield', substring-before(@name, ':'))[1])]">

接下来可以迭代使用key遍历组内的所有节点

Next, you can iterate use the key to iterate over all nodes in the group

<xsl:variable name="intfieldname" select="substring-before(@name, ':')"/>
<xsl:for-each select="key('intfield', $intfieldname)">

把这一切放在一起给出

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml"/>
   <xsl:key name="intfield" match="int" use="substring-before(@name, ':')"/>
   <xsl:template match="/results">
      <results>
         <xsl:for-each select="int[generate-id() = generate-id(key('intfield', substring-before(@name, ':'))[1])]">
            <xsl:variable name="intfieldname" select="substring-before(@name, ':')"/>
            <field>
               <xsl:attribute name="name">
                  <xsl:value-of select="$intfieldname"/>
               </xsl:attribute>
               <xsl:for-each select="key('intfield', $intfieldname)">
                  <value>
                     <xsl:attribute name="name">
                        <xsl:value-of select="substring-after(@name, ':')"/>
                     </xsl:attribute>
                     <xsl:value-of select="."/>
                  </value>
               </xsl:for-each>
            </field>
         </xsl:for-each>
      </results>
   </xsl:template>
</xsl:stylesheet>

在你的例子中,'intfield' 变成了 'numberfield'.在上面的示例中,我将名称保留为intfield".

In your example, 'intfield' becomes 'numberfield' though. I have kept the name as 'intfield' in the above example.

  • 修正了错字.

这篇关于执行“分组依据"在 XPath XSL 中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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