按 Xsl 中子节点的值对 xml 节点进行分组

编程入门 行业动态 更新时间:2024-10-15 20:22:18
本文介绍了按 Xsl 中子节点的值对 xml 节点进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..
<root>
<element>
<id>1</id>
<group>first</group>
</element>

<element>
<id>2</id>
<group>second</group>
</element>


<element>
<id>3</id>
<group>first</group>
</element>
...
<root>

如何在 xslt 1.0 中按组名对元素进行分组.输出:

How I can group my elements by the group name in xslt 1.0. the output:

<root>
<group name="first">
 <element>
    <id>1</id>
    <group>first</group>
 </element>
 <element>
    <id>3</id>
    <group>first</group>
 </element>
</group>
<group name="second">
 <element>
    <id>2</id>
    <group>second</group>
    </element>
</group>
</root>

有什么想法吗?

推荐答案

这是 Muenchian Grouping 的工作.您将在 StackOverflow 上的 XSLT 标记中找到大量示例.

This is a job for Muenchian Grouping. You will numerous examples of it within the XSLT tag here on StackOverflow.

首先,您需要定义一个键来帮助您对组进行分组

First, you need to define a key to help you group the groups

<xsl:key name="groups" match="group" use="."/>

这将查找给定组名的 group 元素.

This will look up group elements for a given group name.

接下来,您需要匹配每个差异组名称的第一个实例的所有匹配项.这是通过这个看起来很可怕的声明完成的

Next, you need to match all the occurrences of the first instance of each distince group name. This is done with this scary looking statement

<xsl:apply-templates select="element/group[generate-id() = generate-id(key('groups', .)[1])]"/>

即匹配在我们的键中第一次出现该元素的组元素.

i.e Match group elements which happen to be the first occurence of that element in our key.

当您匹配了不同的组节点后,您可以遍历所有其他同名的组节点(其中 $currentGroup 是保存当前组名的变量)

When you have matched the distinct group nodes, you can then loop through all other group nodes with the same name (where $currentGroup is a variable holding the current group name)

<xsl:for-each select="key('groups', $currentGroup)">

把这个放在一起给出

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3/1999/XSL/Transform">

   <xsl:key name="groups" match="group" use="."/>

   <xsl:template match="/root">
      <root>
         <xsl:apply-templates select="element/group[generate-id() = generate-id(key('groups', .)[1])]"/>
      </root>
   </xsl:template>

   <xsl:template match="group">
      <xsl:variable name="currentGroup" select="."/>
      <group>
         <xsl:attribute name="name">
            <xsl:value-of select="$currentGroup"/>
         </xsl:attribute>
         <xsl:for-each select="key('groups', $currentGroup)">
            <element>
               <id>
                  <xsl:value-of select="../id"/>
               </id>
               <name>
                  <xsl:value-of select="$currentGroup"/>
               </name>
            </element>
         </xsl:for-each>
      </group>
   </xsl:template>

</xsl:stylesheet>

将其应用于您的示例 XML 会得到以下结果

Applying this on your sample XML gives the following result

<root>
   <group name="first">
      <element>
         <id>1</id>
         <name>first</name>
      </element>
      <element>
         <id>3</id>
         <name>first</name>
      </element>
   </group>
   <group name="seccond">
      <element>
         <id>2</id>
         <name>seccond</name>
      </element>
   </group>
</root>

这篇关于按 Xsl 中子节点的值对 xml 节点进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 07:23:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1058701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   中子   Xsl   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!