Template match result to variable?

GregWait42
Posts: 8
Joined: Wed Feb 19, 2020 10:52 pm

Template match result to variable?

Post by GregWait42 » Wed Mar 10, 2021 1:26 am

Within a dita to text plugin, I'm splitting up a lenghty element using

Code: Select all

<?linebreak?>
in the dita docs. The plugin then uses

Code: Select all

<xsl:template name="lbremain" match="processing-instruction()[name() = 'linebreak']">
to break it up at those processing instructions.

What I'd like to do (and have so far failed to find a way to do) is take each result from the template "matching" and populate a variable with the result. My plugin spits out the separate "pieces", but I don't know how to take that and put it in a variable. The ultimate goal is to be able to get the string-length of each segment and determine when one is long enough that I need to force a wrap and create an indent for the wrapped segment.

It seems like this should be simple, but my many attempts have yielded an empty variable every time. Any help would be appreciated. - G

Radu
Posts: 7431
Joined: Fri Jul 09, 2004 5:18 pm

Re: Template match result to variable?

Post by Radu » Wed Mar 10, 2021 10:27 am

Hi Greg,

When you match on the processing instruction, you can only control the output underneath it, you cannot control the output of text fragments before and after it.
What you probably need to do is to match the element which contains the processing instruction and in this way have access to the entire set of text nodes and processing instructions in the element.
Something like:

Code: Select all

    <!-- Match any XML element which has a linebreak PI as a direct child-->
    <xsl:template match="*[processing-instruction()[name() = 'linebreak']]">
        <!-- Copy element as it is -->
        <xsl:copy>
            <!-- Copy attributes as they are -->
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select="node()">
                <xsl:if test="name() = 'linebreak'">
                    <!-- This is a processing instruction, you can use the preceding-sibling:: to look at the text before it for example-->
                </xsl:if>
                <xsl:apply-templates select="."/>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply