xsl-list
[Top] [All Lists]

Re: [xsl] General algorithm for finding nodes between PIs

2017-01-26 14:31:16
On 26.01.2017 20:53, Martin Honnen martin(_dot_)honnen(_at_)gmx(_dot_)de wrote:
On 26.01.2017 20:45, Rick Quatro rick(_at_)rickquatro(_dot_)com wrote:

I have XML with a bunch of different processing instructions in them;
here
is a sample:

<?xml version="1.0" encoding="UTF-8"?>
<info>
    <?Fm Condstart VbV-VCO?>
    <p>For this, use <b>that </b>to do that.</p>
    <?Fm Condend VbV-VCO?>
    <p><?Fm Condstart USB?>If you need this, do that.<?Fm Condend
USB?></p>
</info>


For each "pair" of processing instructions (Condstart and Condend with
the
same string), I want to find all of the nodes between them.

Are the matching pairs always sibling nodes? Or can they be at different
nesting levels?

In case of siblings it sounds like an easy job for for-each-group
group-starting-with="processing-instruction('Fm')"/for-each-group
group-ending-with.


Like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        xmlns:xs="http://www.w3.org/2001/XMLSchema";
        exclude-result-prefixes="xs"
        version="2.0">
        
        <xsl:template match="/">
                <results>
                        <xsl:apply-templates/>
                </results>
        </xsl:template>
        
<xsl:template match="*[processing-instruction('Fm')[starts-with(., 'Condstart')]]"> <xsl:for-each-group select="node()" group-starting-with="processing-instruction('Fm')[starts-with(., 'Condstart')]"> <xsl:if test="self::processing-instruction('Fm')[starts-with(., 'Condstart')]">
                                <group name="{.}">
<xsl:variable name="pi-end-name" select="replace(., 'Condstart', 'Condend')"/> <xsl:for-each-group select="current-group() except ." group-ending-with="processing-instruction('Fm')[. = $pi-end-name]"> <xsl:if test="current-group()[last()][self::processing-instruction('Fm')[. = $pi-end-name]]">
                                                        <xsl:copy-of 
select="current-group()[position() ne last()]"/>
                                                </xsl:if>
                                        </xsl:for-each-group>
                                </group>
                        </xsl:if>
                </xsl:for-each-group>
                <xsl:apply-templates/>
        </xsl:template>
        
        <xsl:template match="text()"/>
        
</xsl:stylesheet>

--~----------------------------------------------------------------
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>