使用XSLT将XML节点与同一属性的不同值组合在一起

时间:2015-03-29 15:59:22

标签: xml xslt

我有一个xml文件,如下所示:

<text top="143" left="721" width="209" height="366" font="0">1</text>
<text top="222" left="608" width="221" height="52" font="1">Chapter 1</text>
<text top="481" left="171" width="488" height="71" font="2">Introduction to</text>
<text top="564" left="171" width="376" height="71" font="2">Information</text>
<text top="646" left="171" width="360" height="71" font="2">Technology</text>
<text top="731" left="171" width="387" height="31" font="3">Mind Tools for Your Future</text>
<text top="786" left="171" width="119" height="18" font="4">Key Questions</text>
<text top="813" left="171" width="344" height="10" font="5">You should be able to answer the following questions.</text>
<text top="840" left="171" width="19" height="10" font="5">1.1</text>
<text top="840" left="213" width="463" height="10" font="5">Infotech Becomes Commonplace: Cellphones, E-Mail, the Internet, &amp; the </text>
<text top="858" left="213" width="50" height="10" font="5">E-World </text>
<text top="856" left="265" width="568" height="13" font="6">How does information technology facilitate e-mail, networks, and the use of the Internet</text>
<text top="874" left="213" width="388" height="13" font="6">and the Web; what is the meaning of the term cyberspace?</text>
<text top="897" left="171" width="19" height="10" font="5">1.2</text>

文本节点的'height'属性具有不同的值。我想根据高度对文本节点进行分组。

预期产出:

<text>1</text>
<text>Chapter 1</text>
<text>Introduction to Information Technology</text>
<text>Mind Tools for Your Future</text>
<text>Key Questions</text>
<text>You should be able to answer the following questions. 1.1 Infotech Becomes Commonplace: Cellphones, E-Mail, the Internet, &amp; the E-World 1.2</text>
<text>How does information technology facilitate e-mail, networks, and the use of the Internet and the Web; what is the meaning of the term cyberspace?</text>

是否可以根据以下逻辑对文本节点进行分组:   - 将具有相同高度值的元素分组,直到遇到新的高度值

我想要实现的目标是:

<text>1</text>
<text>Chapter 1</text>
<text>Introduction to Information Technology</text>
<text>Mind Tools for Your Future</text>
<text>Key Questions</text>
<text>You should be able to answer the following questions. 1.1 Infotech Becomes Commonplace: Cellphones, E-Mail, the Internet, &amp; the E-World</text>
<text>How does information technology facilitate e-mail, networks, and the use of the Internet and the Web; what is the meaning of the term cyberspace?</text>
<text>1.2</text>

如高度= 10所示,文本节点

<text>1.2</text>

再次创建。它没有与前一个合并。

1 个答案:

答案 0 :(得分:2)

在XSLT 2.0中,您需要使用xsl:for-each-group和height属性作为group-by属性的值,例如

<xsl:template match="unnamed-parent-element">
  <xsl:for-each-group select="text" group-by="@height">
    <xsl:sort select="number(@height)"/>
    <div>
      <head>Text elements whose height is 
        <xsl:value-of select="current-grouping-key()"/>
      </head>
      <xsl:for-each select="current-group()">
        <xsl:copy-of select="."/>
        <!--* or do whatever you need to do with the
            * members of the group here ... *-->
      </
    </div>
  </
</

在XSLT 1.0中,您将需要搜索“Muenchian分组”,并阅读有关该技术的许多优秀讨论之一,如果只有一个人知道要搜索的术语。