2011-01-13 16 views
14

Ho problemi con xsl:variable. Voglio creare una variabile con un valore che dipende dal valore di un altro attributo del nodo XML. Questo funziona bene. Ma quando provo a creare una variabile con un valore stringa che rappresenta XPath, non funziona quando cerco di usarlo come XPath in un tag XSL successivo.xsl: variabile come valore xpath per l'altro tag xsl

<xsl:variable name="test"> 
    <xsl:choose> 
    <xsl:when test="node/@attribute=0">string/represent/xpath/1</xsl:when> 
    <xsl:otherwise>string/represent/xpath/2</xsl:otherwise> 
    </xsl:choose>  
</xsl:variable>     
<xsl:for-each select="$test"> 
    [...] 
</xsl:for-each> 

ho provato: How to use xsl variable in xsl if e trouble with xsl:for-each selection using xsl:variable. Ma senza risultati.

risposta

10

Se coloro percorso sono noti in anticipo come questo caso, allora si può usare:

<xsl:variable name="vCondition" select="node/@attribute = 0"/> 
<xsl:variable name="test" select="actual/path[$vCondition] | 
            other/actual/path[not($vCondition)]"/> 
+0

grazie. questo non è esattamente quello che stavo chiedendo, ma esattamente quello di cui avevo bisogno) –

+0

@igor milla: prego. –

10

valutazione dinamica di un'espressione XPath non viene supportata in XSLT (sia 1.0 e 2.0), tuttavia:

Possiamo implementare un piuttosto generale valutatore XPath dinamico se ci limitiamo solo ogni percorso posizione di essere un elemento nome:

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

<xsl:param name="inputId" select="'param/yyy/value'"/> 

<xsl:variable name="vXpathExpression" 
    select="concat('root/meta/url_params/', $inputId)"/> 

<xsl:template match="/"> 
    <xsl:value-of select="$vXpathExpression"/>: <xsl:text/> 

    <xsl:call-template name="getNodeValue"> 
    <xsl:with-param name="pExpression" 
     select="$vXpathExpression"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="getNodeValue"> 
    <xsl:param name="pExpression"/> 
    <xsl:param name="pCurrentNode" select="."/> 

    <xsl:choose> 
    <xsl:when test="not(contains($pExpression, '/'))"> 
     <xsl:value-of select="$pCurrentNode/*[name()=$pExpression]"/> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:call-template name="getNodeValue"> 
     <xsl:with-param name="pExpression" 
      select="substring-after($pExpression, '/')"/> 
     <xsl:with-param name="pCurrentNode" select= 
     "$pCurrentNode/*[name()=substring-before($pExpression, '/')]"/> 
     </xsl:call-template> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

quando questa trasformazione è applicata in questo documento XML:

<root> 
    <meta> 
    <url_params> 
     <param> 
     <xxx> 
      <value>5</value> 
     </xxx> 
     </param> 
     <param> 
     <yyy> 
      <value>8</value> 
     </yyy> 
     </param> 
    </url_params> 
    </meta> 
</root> 

il voluto, risultato corretto è prodotto:

root/meta/url_params/param/yyy/value: 8 
+0

+1. Sembra divertente. – Flack