2013-01-10 14 views
6

के माध्यम से एक एक्सएसएलटी चर संदर्भित करें मैं एक छोटी सी समस्या से फंस गया हूं।एक गतिशील नाम

XSL-फ़ाइल:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
<xsl:template match="/"> 


<xsl:variable name="unumericValue" select="10" /> 
<xsl:variable name="uanotherValue" select="8" /> 



<xsl:for-each select="/root/try"> 
<xsl:value-of select="var" /> 
<xsl:variable name="min"><xsl:value-of select="@minimum" /></xsl:variable> 
<xsl:value-of select="@type" /> 
<xsl:variable name="referenceName"><xsl:value-of select='concat("u",var)' /></xsl:variable> 
<xsl:value-of select="$referenceName" /> 
<xsl:if test='$referenceName > $min'> 
<p>Do something.</p> 
</xsl:if> 
</xsl:for-each> 

</xsl:template> 
</xsl:stylesheet> 

एक्सएमएल फ़ाइल:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="q1.xsl"?> 
<root> 
<try type="compare" minimum="9"> 
<var>numericValue</var> 
<something>...</something> 
</try> 

<try type="compare" minimum="10"> 
<var>anotherValue</var> 
<something>...</something> 
</try> 
</root> 

आप एक्सएमएल फ़ाइल दो वर-तत्व जो XSLT फ़ाइल में चर के लिए मैच चाहिए है देख सकते हैं । हालांकि मुझे नहीं पता कि कौन सी सिंटेक्स सही है। $ संदर्भ नाम केवल वेरिएबल का नाम है जिसका मैं उपयोग करना चाहता हूं। लेकिन मुझे नहीं पता कि मौजूदा चर में नाम का संदर्भ कैसे दिया जाए।

उत्तर

11

$referenceName "unumericValue" नाम से भिन्नता का संदर्भ नहीं है या अन्यथा। यह सिर्फ स्ट्रिंग मान "अन्युमेरिक वैल्यू" इत्यादि है, इसलिए यह $min से अधिक कभी नहीं होगा। हालांकि, कुछ अतिरिक्त कार्यों के साथ, वहाँ एक चाल को उसके नाम से चर को मिल रहा है: यहाँ नोट करने के लिए

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:variable name="numericValue" select="10" /> 
    <xsl:variable name="anotherValue" select="8" /> 
    <xsl:variable name="vars" select="document('')/*/xsl:variable" /> 

    <xsl:template match="/"> 
    <xsl:variable name="referenceName" select="'numericValue'" /> 
    <xsl:variable name="referenceValue" select="$vars[@name = $referenceName]/@select" /> 
    Reference value: <xsl:value-of select="$referenceValue" /> 
    </xsl:template> 
</xsl:stylesheet> 

एक बड़ा सीमा यह है कि यह केवल चर कि एक निरंतर संख्यात्मक मान रहे हैं के लिए काम करेगा।

यहाँ एक तरह से स्थिर स्ट्रिंग मूल्यों के साथ चर अनुकरण करने के लिए है:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:v="variables-node" 
> 
    <v:variables> 
    <v:variable n="numericValue" value="10" /> 
    <v:variable n="nonNumericValue" value="Hello World" /> 
    </v:variables> 
    <xsl:variable name="vars" select="document('')//v:variables/v:variable" /> 

    <xsl:template match="/"> 
     <xsl:variable name="referenceName" select="'nonNumericValue'" /> 
     <xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" /> 
     <xsl:value-of select="concat('The variable with the name ', $referenceName, ' has the value ', $referenceValue)"/> 
    </xsl:template> 
</xsl:stylesheet> 

और अंत में, एक तरह से गणना की मूल्यों के साथ चर अनुकरण करने के लिए:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:exslt="http://exslt.org/common" 
> 

    <xsl:variable name="varsRaw"> 
    <var n="computedValue" value="{concat('2 + 4 is ', 2 + 4)}" /> 
    <var n="computedNumber" value="{22 div 7}" /> 
    </xsl:variable> 
    <xsl:variable name="vars" select="exslt:node-set($varsRaw)/var" /> 

    <xsl:template match="/"> 
     <xsl:variable name="referenceName" select="'computedValue'" /> 
     <xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" /> 
     <xsl:value-of select="concat('The variable with the name ', $referenceName, ' has the value ', $referenceValue)"/> 

     <xsl:value-of select="'  '"/> 

     <xsl:variable name="referenceName2" select="'computedNumber'" /> 
     <xsl:variable name="referenceValue2" select="$vars[@n = $referenceName2]/@value" /> 
     <xsl:value-of select="concat('The variable with the name ', $referenceName2, ' has the value ', $referenceValue2)"/> 
    </xsl:template> 
</xsl:stylesheet> 

पिछले दृष्टिकोण शायद वास्तव में सबसे रूढ़िवादी है , लेकिन एक्सएसएलटी प्रोसेसर-निर्भर (कम से कम एक्सएसएलटी 1.0 में) node-set() फ़ंक्शन की आवश्यकता है।

+0

बहुत बहुत धन्यवाद। पहला सुझाव मेरे लिए बहुत अच्छा काम किया। – eadrax

1

अधिकांश प्रोग्रामिंग भाषाओं के साथ, एक्सएसएलटी परिवर्तनीय नाम रन-टाइम पर उपलब्ध नहीं हैं। परिवर्तनीय रन-टाइम पर भी मौजूद नहीं हो सकता है - ऑप्टिमाइज़र को सभी प्रकार की चाल चलाने की अनुमति है, जैसे वेरिएबल के सभी संदर्भों को उस बिंदु पर रेखांकित करते हैं जहां चर का उपयोग किया जाता है।

सबसे अच्छा तरीका एक मानक नाम के साथ एक चर है, और इसे एक्सएमएल सामग्री दें। एक्सएमएल में तत्व और विशेषता नाम रन-टाइम पर परिवर्तनीय नामों के विपरीत उपलब्ध हैं।

5

वैसे, ऐसा नहीं करते हैं:

<xsl:variable name="min"><xsl:value-of select="@minimum" /></xsl:variable> 

आप ऐसा कर सकता है जब:

<xsl:variable name="min" select="@minimum" /> 

यह केवल वर्बोज़, यह भी अक्षम है - डेटा की नकल करने की आवश्यकता नहीं है और एक नया पेड़ बनाएं, जो एक बहुत महंगा ऑपरेशन है, जब आप चाहते हैं कि मौजूदा नोड का संदर्भ हो।

+0

बेशक आप सही हैं। यह लिखने के लिए भी बहुत आसान और छोटा है :) – eadrax

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