简体   繁体   中英

XSLT1 fo replacing placeholders

I have to replace some placeholder like $$RUOLO^ with a xpath, this is the result xsl:fo:

 <fo:block>e per il ruolo $$RUOLO^ , funzione <xsl:value-of xmlns:xsl="http://www.w3.org/1999/XSL/Transform" select="/root/ddx[@id='rsSelect']/r/FUNZIONE_BREVE"/> da lei ricoperta per la specifica informazione con la seguente motivazione $$DESCRIZIONE_ISCRIZIONE^ .</fo:block> 

I have replaced $$FUNZIONE^ with select="/root/ddx[@id='rsSelect']/r/FUNZIONE_BREVE"/> but this dosent happen for RUOLO. Why not?

This is like I do:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo fn xsl XWC" 
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:XWC="http://www.netbureau.it/XWC/2.0">

<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" indent="yes"/>

<!-- non toccare -->
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

<!-- fine non toccare -->

<!-- match templates per sostituzione variabili-->

    <xsl:template match="fo:block[contains(text(),'$$EVENTO^')] | fo:inline[contains(text(),'$$EVENTO^')]">
        <xsl:call-template  name="eventotmp"/>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$DECORRENZA^')] | fo:inline[contains(text(),'$$DECORRENZA^')]">
        <xsl:call-template  name="decorrenzatmp">
            <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$DESCRIZIONE_ISCRIZIONE^')] | fo:inline[contains(text(),'$$DESCRIZIONE_ISCRIZIONE^')]">
        <xsl:call-template  name="descrizionetmp">
            <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$TIPOLOGIA_INFORMAZIONE_PRIVILEGIATA^')] | fo:inline[contains(text(),'$$TIPOLOGIA_INFORMAZIONE_PRIVILEGIATA^')]">
        <xsl:call-template  name="tipologiatmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$RUOLO^')] | fo:inline[contains(text(),'$$RUOLO^')]">
        <xsl:call-template  name="ruolotmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$FUNZIONE^')] | fo:inline[contains(text(),'$$FUNZIONE^')]">
        <xsl:call-template  name="funzionetmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$NR_ARTICOLO^')] | fo:inline[contains(text(),'$$NR_ARTICOLO^')]">
        <xsl:call-template  name="nrarticolotmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$CODICE_INFORMAZIONE^')] | fo:inline[contains(text(),'$$CODICE_INFORMAZIONE^')]">
        <xsl:call-template  name="codinfotmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$CIP^')] | fo:inline[contains(text(),'$$CIP^')]">
        <xsl:call-template  name="ciptmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$PROTOCOLLO^')] | fo:inline[contains(text(),'$$PROTOCOLLO^')]">
        <xsl:call-template  name="protocollotmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$EMITTENTE^')] | fo:inline[contains(text(),'$$EMITTENTE^')]">
        <xsl:call-template  name="emittentetmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="fo:block[contains(text(),'$$AREA_COMPETENZA^')] | fo:inline[contains(text(),'$$AREA_COMPETENZA^')]">
        <xsl:call-template  name="areacompetenzatmp">
        <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>




<!-- end match templates -->
<!--  non toccare -->

    <!--  match/copia tutti i commenti  -->
    <xsl:template match='comment()'>
     <xsl:comment><xsl:value-of select="."/></xsl:comment>
    </xsl:template>

    <!--  match/copia di eventuali istruzioni <?elementostart> ecc : ovvero eventuali processing istructions -->
    <xsl:template match='processing-instruction()'>
     <xsl:processing-instruction name="{name()}">
      <xsl:value-of select="."/>
     </xsl:processing-instruction>
    </xsl:template>


    <!--  funzione identità : match/copia di tutti tag che contengono testo e non, compresi i CDATA :
     "*" seleziona solo i tag,   
     node() seleziona tutti i nodi testo, non testo ed i cdata, 
     @* seleziona tutti gli attributi, 
     | => OR
     i commenti non vengono copiati, vedere template comment()

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

<!-- end non toccare -->    


<!-- definizione templates per sostituzione variabili -->
    <xsl:template name="eventotmp">
    <xsl:element name="fo:block">
            <xsl:element name="xsl:value-of">
                <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/TITOLO</xsl:attribute>
            </xsl:element>
    <!--        <xsl:value-of select="/root/ddx[@id='rsSelect']/r/TITOLO"/>  -->
    </xsl:element>
    </xsl:template>

    <xsl:template name="decorrenzatmp">
    <xsl:param name="text"/>
        <xsl:element name="fo:block">
            <xsl:value-of select="substring-before($text,'$$DECORRENZA^')"/>
            <xsl:element name="xsl:value-of">
                <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/DATA_ISCRIZIONE_ESTESA</xsl:attribute>
            </xsl:element>
            <xsl:value-of select="substring-after($text,'$$DECORRENZA^')"/>
        </xsl:element>
    </xsl:template>



    <xsl:template name="descrizionetmp">
    <xsl:param name="text"/>
        <xsl:element name="fo:block">
<!--        <xsl:text>RAGIONE: </xsl:text> -->
            <xsl:value-of select="substring-before($text,'$$DESCRIZIONE_ISCRIZIONE^')"/>
            <xsl:element name="xsl:value-of">
                <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/DESCRIZIONE_ISCRIZIONE</xsl:attribute>
            </xsl:element>
            <xsl:value-of select="substring-after($text,'$$DESCRIZIONE_ISCRIZIONE^')"/>
<!--        <xsl:text>RAGIONE: </xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/DESCRIZIONE_ISCRIZIONE"/> -->
        </xsl:element>
    </xsl:template>



    <xsl:template name="tipologiatmp">
    <xsl:param name="text"/>
        <xsl:element name="fo:block">
<!--        <xsl:text>TIPOLOGIA: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$TIPOLOGIA_INFORMAZIONE_PRIVILEGIATA^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/TIPO_INFORMAZIONE_PRIVILEGIATA</xsl:attribute>
        </xsl:element>
<!--        <xsl:text>TIPOLOGIA: </xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/TIPO_INFORMAZIONE_PRIVILEGIATA"/> -->
        <xsl:value-of select="substring-after($text,'$$TIPOLOGIA_INFORMAZIONE_PRIVILEGIATA^')"/>
        </xsl:element>
    </xsl:template>

    <xsl:template name="ruolotmp">
    <xsl:param name="text"/>
        <xsl:element name="fo:block">
<!--        <xsl:text>RUOLO: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$RUOLO^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/RUOLO_BREVE</xsl:attribute>
        </xsl:element>
        <xsl:value-of select="substring-after($text,'$$RUOLO^')"/>
<!--        <xsl:text>RUOLO: </xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/RUOLO_BREVE"/> -->
        </xsl:element>
    </xsl:template>



    <xsl:template name="funzionetmp">
    <xsl:param name="text"/>
    <xsl:element name="fo:block">
<!--        <xsl:text>FUNZIONE: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$FUNZIONE^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/FUNZIONE_BREVE</xsl:attribute>
        </xsl:element>
<!--        <xsl:text>FUNZIONE: </xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/FUNZIONE_BREVE"/> -->
        <xsl:value-of select="substring-after($text,'$$FUNZIONE^')"/>
    </xsl:element>
    </xsl:template>




    <xsl:template name="nrarticolotmp">
    <xsl:param name="text"/>
    <xsl:element name="fo:block">
<!--        <xsl:text>NR. ARTICOLO: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$NR_ARTICOLO^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/NR_ARTICOLO</xsl:attribute>
        </xsl:element>
<!--        <xsl:text>NR. ARTICOLO: </xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/NR_ARTICOLO"/> -->
        <xsl:value-of select="substring-after($text,'$$NR_ARTICOLO^')"/>
    </xsl:element>
    </xsl:template>

    <xsl:template name="codinfotmp">
    <xsl:param name="text"/>
    <xsl:element name="fo:block">
<!--        <xsl:text>CODICE INFORMAZIONE: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$CODICE_INFORMAZIONE^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/CODICE_INFORMAZIONE</xsl:attribute>
        </xsl:element>
        <xsl:value-of select="substring-after($text,'$$CODICE_INFORMAZIONE^')"/>
<!--        <xsl:text>CODICE INFORMAZIONE: </xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/CODICE_INFORMAZIONE"/> -->
    </xsl:element>
    </xsl:template>

    <xsl:template name="ciptmp">
    <xsl:param name="text"/>
    <xsl:element name="fo:block">
<!--        <xsl:text>CODICE CIP: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$CIP^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/CODICE_PRATICA</xsl:attribute>
        </xsl:element>
        <xsl:value-of select="substring-after($text,'$$CIP^')"/>
<!--        <xsl:text>CIP:</xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/CODICE_PRATICA"/> -->
    </xsl:element>
    </xsl:template>

    <xsl:template name="protocollotmp">
    <xsl:param name="text"/>
    <xsl:element name="fo:block">
<!--        <xsl:text>PROTOCOLLO: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$PROTOCOLLO^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/PROTOCOLLO</xsl:attribute>
        </xsl:element>
        <xsl:value-of select="substring-after($text,'$$PROTOCOLLO^')"/>
<!--        <xsl:text>PROTOCOLLO: </xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/PROTOCOLLO"/> -->
    </xsl:element>
    </xsl:template>

    <xsl:template name="emittentetmp">
    <xsl:param name="text"/>
    <xsl:element name="fo:block">
<!--    <xsl:text>EMITTENTE: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$EMITTENTE^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/EMITTENTE_ESTESA</xsl:attribute>
        </xsl:element>

        <xsl:value-of select="substring-after($text,'$$EMITTENTE^')"/>
    </xsl:element>
    </xsl:template>

    <xsl:template name="areacompetenzatmp">
    <xsl:param name="text"/>
    <xsl:element name="fo:block">
<!--    <xsl:text>EMITTENTE: </xsl:text> -->
        <xsl:value-of select="substring-before($text,'$$AREA_COMPETENZA^')"/>
        <xsl:element name="xsl:value-of">
            <xsl:attribute name="select">/root/ddx[@id='rsSelect']/r/AREA_COMPETENZA</xsl:attribute>
        </xsl:element>
<!--        <xsl:text>EMITTENTE: </xsl:text><xsl:value-of select="/root/ddx[@id='rsSelect']/r/EMITTENTE_ESTESA"/> -->
        <xsl:value-of select="substring-after($text,'$$AREA_COMPETENZA^')"/>
    </xsl:element>
    </xsl:template>


</xsl:stylesheet>

I use this template only, like the last of chain substitutions (pipeline). Why doens't get the same expected for RUOLO and DESCRIZIONE_ISCRIZIONE?

Thanks

Roby


Thanks @JLRishe It works for replacing but, it doesn't consider the so all text appear in the same line without CARRIAGE RETURN preservation.. XSL FO Input look likes:

 <fo:page-sequence master-reference="all-pages"><fo:static-content flow-name="page-header"><fo:block font-size="small" text-align="center" space-before="0.5in" space-before.conditionality="retain"/></fo:static-content><fo:static-content flow-name="page-footer"><fo:block font-size="small" text-align="center" space-after="0.5in" space-after.conditionality="retain">- <fo:page-number/> -</fo:block></fo:static-content><fo:flow flow-name="xsl-region-body"><fo:block role="html:body">
  <fo:block space-before="1em" space-after="1em" role="html:p">ISCRIZIONE PRIVILEGIATA</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p"><fo:block role="html:br"/>Comunicazione relativa all'Elenco dele persone che hanno accesso alle informazioni privilegiate istituito ai sensi del</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">$$EVENTO^</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">Con la presente Le comunichiamo che in data $$DECORRENZA^ è stato iscritto nel ..) (“MAR”) e dalla relativa disciplina di attuazione contenuta nel Regolamento di esecuzione .. – con riferimento a:</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p"> </fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">Tipologia informazione privilegiata $$TIPOLOGIA_INFORMAZIONE_PRIVILEGIATA^ e per il ruolo $$RUOLO^, funzione $$FUNZIONE^ da lei ricoperta per la specifica informazione privilegiata con la seguente motivazione $$DESCRIZIONE_ISCRIZIONE^.</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">Si chiede di far preciso riferimento alla “Procedura per la gestione del Registro delle Persone che hanno accesso a Informazioni Privilegiate” (la “Procedura”) che risulta in vigore che risulta consultabile sul sito internet della Società al seguente indirizzo</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p"> </fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">Per la nozione di informazione privilegiata si rinvia all’articolo 7 MAR riprodotto in allegato alla presente, nonché alla procedura denominata “Procedura per la gestione interna e per la comunicazione al pubblico di Informazioni xxx” adottata dalla Società che parimenti risulta consultabile sul sito internet all’indirizzo sopra indicato.</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p"> </fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">$$EMITTENTE^</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">$$CIP^</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">$$PROTOCOLLO^</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">$$AREA_COMPETENZA^</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">Per presa visione e ritiro di copia</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">Data ...........................................</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p">Firma .....................................................</fo:block> 
  <fo:block space-before="1em" space-after="1em" role="html:p"> </fo:block>
 </fo:block></fo:flow></fo:page-sequence>
</fo:root>

The problem here is that when applying templates to nodes, a single node can only be matched by one template. Since your template for FUNZIONE has a higher precedence than the one for RUOLO or DESCRIZIONE_ISCRIZIONE (because it appears later than them), that is the one that is being used.

The solution is to use a recursive approach that progressively replaces placeholders until there are no more to replace. The following does this and also eliminates a lot of the duplication in your XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo vr"
                xmlns:vr="my-variables"
>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

  <vr:substitutions>
    <substitution key="$$FUNZIONE^" path="/root/ddx[@id='rsSelect']/r/FUNZIONE_BREVE" />
    <substitution key="$$RUOLO^" path="/root/ddx[@id='rsSelect']/r/RUOLO_BREVE" />
    <substitution key="$$CODICE_INFORMAZIONE^" path="/root/ddx[@id='rsSelect']/r/CODICE_INFORMAZIONE" />
    <substitution key="$$DESCRIZIONE_ISCRIZIONE^" path="/root/ddx[@id='rsSelect']/r/DESCRIZIONE_ISCRIZIONE" />
  </vr:substitutions>

  <xsl:variable name="substitutions" select="document('')/*/vr:substitutions/substitution" />

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

  <xsl:template match="fo:block[contains(., '$$')] | fo:inline[contains(., '$$')]">
    <fo:block>
      <xsl:call-template name="PerformSubstitutions">
        <xsl:with-param name="text" select="." />
      </xsl:call-template>
    </fo:block>
  </xsl:template>

  <xsl:template name="PerformSubstitutions">
    <xsl:param name="text" />
    <xsl:variable name="substitution" select="$substitutions[contains($text, @key)]" />

    <xsl:choose>
      <xsl:when test="$substitution">
        <!-- Recursively call PerformSubstitutions on portion before placeholder-->
        <xsl:call-template name="PerformSubstitutions">
          <xsl:with-param name="text" select="substring-before($text, $substitution/@key)" />
        </xsl:call-template>
        <!-- Insert xsl:value-of -->
        <xsl:element name="xsl:value-of">
          <xsl:attribute name="select">
            <xsl:value-of select="$substitution/@path"/>
          </xsl:attribute>
        </xsl:element>
        <!-- Recursively call PerformSubstitutions on portion after placeholder-->
        <xsl:call-template name="PerformSubstitutions">
          <xsl:with-param name="text" select="substring-after($text, $substitution/@key)" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <!-- No substitutions found. Just output the text -->
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

You can handle as many placeholders as you need by adding more elements to the <vr:substitutions> section.

When run on this input XML:

<n xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <fo:inline>Some text $$FUNZIONE^ and $$RUOLO^ other text $$DESCRIZIONE_ISCRIZIONE^ </fo:inline>
  <fo:block>Some more text $$CODICE_INFORMAZIONE^ yay.</fo:block>
</n>

The output is:

<n xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <fo:block>Some text <xsl:value-of select="/root/ddx[@id='rsSelect']/r/FUNZIONE_BREVE" /> and <xsl:value-of select="/root/ddx[@id='rsSelect']/r/RUOLO_BREVE" /> other text <xsl:value-of select="/root/ddx[@id='rsSelect']/r/DESCRIZIONE_ISCRIZIONE" /> </fo:block>
  <fo:block>Some more text <xsl:value-of select="/root/ddx[@id='rsSelect']/r/CODICE_INFORMAZIONE" /> yay.</fo:block>
</n>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM