2010-10-24 8 views
5

मान लीजिए मेरे पास टेम्पलेट foo है जो पैरामीटर दिए गए कुछ आउटपुट कर सकता है। अब मैं अपने आउटलेट, loop पर पैरामीटर के रूप में उस आउटपुट का उपयोग करना चाहता हूं, इसलिए मैं आउटपुट को एक निश्चित संख्या में लूप कर सकता हूं। मैंएक्सएसटी - कॉल-टेम्पलेट से आउटपुट का उपयोग रिटर्न वैल्यू

<xsl:call-template name="loop"> 
     <xsl:with-param name="times" select="someParam"/> 
     <xsl:with-param name="output"> 
      <xsl:call-template name="foo"> 
       <xsl:with-param name="type" select="something"/> 
      </xsl:call-template> 
     </xsl:with-param> 
    </xsl:call-template> 

दूसरे शब्दों में, output के रास्ते अब foo करने के लिए कॉल से उत्पादन को शामिल करना चाहिए साथ कुछ कोशिश की है। loop और foo दोनों स्वतंत्र रूप से काम करते हैं लेकिन ऐसा लगता है कि मैं उन्हें इस तरह से घोंसला नहीं कर सकता। मुझे इसे कैसे पूरा करना चाहिए? अग्रिम में धन्यवाद।

+0

अच्छा सवाल है, +1। अपने प्रदत्त कोड के कुछ उदाहरण और कुछ अनुशंसाओं के लिए मेरा उत्तर देखें। –

उत्तर

9

समस्या उस कोड में है जिसे आपने हमें नहीं दिखाया है। इस श्रृंखला/पाइप टेम्पलेट्स के लिए एक सही तरीका है, हालांकि मैं, इसकी सलाह नहीं होगा (इस जवाब के अंत में देखें)

यह परिवर्तन:

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

<xsl:template match="/"> 
    <xsl:call-template name="loop"> 
     <xsl:with-param name="times" select="3"/> 
     <xsl:with-param name="output"> 
      <xsl:call-template name="foo"> 
       <xsl:with-param name="pN" select="5"/> 
      </xsl:call-template> 
     </xsl:with-param> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="loop"> 
    <xsl:param name="times" select="1"/> 
    <xsl:param name="output" select="2"/> 

    <xsl:choose> 
     <xsl:when test="not($times > 0)"> 
     <xsl:value-of select="$output"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:call-template name="loop"> 
     <xsl:with-param name="times" select="$times -1"/> 
     <xsl:with-param name="output" select="2*$output"/> 
     </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<xsl:template name="foo"> 
    <xsl:param name="pN" select="1"/> 

    <xsl:value-of select="2*$pN"/> 
</xsl:template> 
</xsl:stylesheet> 

जब किसी भी एक्सएमएल (नहीं पर लागू

80 

शैली संबंधी सिफारिश: प्रयुक्त), चाहता था, सही परिणाम पैदा करता

इस तरह से चेनिंग टेम्पलेट से बचने का प्रयास करें क्योंकि यह परिणाम अपठनीय और अनजान कोड में हैं।

इंटरमीडिएट परिणाम (सही ढंग से नामित) चर प्राप्त करना बेहतर है। कोड न केवल इस तरह से अधिक पठनीय और रखरखाव योग्य है, लेकिन किसी भी मध्यवर्ती परिणाम को फिर से मूल्यांकन करने की आवश्यकता के बिना कई बार फिर से उपयोग किया जा सकता है।

यहाँ एक ही परिवर्तन है, लेकिन सिफारिश की शैलीगत आवश्यकताओं को पूरा करने पर निर्भर करता:

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

<xsl:template match="/"> 
    <xsl:variable name="vTwice"> 
    <xsl:call-template name="twice"> 
     <xsl:with-param name="pN" select="5"/> 
    </xsl:call-template> 
    </xsl:variable> 

    <xsl:call-template name="loop"> 
     <xsl:with-param name="pTtimes" select="3"/> 
     <xsl:with-param name="pN" select="$vTwice"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="loop"> 
    <xsl:param name="pTtimes" select="1"/> 
    <xsl:param name="pN" select="2"/> 

    <xsl:choose> 
     <xsl:when test="not($pTtimes > 0)"> 
     <xsl:value-of select="$pN"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:call-template name="loop"> 
     <xsl:with-param name="pTtimes" select="$pTtimes -1"/> 
     <xsl:with-param name="pN" select="2*$pN"/> 
     </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<xsl:template name="twice"> 
    <xsl:param name="pN" select="1"/> 

    <xsl:value-of select="2*$pN"/> 
</xsl:template> 
</xsl:stylesheet> 
+0

+1 सही उत्तर। –

संबंधित मुद्दे