2012-05-03 13 views
6

Sto provando a fare qualcosa che sembra dovrebbe essere molto semplice, ma non riesco a farlo funzionare, e non riesco a trovare alcun esempio non coinvolgere molte cose irrilevanti. Voglio aggiornare il contenuto del testo di uno specifico tag xml ad un valore specifico (passato come parametro, questo XSLT sarà usato da ant). Un semplice esempio:Aggiornare il testo di un elemento con XSLT in base al parametro

voglio trasformare

<foo> 
    <bar> 
    baz 
    </bar> 
</foo> 

Per

<foo> 
    <bar> 
     something different 
    </bar> 
</foo> 

Questo è il foglio di stile che ho provato, che si traduce in soli tag, nessun testo a tutti

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <!-- identity transformation, to keep everything unchanged except for the stuff we want to change --> 
    <!-- Whenever you match any node or any attribute --> 
    <xsl:template match="node()|@*"> 
     <!-- Copy the current node --> 
     <xsl:copy> 
      <!-- Including any attributes it has and any child nodes --> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <!-- change the text of the bar node, in the real template the value won't be specified inline --> 
    <xsl:template match="/foo/bar/"> 
     <xsl:param name="baz" value="something different"/> 
      <xsl:value-of select="$baz"/> 
    </xsl:template> 
</xsl:stylesheet> 

Grazie in anticipo!

risposta

10

Ci sono una serie di problemi con il codice fornito che ha portato alla compilazione in tempo errori:

<xsl:template match="/foo/bar/"> 
    <xsl:param name="baz" value="something different"/> 
     <xsl:value-of select="$baz"/> 
</xsl:template> 
  1. Il modello partita specificato su questo modello è sintatticamente illegale - un'espressione XPath non può terminare con il carattere /.

  2. xsl:param non possono avere un attributo sconosciuto come value

Soluzione:

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

<xsl:param name="pReplacement" select="'Something Different'"/> 

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

<xsl:template match="foo/bar/text()"> 
    <xsl:value-of select="$pReplacement"/> 
</xsl:template> 
</xsl:stylesheet> 

Quando questa trasformazione è applicato sul documento XML fornito:

<foo> 
    <bar> 
    baz 
    </bar> 
</foo> 

la desiderata, risultato corretto è prodotta:

<foo> 
    <bar>Something Different</bar> 
</foo> 
0
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
<xsl:output indent="yes" encoding="UTF-8" method="xml"/> 
<xsl:param name="param-from-ant" as="xs:string"/> 

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

<xsl:template match="/foo/bar"> 
<xsl:value-of select="$param-from-ant"/> 
</xsl:template> 
</xsl:stylesheet> 
+0

ho provato (sia nel mio esempio leggermente più complessa, e [ l'esempio di base] (http://www.xsltcake.com/slices/Y8fojh/4)), ma non sembra funzionare? Sto ottenendo un tag foo vuoto. Ho copiato qualcosa di sbagliato? – user1126518

+0

Sean, il "codice XSLT" fornito causa errori di compilazione con qualsiasi processore XSLT 1.0 conforme. –

0

Un approccio leggermente diverso:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:param name="replace"/> 

    <xsl:template match="node()|@*"> 
    <xsl:choose> 
     <xsl:when test="text() and name(..)='bar' and name(../..)='foo'"> 
     <xsl:value-of select="$replace"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

Problemi correlati