Home > Enterprise >  How to add multiple page number in a xsl tag
How to add multiple page number in a xsl tag

Time:06-08

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

CodePudding user response:

You could do something like this (only checking for the 'N'-restriction ):

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

    <!-- The pagenumber as text() can always be done -->
    <xsl:value-of select="format-number(., '#')"/>

    <!-- First Store the pagenumber-information in a variable -->
    <xsl:variable name="pagenumberInformation" select="substring(@Lien,string-length(@Lien) - 7,4)"/>

    <!-- Check if it is not ending with a N -->
    <xsl:if test="substring($pagenumberInformation,4)!='N'">

      <!-- Put the extra number behind the comma -->
      <xsl:text>,</xsl:text>
      <xsl:value-of select="format-number(number(substring($pagenumberInformation,1,3)), '#')"/>

    </xsl:if>
 </xsl:element>
  
</xsl:if>

CodePudding user response:

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>
  • Related