XSL to find all nodes between nodes

I have a large poorly formed XML file in which the information associated with one position is divided into several lines of information, which I am trying to group with the parent position (ITEM_ID). The information is consistent, so the key is an ITEM_ID node, but I cannot create the proper XSL needed to group the information associated with the element (ITEM_ID), given the following XML source (Updated to include the newly discovered grandson element in the XML source):

<LINE_INFO>
    <ITEM_ID>some_part_num</ITEM_ID>
    <DESC>some_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
</LINE_INFO>
<LINE_INFO>
    <EXT_DESC>more_description_for_some_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
    <ITEM_ID>some_other_part_num</ITEM_ID>
    <DESC>some_other_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
</LINE_INFO>
<LINE_INFO>
    <EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
    <LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
</LINE_INFO>
<LINE_INFO>
    <ADDTL_NOTE_DETAIL>
        <NOTE>This is the grandchild note that sometimes appears in my data</NOTE>
    </ADDTL_NOTE_DETAIL>
</LINE_INFO>
<LINE_INFO>
    <ITEM_ID>yet_another_part_num</ITEM_ID>
    <DESC>yet_another_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
</LINE_INFO>
  ...

Required Conclusion:

<LINE_INFO>
    <ITEM_ID>some_part_num</ITEM_ID>
    <DESC>some_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
    <EXT_DESC>more_description_for_some_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
    <ITEM_ID>some_other_part_num</ITEM_ID>
    <DESC>some_other_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
    <EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
    <LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
    <NOTE>This is the grandchild note that sometimes appears in my data</NOTE>
</LINE_INFO>
<LINE_INFO>
    <ITEM_ID>yet_another_part_num</ITEM_ID>
    <DESC>yet_another_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
</LINE_INFO>
+3
source share
3 answers

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kFollowing" match="LINE_INFO[not(ITEM_ID)]"
 use="generate-id(preceding-sibling::LINE_INFO[ITEM_ID][1])"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="LINE_INFO[ITEM_ID]">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>

   <xsl:apply-templates select="key('kFollowing', generate-id())/node()"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="LINE_INFO[not(ITEM_ID)]"/>
</xsl:stylesheet>

XML- ( , ):

<t>
    <LINE_INFO>
        <ITEM_ID>some_part_num</ITEM_ID>
        <DESC>some_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
    </LINE_INFO>
    <LINE_INFO>
        <EXT_DESC>more_description_for_some_part_num</EXT_DESC>
    </LINE_INFO>
    <LINE_INFO>
        <ITEM_ID>some_other_part_num</ITEM_ID>
        <DESC>some_other_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
    </LINE_INFO>
    <LINE_INFO>
        <EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
    </LINE_INFO>
    <LINE_INFO>
        <LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
    </LINE_INFO>
    <LINE_INFO>
        <ITEM_ID>yet_another_part_num</ITEM_ID>
        <DESC>yet_another_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
    </LINE_INFO>
</t>

, :

<t>
    <LINE_INFO>
        <ITEM_ID>some_part_num</ITEM_ID>
        <DESC>some_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
        <EXT_DESC>more_description_for_some_part_num</EXT_DESC>
    </LINE_INFO>
    <LINE_INFO>
        <ITEM_ID>some_other_part_num</ITEM_ID>
        <DESC>some_other_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
        <EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
        <LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
    </LINE_INFO>
    <LINE_INFO>
        <ITEM_ID>yet_another_part_num</ITEM_ID>
        <DESC>yet_another_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
    </LINE_INFO>
</t>

. LINE_INFO, ITEM_ID LINE_INFO node ITEM_ID .

+2

. , XSLT 2.0 1.0.

2.0, <xsl:for-each-group>:

<table>
   <xsl:for-each-group select="LINE_INFO" group-starting-with="LINE_INFO[ITEM_ID]">

XPath select group-starting-with , node LINE_INFO. // , .

, :

      <tr>
         <td><xsl:value-of select="current-group()/ITEM_ID" /></td>
         <td>
           <xsl:value-of "concat(current-group()/DESC, current-group()/EXT_DESC)"/>
           <br />
           <xsl:value-of "concat(current-group()/LINE_NOTE)" />
           <br />
           <xsl:value-of "concat(current-group()/NOTE)" />
         </td>
         <td><xsl:value-of select="current-group()/QTY" /></td>
         <td><xsl:value-of select="current-group()/ADDTL_NOTE_DETAIL/NOTE" /></td>
      </tr>
   </xsl:for-each-group>
</table>

( , OP XSLT 2.0.)

1.0, Muenchian grouping. - ( 1) ,

<xsl:key name="LINE_INFO-by-section" match="LINE_INFO"
    use="generate-id((. | preceding-sibling::LINE_INFO)[ITEM_ID][last()])" />

:

<xsl:for-each select="LINE_INFO[ITEM_ID]">
   <xsl:copy>

:

      <xsl:variable name="section-starter-id" select="generate-id(.)" />
      <xsl:for-each select="key('LINE_INFO-by-section', $section-starter-id))">
         <xsl:copy-of select="node()|@*" />
      </xsl:for-each>
   </xsl:copy>
</xsl:for-each>

(.)

+1

XSLT 2.0:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="root">
        <xsl:for-each-group select="LINE_INFO"
                            group-starting-with="LINE_INFO[ITEM_ID]">
            <xsl:copy>
                <xsl:apply-templates select="current-group()/node()"/>
            </xsl:copy>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

:

<root>
    <LINE_INFO>
        <ITEM_ID>some_part_num</ITEM_ID>
        <DESC>some_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
    </LINE_INFO>
    <LINE_INFO>
        <EXT_DESC>more_description_for_some_part_num</EXT_DESC>
    </LINE_INFO>
    <LINE_INFO>
        <ITEM_ID>some_other_part_num</ITEM_ID>
        <DESC>some_other_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
    </LINE_INFO>
    <LINE_INFO>
        <EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
    </LINE_INFO>
    <LINE_INFO>
        <LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
    </LINE_INFO>
    <LINE_INFO>
        <ITEM_ID>yet_another_part_num</ITEM_ID>
        <DESC>yet_another_part_num_description</DESC>
        <QTY>nn</QTY>
        <UNIT>uom</UNIT>
    </LINE_INFO>
</root>

:

<LINE_INFO>
    <ITEM_ID>some_part_num</ITEM_ID>
    <DESC>some_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
    <EXT_DESC>more_description_for_some_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
    <ITEM_ID>some_other_part_num</ITEM_ID>
    <DESC>some_other_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
    <EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
    <LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
</LINE_INFO>
<LINE_INFO>
    <ITEM_ID>yet_another_part_num</ITEM_ID>
    <DESC>yet_another_part_num_description</DESC>
    <QTY>nn</QTY>
    <UNIT>uom</UNIT>
</LINE_INFO>
+1

Source: https://habr.com/ru/post/1771779/


All Articles