2011-11-14 16 views
8

Sto tentando di dichiarare una variabile che ha un valore predefinito o se un valore è presente in un set ripetuto per utilizzare un nuovo valore diverso.Valore variabile predefinito XSLT se il valore non è presente

Questo è quello che ho finora.

 <xsl:variable name="lsind"> 
     <xsl:value-of select="'N'"/> 

     <xsl:for-each select='./Plan/InvestmentStrategy/FundSplit'> 
      <xsl:choose> 
      <xsl:when test="contains(./@FundName, 'Lifestyle')"> 
       <xsl:value-of select="'Y'"/> 
      </xsl:when> 
      </xsl:choose> 
     </xsl:for-each> 
     </xsl:variable> 

Quello che voglio è che se tutte le istanze di ./Plan/InvestmentStrategy/FundSplit/@FundName 'contiene' Stile poi lsind 'Y' in caso contrario passa al valore predefinito di 'N'.

Lo sto facendo in questo modo, come se io usassi "altrimenti l'ultima ricorrenza potrebbe potenzialmente tornare a N?

Qualche suggerimento?

risposta

12
<xsl:variable name="lsind"> 
    <xsl:choose> 
    <xsl:when test="Plan/InvestmentStrategy/FundSplit[contains(@FundName, 'Lifestyle')]"> 
     <xsl:text>Y</xsl:text> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:text>N</xsl:text> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:variable> 

dovrebbe essere sufficiente

+1

Sei un bellissimo essere umano. Grazie! Stavo solo iniziando a esplorare la rotta xpath sospettando che ci fosse un modo. –

4

Questo può essere specificato in un'unica espressione XPath (anche in XPath 1.0):

<xsl:variable name="vLsind" select= 
"substring('YN', 
      2 - boolean(plan/InvestmentStrategy/FundSplit[@FundName='Lifestyle']), 
      1)"/> 

Esempio 1:

<plan> 
<InvestmentStrategy> 
    <FundSplit FundName="Lifestyle"/> 
</InvestmentStrategy> 
</plan> 

Trasformazione:

<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:variable name="vLsind" select= 
"substring('YN', 
      2 - boolean(plan/InvestmentStrategy/FundSplit[@FundName='Lifestyle']), 
      1)"/> 

<xsl:template match="/"> 
    <xsl:value-of select="$vLsind"/> 
</xsl:template> 
</xsl:stylesheet> 

Risultato:

Y 

Esempio 2:

<plan> 
<InvestmentStrategy> 
    <FundSplit FundName="No Lifestyle"/> 
</InvestmentStrategy> 
</plan> 

Risultato:

N 

Spiegazione:

  1. Per definizione boolean(some-node-set) è true() esattamente quando some-node-set è non vuoto.

  2. Per definizione number(true()) è 1 e number(false()) è 0

  3. 1 e 2 cobined ci dà: number(boolean(some-node-set)) è 1 esattamente quando some-node-set non è vuota, altrimenti è 0.

Altre soluzioni single-espressione:

XPath 1.0:

translate(number(boolean(YourXPathExpression)), '10', 'YN') 

XPath 2.0:

if(YourXPathExpression) 
then 'Y' 
else 'N' 

O anche:

('N', 'Y')[number(boolean(YourXPathExpression)) +1] 
+0

+1 Questi sono alcuni meravigliosi esempi di (mis) utilizzo delle funzioni di XPath 1.0! – Goran

+4

@Goran: questo è stato dichiarato dal dipartimento di polizia? Se * questo * è un uso improprio, allora ho un problema molto più grande ... :) –

+0

@DimitreNovatchev. Non è un uso improprio, ma può essere molto ottuso, perché maschera un costrutto logico all'interno di un'espressione, che potrebbe non essere buono per la manutenzione a lungo termine, poiché la maggior parte, o chiunque li segua, potrebbe aver bisogno di cercare di ottenere la propria testa intorno ad esso ogni volta che lo visitano di nuovo. – Patanjali

Problemi correlati