Yvon Desjardins
Yvon Desjardins

Reputation: 1

How to add multiple page number in a xsl tag

I want to add a page number when there is more than one page in a xsl tag.

<Page Lien="../pages/Q_MLMQ_20220604_00000_N_002_003Y.pdf">02</Page>

In this example the page numbers are 02, and 003 (but i want to delete the 0).

If at the end there is a Y.pdf, i need the page number before the Y.pdf

<Page>2,3</Page>

If it's N.pdf, i do not need the page number before the N.pdf since it's only one page

<Page Lien="../pages/Q_MLMQ_20220604_00000_N_017_017N.pdf">17</Page>
<Page>17</Page>

Here's how i did it (but i only get page 2)

<xsl:if test="@Lien">

<xsl:element namespace="http://www.cedrom-sni.com/schema/newsv1.xsd" name="page">

<xsl:value-of select="format-number(..//Page, '#')"/>

</xsl:element>

</xsl:if>

I get

<Page>2</Page>

Thank you

Upvotes: 0

Views: 65

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117102

Consider the following minimal example:

XML

<Pages>
    <Page Lien="../pages/Q_MLMQ_20220604_00000_N_002_003Y.pdf">02</Page>
    <Page Lien="../pages/Q_MLMQ_20220604_00000_N_017_017N.pdf">17</Page>
</Pages>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Page">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="contains(@Lien, 'Y.pdf')">
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-before(substring-after(@Lien, '_N_'), 'Y.pdf')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains(@Lien, 'N.pdf')">
                <xsl:value-of select="number(substring-before(substring-after(@Lien, '_N_'), '_'))" />
            </xsl:when>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'_'"/>
    <xsl:value-of  select="number(substring-before(concat($text, $delimiter), $delimiter))" />
    <xsl:if test="contains($text, $delimiter)">
        <xsl:text>,</xsl:text>
        <!-- recursive call -->
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<Pages>
  <Page>2,3</Page>
  <Page>17</Page>
</Pages>

Upvotes: 1

Related Questions