xsl-list
[Top] [All Lists]

Re: [xsl] preceding-sibling in a group

2021-05-21 02:46:07
Martin's suggestions would both work. Another idea would be 

<xsl:variable name="first-note" 
select="current-group/PART_DES[matches(.,'NOTE')][1]

<xsl:for-each select="current-group()">
                <xsl:choose>
                    <xsl:when test=". is $first-note or . >> $first-note">
                        <note>
...

Michael Kay
Saxonica

On 20 May 2021, at 21:20, rick(_at_)rickquatro(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Here is my sample input:
 
<?xml version="1.0" encoding="UTF-8"?>
<dataroot>
    <DH675AU>
        <FIG_NO>99999</FIG_NO>
        <PART_DES>DIPSTICK</PART_DES>
    </DH675AU>
    <DH675AU>
        <FIG_NO>99999</FIG_NO>
        <PART_DES>O-RING</PART_DES>
    </DH675AU>
    <DH675AU>
        <FIG_NO>99999</FIG_NO>
        <PART_DES>SCREW, SELF-TAPPING</PART_DES>
    </DH675AU>
    <DH675AU>
        <FIG_NO>99999</FIG_NO>
        <PART_DES>NOTE: (1) NOT SERVICED SEPARATELY. INCLUDED IN</PART_DES>
    </DH675AU>
    <DH675AU>
        <FIG_NO>99999</FIG_NO>
        <PART_DES>58F-98-40740 SEAL KIT.</PART_DES>
    </DH675AU>
    <DH675AU>
        <FIG_NO>69500</FIG_NO>
        <PART_DES>DIPSTICK</PART_DES>
    </DH675AU>
    <DH675AU>
        <FIG_NO>69500</FIG_NO>
        <PART_DES>O-RING</PART_DES>
    </DH675AU>
    <DH675AU>
        <FIG_NO>69500</FIG_NO>
        <PART_DES>SCREW, SELF-TAPPING</PART_DES>
    </DH675AU>
</dataroot>
 
Here is my desired output:
 
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <entry>
      <figure>99999</figure>
      <description>DIPSTICK</description>
      <description>O-RING</description>
      <description>SCREW, SELF-TAPPING</description>
      <note>NOTE: (1) NOT SERVICED SEPARATELY. INCLUDED IN</note>
      <note>58F-98-40740 SEAL KIT.</note>
   </entry>
   <entry>
      <figure>69500</figure>
      <description>DIPSTICK</description>
      <description>O-RING</description>
      <description>SCREW, SELF-TAPPING</description>
   </entry>
</root>
 
Here is my stylesheet:
 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform 
<http://www.w3.org/1999/XSL/Transform>"
    xmlns:xs="http://www.w3.org/2001/XMLSchema 
<http://www.w3.org/2001/XMLSchema>"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math 
<http://www.w3.org/2005/xpath-functions/math>"
    exclude-result-prefixes="xs math"
    version="3.0" expand-text="true">
    
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/dataroot">
        <root>
            <xsl:call-template name="make-components"/>
        </root>
    </xsl:template>
    
    <xsl:template name="make-components">
        <xsl:for-each-group select="*" group-by="child::FIG_NO">
            <xsl:call-template name="make-entries"/>
        </xsl:for-each-group>
    </xsl:template>
    
    <xsl:template name="make-entries">
        <entry>
            <figure><xsl:apply-templates 
select="current-group()[1]/FIG_NO"/></figure>
            <xsl:for-each select="current-group()">
                <xsl:choose>
                    <xsl:when test="PART_DES[matches(.,'NOTE')] or 
preceding::PART_DES[matches(.,'NOTE')]">
                        <note>
                            <xsl:apply-templates select="PART_DES"/>
                        </note>
                    </xsl:when>
                    <xsl:otherwise>
                        <description>
                            <xsl:apply-templates select="PART_DES"/>
                        </description>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </entry>
    </xsl:template>
     
</xsl:stylesheet>
 
Here is what I am getting:
 
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <entry>
      <figure>99999</figure>
      <description>DIPSTICK</description>
      <description>O-RING</description>
      <description>SCREW, SELF-TAPPING</description>
      <note>NOTE: (1) NOT SERVICED SEPARATELY. INCLUDED IN</note>
      <note>58F-98-40740 SEAL KIT.</note>
   </entry>
   <entry>
      <figure>69500</figure>
      <note>DIPSTICK</note>
      <note>O-RING</note>
      <note>SCREW, SELF-TAPPING</note>
   </entry>
</root>
 
The idea is that when I see a “NOTE” string, I want that entry and all 
following within the same group to be <note> elements. So I am look for 
preceding-sibling or self that contains “NOTE” but restricted to the same 
group. I am using XSLT 3. Thank you very much.
 
Rick
 
Rick Quatro
Carmen Publishing Inc.
585-729-6746
rick(_at_)frameexpert(_dot_)com <mailto:rick(_at_)frameexpert(_dot_)com>
http://www.frameexpert.com/store <http://www.frameexpert.com/store>
 
 
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/612310> (by 
email <applewebdata://E82B001C-7C55-4AE4-9E27-67B33E3B9491>) 
XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by 
email <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>