2013-10-08 9 views
11

मैं पहले आवंटित एक चर के लिए एक मूल्य को फिर से कैसे सौंप सकता हूं? मैं इसे इस तरह काम करता है की जरूरत है:एक्सएसएलटी में एक चर बदलने या पुन: असाइन कैसे करें?

<xsl:variable name="variable2" select="'N'" /> 
.... 
<xsl:when test="@tip = '2' and $variable2 != 'Y'">             
    <xsl:variable name="variable2" select="'Y'" /> 
</xsl:when> 

उत्तर

10

एक्सएसएलटी में चर को केवल एक बार मूल्य दिया जा सकता है। यह डिजाइन द्वारा किया जाता है। सामान्य रूप से प्रेरणा की सराहना के लिए Why Functional languages? देखें।

एक चर को पुन: असाइन करने के बजाय, सीधे इनपुट दस्तावेज़ के खिलाफ सशर्त लिखें, या अलग-अलग स्थानीय पैरामीटर के साथ एक फ़ंक्शन (या नामित टेम्पलेट) को दोबारा कॉल करें।

आपको जो भी करने की ज़रूरत है उसे एक ऐसे दृष्टिकोण से किया जा सकता है जिसके लिए चर के पुनर्मूल्यांकन की आवश्यकता नहीं है। अधिक विशिष्ट उत्तर प्राप्त करने के लिए, एक और विशिष्ट प्रश्न प्रदान करें।

यह भी देखें:

1

आप नहीं कर सकते हैं - XSLT में 'चर' वास्तव में अधिक अन्य भाषाओं में स्थिरांक की तरह होते हैं, वे मूल्य नहीं बदल सकते।

+0

मुझे क्या करने की ज़रूरत है? धन्यवाद –

5

बस एकाधिक चर का उपयोग करें। यहाँ अपने उदाहरण काम करने के लिए बना दिया है ...

<xsl:variable name="variable1" select="'N'" /> 
    .... 
    <xsl:variable name="variable2"> 
     <xsl:choose> 
      <xsl:when test="@tip = '2' and $variable1 != 'Y'"> 
       <xsl:value-of select="'Y'" /> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$variable1" /> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 
0

पुनः असाइन करने योग्य चर एक संचायक, XSLT संस्करण 3.0 से उपलब्ध का उपयोग कर की घोषणा की जा सकती है। :

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0" > 
     <xsl:mode use-accumulators="variable2" streamable="no"/> 
     <xsl:output omit-xml-declaration="no" indent="yes"/> 

     <xsl:accumulator name="variable2" initial-value="'N'"> 
     <xsl:accumulator-rule match="Inpayment" select="if ($value = 'N' and @tip = '2') then 'Y' else 'N' "/> 
     </xsl:accumulator> 

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

     <xsl:template match="Inpayment"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:value-of select="accumulator-before('variable2')"/> 
      <xsl:apply-templates select="node()"/> 
     </xsl:copy> 
     </xsl:template> 

    </xsl:stylesheet> 
संबंधित मुद्दे