Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    18

    [xsl]Problema con nodi fratelli

    Ciao a tutti,

    Sono sempre alle prese con il codice da applicare al docx.

    Mi trovo in una situazione analoga alla seguente:

    Struttura XML

    codice:
    <document>
      <body>
         
    
    
           <r>
             <t> Fantasia</t>
           </r>       
           <r>        
             <rPr>
               <stile attr="parentesi" />
             </rPr>
             <t> Walt Disney </t>
           </r>
         </p>
       </body>
    </document>
    Voglio in output: Fantasia (Walt Disney)

    Ho scritto:

    codice:
    <xsl:template match="/document">
    Elenco film
    <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="body">
    [animazione]
    <xsl:apply-templates/>
    [/animazione]
    </xsl:template>
    
    <xsl:template match="p">
    <xsl:apply-templates/><xsl:if test="position()!=last()"><xsl:text>
    </xsl:text></xsl:if>
    </xsl:template>
    
    <xsl:template match="rPr">
    <xsl:variable name="par" select="following-sibling::t/text()" />
    <xsl:if test="stile[@attr='parentesi']">
    <xsl:text>(</xsl:text>
    <xsl:value-of select="$par" />
    <xsl:text>)</xsl:text>
    </xsl:if>
    </xsl:template>
    E il risultato è:

    Elenco film
    [animazione]
    Fantasia (Walt Disney) Walt Disney
    [/animazione]

    Come posso fare per evitare la ripetizione fuori parentesi ?

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    18
    Ho provato a sostituire l'if con un choose/when

    codice:
    <xsl:template match="rPr">
    <xsl:choose>
      <xsl:when test="stile[@attr='parentesi']">
        <xsl:text>(</xsl:text>
        <xsl:apply-templates select="following-sibling::t" />
        <xsl:text> )</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates />
        </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    Ma niente da fare, il risultato è sempre lo stesso: il testo in parentesi mi viene duplicato anche fuori parentesi: Fantasia (Walt Disney) Walt Disney

    Qualcuno sa dirmi dove sto sbagliando ?
    Ah, sto usando xsltproc sotto Linux (xsl versione 1.0).

    Grazie

  3. #3
    codice:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    	<xsl:template match="/">
    		<xsl:text>Elenco film &#x0a;</xsl:text>
    		<xsl:text>[animazione] &#x0a;</xsl:text>
    		<xsl:for-each select="document/body/p">
    			<xsl:variable name="produttore">
    				<xsl:if test="r[2]/rPr/stile/@attr = 'parentesi'">
    					<xsl:value-of select="r[2]/t"/>
    				</xsl:if>
    			</xsl:variable>
    			<xsl:choose>
    				<xsl:when test="$produttore != ''">
    					<xsl:value-of select="concat(r[1]/t,'(',$produttore,')')"/>
    				</xsl:when>
    				<xsl:otherwise>
    					<xsl:value-of select="r[1]/t"/>
    				</xsl:otherwise>
    			</xsl:choose>
    		</xsl:for-each>
    		<xsl:text>&#x0a;[/animazione]</xsl:text>
    	</xsl:template>
    
    
    </xsl:stylesheet>
    per andare a capo ho usato & # x0a; (scritto tutto attaccato )
    se vuoi togliere gli a capo basta che togli gli &#x0a;

  4. #4
    Utente di HTML.it
    Registrato dal
    Mar 2008
    Messaggi
    18
    Ciao Mattia,

    Grazie per la risposta. Mi hai spinta ad approfondire meglio predicati e assegnazioni.
    La soluzione era in realtà più semplice. Invece che:

    codice:
    <xsl:template match="rPr">
    <xsl:choose>
      <xsl:when test="stile[@attr='parentesi']">
        <xsl:text>(</xsl:text>
        <xsl:apply-templates select="following-sibling::t" />
        <xsl:text> )</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates />
        </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    Bisogna usare:

    codice:
    <xsl:template match="rPr">
    <xsl:choose>
      <xsl:when test="stile[@attr='parentesi']">
        <xsl:text>(</xsl:text>
        <xsl:value-of select="." />
        <xsl:text> )</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates />
        </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    Ho poi un'altra domanda sui manuali on-line disponibili, ma posto un altro thread apposito.

    Grazie per l'attenzione.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.