2011-01-13 24 views
14

xsl:variable ile ilgili sorunlar yaşıyorum. Başka bir XML düğüm özniteliğinin değerine bağlı bir değere sahip bir değişken oluşturmak istiyorum. Bu iyi çalışıyor. Ancak XPath'ı temsil eden bir dize değeri olan bir değişken oluşturmaya çalıştığımda, daha sonraki bir XSL etiketinde XPath olarak kullanmaya çalıştığımda işe yaramıyor. xsl: diğer xsl etiketi için xpath değeri olarak değişken

<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> 

Denedim: How to use xsl variable in xsl if ve trouble with xsl:for-each selection using xsl:variable. Ama sonuç yok.

cevap

10

, o zaman kullanabilirsiniz:

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

teşekkürler. Bu tam olarak ne istediğimi değil, tam olarak ne gerekiyordu?) –

+0

@igor milla: Rica ederim. –

10

Dinamik değerlendirme genellikle (her ikisi de 1.0 ve 2.0) XSLT'de desteklenmez Ancak:

biz sadece bir unsuru olmaya her yere yolunu kısıtlamak eğer biz oldukça genel dinamik XPath değerlendirici uygulayabilir

isim:

<root> 
    <meta> 
    <url_params> 
     <param> 
     <xxx> 
      <value>5</value> 
     </xxx> 
     </param> 
     <param> 
     <yyy> 
      <value>8</value> 
     </yyy> 
     </param> 
    </url_params> 
    </meta> 
</root> 
: Bu dönüşüm bu XML belgesinin uygulanır

<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> 

istedik doğru sonuç üretilir

: bu yol bu vaka gibi önceden biliniyorsa

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

+1. Eğlenceli görünüyor. – Flack