2010-01-30 15 views
10

क्या एक वैरिएबल टेम्पलेट से अपने बच्चे तत्व में एक वैरिएबल पास करना संभव है?एक्सएसएल: टेम्पलेट्स के बीच गुजरने वाले चर

<xsl:template match="structure"> 
    <xsl:variable name="var"><xsl:value-of select="@path" /></xsl:variable> 
    <xsl:apply-templates select="folders"> 
    <xsl:with-param name="var1" select="'{var}'"/> 
    </xsl:apply-templates> 
</xsl:template> 

इस टेम्पलेट मिलान हो जाएगा:

<xsl:template match="folder"> 
    <xsl:param name="var1"/> 
    <xsl:value-of select="$var1"/> 
</xsl:template> 

तुम देखो मैं मिलान किया टेम्पलेट में var1 के रूप में उपयोग वर चाहता हूँ।

मैं यह काम कैसे कर सकता हूं?

संपादित करें: संरचना इस तरह है:

<structure path="C:\xampplite\htdocs\xampp"> 
    <folders> 
    <folder name="img"> 
     <date>01/28/10 21:59:00</date> 
     <size>37.4 KB</size> 
    </folder> 
</folders> 
</structure> 

EDIT2:

<xsl:template match="folder"> 
<xsl:variable name="var1"><xsl:value-of select="../../@path"/></xsl:variable> 
<xsl:variable name="var2"><xsl:value-of select="@name" /></xsl:variable> 
<xsl:variable name="var3"><xsl:value-of select="$var1"/>\<xsl:copy-of select="$var2"/> </xsl:variable> 
<th colspan="2" align="left" bgcolor="#FF5500"><a onclick="foo('{$var3}')"><xsl:value-of select="$var3"/></a></th> 

jscript समारोह में स्ट्रिंग अपने बैकस्लैश के बिना है। किसी को पता है क्यों?

सी: xampplitehtdocsxamppimg structure टेम्पलेट के साथ

+0

इसे "बस काम करना चाहिए" - सवाल का तात्पर्य है कि यह नहीं है। कोई प्रतीक्षा नहीं ... टेम्पलेट का नाम और चयन = दिखाए गए कोड में मेल नहीं खाता है, आपको चयन = "फ़ोल्डर्स" और मैच = "फोल्डर" के साथ नहीं मिला है। क्या यह समस्या है या यह सिर्फ एक उदाहरण है? – Murph

+0

मेरा जवाब यहां देखें http://stackoverflow.com/a/41530702/4251431 –

उत्तर

26

आप नामित टेम्पलेट्स के मानकों है कि आप <xsl:call-template> के माध्यम से फोन, उदा .:

<xsl:call-template name="name"> 
    <xsl:with-param name="param" select="xpathexpr"/> 
</xsl:call-template> 

<xsl:template name="name"> 
    <xsl:param name="param"/> 
    ... 
</xsl:template> 

पारित कर सकते हैं जब आप एक नामित टेम्पलेट कहते हैं, संदर्भ नोड वर्तमान संदर्भ है। तो बच्चे नोड्स के लिए नामांकित टेम्पलेट कॉल करने के लिए, आप <xsl:for-each> का उपयोग करके वर्तमान संदर्भ बदलने की जरूरत:

<xsl:for-each select="child"> 
    <xsl:call-template name="name"> 
     <xsl:with-param name="param" select="xpathexpr"/> 
    </xsl:call-template> 
</xsl:for-each> 

आपके मामले में, हालांकि, वहाँ पैरामीटर भेजने की आवश्यकता नहीं है, चर है कि आप की कोशिश कर रहे के बाद से उपयोग कुछ ऐसा है जो संदर्भ नोड से नेविगेट करने योग्य है।और आप उन सभी चर का उपयोग करने की जरूरत नहीं है (और न ही आप कभी भी एक चर var1 के रूप में के रूप में बेकार एक नाम देना चाहिए):

<xsl:template match="folder"> 
    <xsl:variable name="linkarg" value="concat(../../@path, '\\', @name)"/> 
    <xsl:variable name="linktext" value="concat(../../@path, '\', @name)"/> 
    <th colspan="2" align="left" bgcolor="#FF5500"> 
     <a onclick="foo('{$linkarg}')"> 
     <xsl:value-of select="$linktext"/> 
     </a> 
    </th> 
</xsl:template> 

इसके अलावा, मैं ../../@path बजाय ancestor::structure[1]/@path का उपयोग करने के लिए परीक्षा होगी, क्योंकि यह इरादा को और अधिक स्पष्ट बनाता है; अपने संस्करण का मतलब है, "जनक तत्व के माता पिता से path विशेषता प्राप्त" जबकि मेरी संस्करण का अर्थ है "जब तक आप पहले एक structure नामित खोजने के पूर्वज तत्वों की श्रृंखला को पार, और इसके path विशेषता मिलता है।"

+2

+1 ऐसा करने के बेवकूफ तरीके की सलाह देने के लिए +1 (उदाहरण के लिए चर के बजाय संदर्भ का उपयोग करना)। (बीटीडब्ल्यू: आपके उत्तर में बैकस्लाश को ठीक करने की आवश्यकता है।) – Tomalak

+0

ओह, ठीक है। जावास्क्रिप्ट। फिक्स्ड। –

+0

नहीं, अभी भी पूरी तरह से तय नहीं है। XPath में कोई बैकस्लैश अनुमति नहीं है (अंतिम पैराग्राफ देखें)। – Tomalak

5

दो समस्याओं:

  1. आप folders टेम्पलेट चुनने आवेदन कर रहे हैं, लेकिन folder पर टेम्पलेट मिलान की है। या तो इसे folder पर बदलें, या यदि आपके पास folders टेम्पलेट है, तो सुनिश्चित करें कि यह var1 पैरामीटर मान folder टेम्पलेट पर नीचे गुजरता है।
  2. आपके साथ-param @select '{var}' का उपयोग करता है, जो उस शाब्दिक स्ट्रिंग {var} का चयन करता है। यदि आप var चर का चयन करना चाहते हैं, तो आसपास के उद्धरण और घुंघराले ब्रेसिज़ को हटाएं और बस $var चुनें।

अपने structure टेम्पलेट के लिए लागू किए गए परिवर्तन:

<xsl:template match="structure"> 
    <xsl:variable name="var"><xsl:value-of select="@path" /></xsl:variable> 
    <xsl:apply-templates select="folder"> 
    <xsl:with-param name="var1" select="$var"/> 
    </xsl:apply-templates> 
</xsl:template> 
4

कॉल के लिए सटीक कोड होगा:

<xsl:template match="structure"> 
    <xsl:variable name="var"><xsl:value-of select="@path" /></xsl:variable> 
    <xsl:apply-templates select="folders/folder"> 
    <xsl:with-param name="var1" select="$var"/> 
    </xsl:apply-templates> 
</xsl:template> 

रूट नोड के @path विशेषता का उपयोग करने की होगी एक और तरीका है अपने टेम्पलेट को संशोधित करने के लिए:

<xsl:template match="folder"> 
    <xsl:value-of select="../../../@path"/> 
</xsl:template> 
+0

अंतिम प्रस्ताव मेरे लिए ठीक काम करता है। thx – binaryguy

7

XSLT 2.0 का उपयोग करते हैं, यह , बच्चे टेम्पलेट्स के लिए मानकों को पारित करने के लिए callsite पर <xsl:with-param .../> को tunnel="yes" जोड़कर संभव है, और कहा जाता है टेम्पलेट पर </xsl:with-param .../> तत्व के रूप में अच्छी तरह से। बस कार्य करें:

<xsl:template match="folder"> 
    <xsl:param name="var1" tunnel="yes"/> <!-- note the 'tunnel="yes"' attribute here! --> 
    <xsl:value-of select="$var1"/> 
</xsl:template> 

<xsl:template match="structure"> 
    <xsl:variable name="var"><xsl:value-of select="@path" /></xsl:variable> 
    <xsl:apply-templates select="folders"> 
    <xsl:with-param name="var1" select="$var" tunnel="yes"/> <!-- note the 'tunnel' attribute here, too! --> 
    </xsl:apply-templates> 
</xsl:template> 

अधिक जानकारी के लिए, XSLT 2.0 विनिर्देश में खंड 10.1.2 Tunnel parameters को देखें।


एक विस्तारित उदाहरण

सुरंग मानकों के साथ

, तो आप भी ऐसा कर सकता है: सुरंग विशेषता की वजह से

<xsl:template match="structure"> 
    <!-- same as before --> 
</xsl:template> 

<xsl:template match="folder"> 
    <!-- Look, ma, no param declaration! --> 
    <!-- ... --> 
    <xsl:apply-templates select="date"/> 
    <!-- ... --> 
</xsl:template> 

<xsl:template match="folder/date"> 
    <xsl:param name="var1" tunnel="yes"/> 
    <xsl:value-of select="$var1"/> 
</xsl:template> 

, var1 पैरामीटर सभी मध्यवर्ती टेम्पलेट्स के माध्यम से प्रारंभिक टेम्पलेट से पारित हो जाता है "folder/date" टेम्पलेट में।

बस याद रखें कि tunnel="yes" विशेषता घोषणा <xsl:param name="var1" tunnel="yes"/>और इसी <xsl:with-param name="var1" tunnel="yes" select="..."/> विशेषता पर दोनों मौजूद होना चाहिए।

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