Grouping 使用XSLT对XML元素进行分组';当节点集存储在变量中时,每个组和以属性开头的组的

Grouping 使用XSLT对XML元素进行分组';当节点集存储在变量中时,每个组和以属性开头的组的,grouping,xslt-2.0,Grouping,Xslt 2.0,我正在处理一个以非结构化方式保存表格数据的源HTML文件。基本上是一堆绝对定位的divs。我的目标是重建某种结构化XML数据。到目前为止,使用XSLT 2.0,我能够生成如下所示的XML: <data> <line top="44"> <item left="294">Some heading text</item> </line> <line top="47">

我正在处理一个以非结构化方式保存表格数据的源HTML文件。基本上是一堆绝对定位的
div
s。我的目标是重建某种结构化XML数据。到目前为止,使用XSLT 2.0,我能够生成如下所示的XML:

<data>
    <line top="44">
         <item left="294">Some heading text</item>
    </line>
    <line top="47">
         <item left="718">A</item> <!-- this item is a section-start -->
         <item left="764">Section heading</item>
    </line>
    <line top="78">
        <item left="92">Data</item>
        <item left="144">Data</item>
        <item left="540">Data</item>
        <item left="588">Data</item>
    </line>
    <line top="101">
        <item left="61">B</item> <!-- this item is a section-start -->
        <item left="144">Section heading</item>
    </line>
    <line top="123">
        <item left="92">Data</item>
        <item left="144">Data</item>
    </line>
</data>
<xsl:for-each-group select="$lines/line" group-starting-with="...pattern here...">
    <section>
        <xsl:copy-of select="current-group()"/>
    </section>
</xsl:for-each-group>
<data>
    <section> <!-- this section started automatically because of being at the beginning -->
        <line top="44">
             <item left="294">Some heading text</item>
        </line>
    </section>
    <section>
        <line top="47">
             <item left="718">A</item> <!-- this item is a section-start -->
             <item left="764">Section heading</item>
        </line>
        <line top="78">
            <item left="92">Data</item>
            <item left="144">Data</item>
            <item left="540">Data</item>
            <item left="588">Data</item>
        </line>
    </section>
    <section>
        <line top="101">
            <item left="61">B</item> <!-- this item is a section-start -->
            <item left="144">Section heading</item>
        </line>
        <line top="123">
            <item left="92">Data</item>
            <item left="144">Data</item>
        </line>
    </section>
</data>
问题是我无法找出一种工作模式来识别节的开始。我所能做的就是确保
//line/item[1]/text()[匹配(,“^[A-Z]$”)]
在XPath计算器中单独使用时有效。然而,我似乎无法派生出一个工作版本,用于以开头的

更新,因此所需结果应如下所示:

<data>
    <line top="44">
         <item left="294">Some heading text</item>
    </line>
    <line top="47">
         <item left="718">A</item> <!-- this item is a section-start -->
         <item left="764">Section heading</item>
    </line>
    <line top="78">
        <item left="92">Data</item>
        <item left="144">Data</item>
        <item left="540">Data</item>
        <item left="588">Data</item>
    </line>
    <line top="101">
        <item left="61">B</item> <!-- this item is a section-start -->
        <item left="144">Section heading</item>
    </line>
    <line top="123">
        <item left="92">Data</item>
        <item left="144">Data</item>
    </line>
</data>
<xsl:for-each-group select="$lines/line" group-starting-with="...pattern here...">
    <section>
        <xsl:copy-of select="current-group()"/>
    </section>
</xsl:for-each-group>
<data>
    <section> <!-- this section started automatically because of being at the beginning -->
        <line top="44">
             <item left="294">Some heading text</item>
        </line>
    </section>
    <section>
        <line top="47">
             <item left="718">A</item> <!-- this item is a section-start -->
             <item left="764">Section heading</item>
        </line>
        <line top="78">
            <item left="92">Data</item>
            <item left="144">Data</item>
            <item left="540">Data</item>
            <item left="588">Data</item>
        </line>
    </section>
    <section>
        <line top="101">
            <item left="61">B</item> <!-- this item is a section-start -->
            <item left="144">Section heading</item>
        </line>
        <line top="123">
            <item left="92">Data</item>
            <item left="144">Data</item>
        </line>
    </section>
</data>

一些标题文本
A.
章节标题
资料
资料
资料
资料
B
章节标题
资料
资料
解决方案:

<xsl:for-each-group select="$lines/line" group-starting-with="line[matches(child::item[1], '^[A-Z]$')]">
    <section name="{current-group()[1]/item[1]}">
        <xsl:copy-of select="current-group()"/>
    </section>
</xsl:for-each-group>

诀窍在于真正理解以
开头的
组应该是一种模式而不是一种条件。

解决方案:

<xsl:for-each-group select="$lines/line" group-starting-with="line[matches(child::item[1], '^[A-Z]$')]">
    <section name="{current-group()[1]/item[1]}">
        <xsl:copy-of select="current-group()"/>
    </section>
</xsl:for-each-group>


诀窍在于真正理解以
开头的
组应该是一种模式而不是一种条件。

那么,想要的结果应该是什么呢?缺少它会使问题变得相当不清楚。@DimitreNovatchev我认为从xslt片段可以清楚地看出这一点。不管怎样,还是更新了问题。@DimitreNovatchev好吧,一个简单的答案比争论我的措辞要有用得多。@DimitreNovatchev-有很多更好的方式要求澄清,而不是你是如何做到的。你的编辑也太苛刻了。当你与那些做出合理努力寻求帮助的人交流时,你能不能礼貌一点?@DimitreNovatchev-这里的措辞似乎有一些分歧,因此不清楚你的加入是否会强化这个问题。而且,你这样做是对提问者的侮辱,完全不合适。我们试图帮助人们,而不是赶走他们:那么,想要的结果应该是什么呢?缺少它会使问题变得相当不清楚。@DimitreNovatchev我认为从xslt片段可以清楚地看出这一点。不管怎样,还是更新了问题。@DimitreNovatchev好吧,一个简单的答案比争论我的措辞要有用得多。@DimitreNovatchev-有很多更好的方式要求澄清,而不是你是如何做到的。你的编辑也太苛刻了。当你与那些做出合理努力寻求帮助的人交流时,你能不能礼貌一点?@DimitreNovatchev-这里的措辞似乎有一些分歧,因此不清楚你的加入是否会强化这个问题。而且,你这样做是对提问者的侮辱,完全不合适。我们试图帮助人们,而不是赶走他们: