[xsl] Creating sequence/range text from position

Subject: [xsl] Creating sequence/range text from position
From: "Charles O'Connor coconnor@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 28 Jan 2023 00:04:14 -0000
Hi all,

We use a word processor-like XML editor that presents users with generated
text for numbered bibliographic references and their in-text citations.
However, downstream systems require actual text to be placed. Question is, how
do we get it?

(Using SaxonEE 10.x)

Given

<article>
    <p>Case 1 is <xref ref-type="bibr" rid="r2"/></p>
    <p>Case 2 is <xref ref-type="bibr" rid="r1 r2 r3 r5 r6 r8 r9 r10"/></p>
    <back>
        <ref-list>
            <ref id="r1">...</ref>
            <ref id="r2">...</ref>
            <ref id="r3">...</ref>
            <ref id="r4">...</ref>
            <ref id="r5">...</ref>
            <ref id="r6">...</ref>
            <ref id="r7">...</ref>
            <ref id="r8">...</ref>
            <ref id="r9">...</ref>
            <ref id="r10">...</ref>
        </ref-list>
    </back>
</article>

We want

<article>
    <p>Case 1 is <xref ref-type="bibr" rid="r2">2</xref></p>
    <p>Case 2 is <xref ref-type="bibr" rid="r1 r2 r3 r5 r6 r8 r9
r10">1-3,5,6,8-10</xref></p>
    <back>
        <ref-list>
            <ref id="r1">...</ref>
            <ref id="r2">...</ref>
            <ref id="r3">...</ref>
            <ref id="r4">...</ref>
            <ref id="r5">...</ref>
            <ref id="r6">...</ref>
            <ref id="r7">...</ref>
            <ref id="r8">...</ref>
            <ref id="r9">...</ref>
            <ref id="r10">...</ref>
        </ref-list>
    </back>
</article>

(only the xrefs have changed)

Case 1 is pretty easily handled with:

    <xsl:template match="xref[@ref-type='bibr']">
        <xsl:variable name="rid" select="@rid" />
        <xsl:copy>
            <xsl:copy-of select="@*" />
            <xsl:value-of
select="count(/article/back/ref-list/ref[@id=$rid]/preceding-sibling::ref) +
1" />
        </xsl:copy>
    </xsl:template>

But Case 2 is not even close. I tried adapting the solution here, to no avail:
https://stackoverflow.com/questions/47559712/xslt-sequence-of-numbers-to-rang
e

Whatever I did, I just got the first number and none of the rest. The extra
twist here is that numbers should only be expressed as a range when 3 or more
occur sequentially.

Thanks,
Charles

Charles O'Connor l Lead Product Manager
Pronouns: He/Him
Aries Systems Corporation l www.ariessys.com
50 High Street, Suite 21 l North Andover, MA l 01845 l USA  


Main: +1 (978) 975-7570
Cell: +1 (802) 585-5655

       

Current Thread