Grouping With XSLT 2.0

本文介绍XSLT 2.0中新增的分组功能,包括按属性值分组、相邻元素分组、指定开始元素分组及指定结束元素分组等方法,并通过实例展示了如何使用这些功能进行数据处理。

by Bob DuCharme
November 05, 2003
Relational databases have always offered a feature known as grouping, that is, sorting a collection of records on a field or combination of fields and then treating each subcollection that has the same value in that sort key as a unit. For example, if the following XML document was stored in a relational database table, grouping the records by project value would let us print the records with a subhead for each project name at the beginning of that project's group of records, and it would let us find statistics such as the average or total size of the files in each project.

<files>
  <file name="swablr.eps"     size="4313" project="mars"/>
  <file name="batboy.wks"     size="424"  project="neptune"/>
  <file name="potrzebie.dbf"  size="1102" project="jupiter"/>
  <file name="kwatz.xom"      size="43"   project="jupiter"/>
  <file name="paisley.doc"    size="988"  project="neptune"/>
  <file name="ummagumma.zip"  size="2441" project="mars"/>
  <file name="schtroumpf.txt" size="389"  project="mars"/>
  <file name="mondegreen.doc" size="1993" project="neptune"/>
  <file name="gadabout.pas"   size="685"  project="jupiter"/>
</files>

While XSLT 1.0 lets you sort elements (see the July 2002 column for an introduction), it still forces you to jump through several hoops to do anything extra with the groups that result from the sort. Oracle's lead XML Technical Evangelist Steve Muench developed an approach using the xsl:key element, and this became so popular that it's known as the "Muenchian Method." Jeni Tennison has a fine explanation of it on her site.

XSLT 2.0 makes grouping even easier than Steve did. The XSLT 2.0 xsl:for-each-group instruction iterates across a series of groups, with the criteria for grouping specified by its attributes. The required select attribute identifies the elements to sort and group, and either the group-by, group-adjacent, group-starting-with, or group-ending-with attribute describes how to sort and group them.

Let's look at a simple example. The single template rule in the following XSLT 2.0 stylesheet tells the XSLT processor that when it finds a files element it should select all the file children of that element and sort them into groups based on the value of each file element's project attribute value. (All examples in this column are available in this zip file. To run them, use Saxon 7, the only XSLT processor current offering support for 2.0.)

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

  <xsl:output method="text"/>
    
  <xsl:template match="files">

    <xsl:for-each-group select="file" group-by="@project">
      <xsl:value-of select="current-grouping-key()"/>
      <xsl:text>
</xsl:text>
    </xsl:for-each-group>

  </xsl:template>

</xsl:stylesheet>

Just as the XSLT 1.0 xsl:for-each instruction iterates across a node set, with child elements of the xsl:for-each element specifying what you want done to each node in the set, the xsl:for-each-group instructions iterates across the groups, with children of the xsl:for-each-group element specifying what you want done to each group. The example above does two simple things as it finds each group:

  • It outputs the value of the current-grouping-key() function, which returns the grouping key value shared by the members of the group.
  • It outputs a carriage return.

Using the XML document shown earlier as a source document, the stylesheet creates this result:

mars
neptune
jupiter

It lists the grouping values. This ability to list all the different project values with no repeats in the list may seem simple, but it would have taken a lot more code in XSLT 1.0.

Let's replace the template rule with one that does a bit more:

<xsl:template match="files">

  <xsl:for-each-group select="file" group-by="@project">
    
    <xsl:for-each select="current-group()">
      <xsl:value-of select="@name"/>, <xsl:value-of select="@size"/>
      <xsl:text>
</xsl:text>
    </xsl:for-each>

    <xsl:text>average size for </xsl:text>
    <xsl:value-of select="current-grouping-key()"/>
    <xsl:text> group: </xsl:text>
    <xsl:value-of select="avg(current-group()/@size)"/>
    <xsl:text>

</xsl:text>
  </xsl:for-each-group>

</xsl:template>

The contents of this xsl:for-each element begin with an XSLT 1.0 xsl:for-each element which, as I mentioned, iterates across a set of nodes. By selecting the current-group() node set, the xsl:for-each element iterates over the nodes of the "mars" group in the first xsl:for-each-group pass, the nodes of the "neptune" group in the second pass, and those of the "jupiter" group in the final pass. Each iteration of the xsl:for-each instruction outputs the value of the name attribute of the context node (the node being processed by the loop), a comma, and the value of the context node's size attribute, finishing with a carriage return added with an xsl:text element.

After the xsl:for-each element iterates across the group being processed by the xsl:for-each-group element, the template outputs a message about the average size value within each group. To do this, it uses the current-grouping-key() function that we saw in our first stylesheet to name the group and the avg() function to compute the average. The argument to the avg() function is the node set consisting of the size attribute values of all the nodes in the current group.

Applied to the same source document, this second stylesheet produces this result:

swablr.eps, 4313
ummagumma.zip, 2441
schtroumpf.txt, 389
average size for mars group: 2381

batboy.wks, 424
paisley.doc, 988
mondegreen.doc, 1993
average size for neptune group: 1135

potrzebie.dbf, 1102
kwatz.xom, 43
gadabout.pas, 685
average size for jupiter group: 610

If the xsl:for-each-group element uses a group-adjacent attribute instead of a group-by attribute, it doesn't sort the selected elements, leaving them in their original order and grouping adjacent elements with the same key value together. For example, if we revise the previous stylesheet's template to look like this (note also the removal of the instructions that compute average file sizes),

<xsl:template match="files">

  <xsl:for-each-group select="file" group-adjacent="@project">
    
    <xsl:for-each select="current-group()">
      <xsl:value-of select="@name"/>, <xsl:value-of select="@size"/>
      <xsl:text>
</xsl:text>
    </xsl:for-each>

    <xsl:text>
</xsl:text>
  </xsl:for-each-group>

</xsl:template>

it only groups together the potrzebie.dbf/kwatz.xom pair and the ummagumma.zip/schtroumpf.txt pair, since those were the only contiguous file elements in our source documents that had the same project attribute value—"jupiter" for potrzebie.dbf and kwatz.xom and "mars" for ummagumma.zip and schtroumpf.txt.

swablr.eps, 4313

batboy.wks, 424

potrzebie.dbf, 1102
kwatz.xom, 43

paisley.doc, 988

ummagumma.zip, 2441
schtroumpf.txt, 389

mondegreen.doc, 1993

gadabout.pas, 685

The group-starting-with attribute names a node that the xsl:for-each-group element will treat as the beginning of a new group. This can add depth to a flat list of elements by enclosing groups of those elements in container elements. HTML documents, in which h1, h2, h3, and p elements after any of these headers are usually siblings, can benefit a lot from this; its flat structure makes it difficult for a stream-based parser to know which section of a document is ending when, and containing elements make this much easier. To add some depth to the following HTML document, the group-starting-with attribute can let us specify that each h1 element starts a new chapter:

<html><body>
<h1>Loomings</h1>
<p>par 1</p>
<p>par 2</p>
<p>par 3</p>
<h1>The Whiteness of the Whale</h1>
<p>par 4</p>
<p>par 5</p>
<p>par 6</p>
</body>
</html>

The following template rule does this to elements within a body element by specifying "h1" as the node starting each group that the XSLT processor should enclose in a chapter element. Note how the select attribute doesn't specify one kind of element to group, but all (*) children of the body element:

<xsl:template match="body">
  <body>
    <xsl:for-each-group select="*" group-starting-with="h1">
      <chapter>
      <xsl:for-each select="current-group()">
        <xsl:copy>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:for-each>
      </chapter>
    </xsl:for-each-group>
  </body>
</xsl:template>

Applying it to the HTML document shown above gives us this result:

<html>

   <body>
      <chapter>
         <h1>Loomings</h1>
         <p>par 1</p>
         <p>par 2</p>
         <p>par 3</p>
      </chapter>
      <chapter>
         <h1>The Whiteness of the Whale</h1>
         <p>par 4</p>
         <p>par 5</p>
         <p>par 6</p>
      </chapter>
   </body>
</html>

The fourth and last way to specify a grouping is the group-ending-with attribute, which names a pattern that identifies nodes that should end each group. The following template rule specifies that a group ends when it finds an element with any name (*) whose position, modulo 3, equals 0 -- in other words, any element whose position within its parent is a multiple of 3. The template rule also encloses the whole result in a book element.


<xsl:template match="files">
  <book>
    <xsl:for-each-group select="*"
               group-ending-with="*[position() mod 3 = 0]">
      <chapter>
        <xsl:for-each select="current-group()">
          <xsl:copy>
            <xsl:apply-templates  select="@*|node()"/>
          </xsl:copy>
        </xsl:for-each>
      </chapter>
    </xsl:for-each-group>
  </book>
</xsl:template>

A stylesheet with this template rule creates this result when using the files document we saw earlier:

<book>
   <chapter>
      <file name="swablr.eps" size="4313" project="mars"/>
      <file name="batboy.wks" size="424" project="neptune"/>
      <file name="potrzebie.dbf" size="1102" project="jupiter"/>
   </chapter>
   <chapter>
      <file name="kwatz.xom" size="43" project="jupiter"/>
      <file name="paisley.doc" size="988" project="neptune"/>
      <file name="ummagumma.zip" size="2441" project="mars"/>
   </chapter>
   <chapter>
      <file name="schtroumpf.txt" size="389" project="mars"/>
      <file name="mondegreen.doc" size="1993" project="neptune"/>
      <file name="gadabout.pas" size="685" project="jupiter"/>
   </chapter>
</book>

The group-by, group-adjacent, group-starting-with, and group-ending-with attributes can all name an element as the criterion to determine grouping boundaries; but, as this last example shows, you can be more creative than that, using functions and XPath predicates to identify the source tree nodes that should be treated as group boundaries. The Examples section of the XSLT 2.0 Working Draft's section on grouping has additional good demonstrations of what you can do with these attributes to customize the xsl:for-each-group element's treatment of your documents.

Demonstrating XSLT 2.0's grouping capability is easiest with simple, flat data that would fit easily into a normalized relational table; remember, however, that you can take advantage of it with all kinds of data, as long as you can count on finding the fields and attributes you need where you need them. After all, sometimes the whole point of using XML is that you have data that won't fit easily into normalized tables; it's nice to see more and more of the tricks for manipulating those tables coming to the world of XML development.

内容概要:本文详细介绍了一种基于Simulink的表贴式永磁同步电机(SPMSM)有限控制集模型预测电流控制(FCS-MPCC)仿真系统。通过构建PMSM数学模型、坐标变换、MPC控制器、SVPWM调制等模块,实现了对电机定子电流的高精度跟踪控制,具备快速动态响应和低稳态误差的特点。文中提供了完整的仿真建模步骤、关键参数设置、核心MATLAB函数代码及仿真结果分析,涵盖转速、电流、转矩和三相电流波形,验证了MPC控制策略在动态性能、稳态精度和抗负载扰动方面的优越性,并提出了参数自整定、加权代价函数、模型预测转矩控制和弱磁扩速等优化方向。; 适合人群:自动化、电气工程及其相关专业本科生、研究生,以及从事电机控制算法研究与仿真的工程技术人员;具备一定的电机原理、自动控制理论和Simulink仿真基础者更佳; 使用场景及目标:①用于永磁同步电机模型预测控制的教学演示、课程设计或毕业设计项目;②作为电机先进控制算法(如MPC、MPTC)的仿真验证平台;③支撑科研中对控制性能优化(如动态响应、抗干扰能力)的研究需求; 阅读建议:建议读者结合Simulink环境动手搭建模型,深入理解各模块间的信号流向与控制逻辑,重点掌握预测模型构建、代价函数设计与开关状态选择机制,并可通过修改电机参数或控制策略进行拓展实验,以增强实践与创新能力。
根据原作 https://pan.quark.cn/s/23d6270309e5 的源码改编 湖北省黄石市2021年中考数学试卷所包含的知识点广泛涉及了中学数学的基础领域,涵盖了实数、科学记数法、分式方程、几何体的三视图、立体几何、概率统计以及代数方程等多个方面。 接下来将对每道试题所关联的知识点进行深入剖析:1. 实数与倒数的定义:该题目旨在检验学生对倒数概念的掌握程度,即一个数a的倒数表达为1/a,因此-7的倒数可表示为-1/7。 2. 科学记数法的运用:科学记数法是一种表示极大或极小数字的方法,其形式为a×10^n,其中1≤|a|<10,n为整数。 此题要求学生运用科学记数法表示一个天文单位的距离,将1.4960亿千米转换为1.4960×10^8千米。 3. 分式方程的求解方法:考察学生解决包含分母的方程的能力,题目要求找出满足方程3/(2x-1)=1的x值,需通过消除分母的方式转化为整式方程进行解答。 4. 三视图的辨认:该题目测试学生对于几何体三视图(主视图、左视图、俯视图)的认识,需要识别出具有两个相同视图而另一个不同的几何体。 5. 立体几何与表面积的计算:题目要求学生计算由直角三角形旋转形成的圆锥的表面积,要求学生对圆锥的底面积和侧面积公式有所了解并加以运用。 6. 统计学的基础概念:题目涉及众数、平均数、极差和中位数的定义,要求学生根据提供的数据信息选择恰当的统计量。 7. 方程的整数解求解:考察学生在实际问题中进行数学建模的能力,通过建立方程来计算在特定条件下帐篷的搭建方案数量。 8. 三角学的实际应用:题目通过在直角三角形中运用三角函数来求解特定线段的长度。 利用正弦定理求解AD的长度是解答该问题的关键。 9. 几何变换的应用:题目要求学生运用三角板的旋转来求解特定点的...
Python基于改进粒子群IPSO与LSTM的短期电力负荷预测研究内容概要:本文围绕“Python基于改进粒子群IPSO与LSTM的短期电力负荷预测研究”展开,提出了一种结合改进粒子群优化算法(IPSO)与长短期记忆网络(LSTM)的混合预测模型。通过IPSO算法优化LSTM网络的关键参数(如学习率、隐层节点数等),有效提升了模型在短期电力负荷预测中的精度与收敛速度。文中详细阐述了IPSO算法的改进策略(如引入自适应惯性权重、变异机制等),增强了全局搜索能力与避免早熟收敛,并利用实际电力负荷数据进行实验验证,结果表明该IPSO-LSTM模型相较于传统LSTM、PSO-LSTM等方法在预测准确性(如MAE、RMSE指标)方面表现更优。研究为电力系统调度、能源管理提供了高精度的负荷预测技术支持。; 适合人群:具备一定Python编程基础、熟悉基本机器学习算法的高校研究生、科研人员及电力系统相关领域的技术人员,尤其适合从事负荷预测、智能优化算法应用研究的专业人士。; 使用场景及目标:①应用于短期电力负荷预测,提升电网调度的精确性与稳定性;②为优化算法(如粒子群算法)与深度学习模型(如LSTM)的融合应用提供实践案例;③可用于学术研究、毕业论文复现或电力企业智能化改造的技术参考。; 阅读建议:建议读者结合文中提到的IPSO与LSTM原理进行理论学习,重点关注参数优化机制的设计思路,并动手复现实验部分,通过对比不同模型的预测结果加深理解。同时可拓展尝试将该方法应用于其他时序预测场景。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值