2014-09-23 5 views
5

मैं है निम्न XML स्रोत:बिल्ड XML संरचना

<element not-relevant="true" bold="true" superscript="true" subscript="false" text="stuff"/> 

किसी भी क्रम में, मैं विशिष्ट विशेषताओं के माध्यम से लूप (यानी केवल लोगों को जो एचटीएमएल मैं निर्माण कर रहा हूँ से संबंधित हैं की जरूरत है : बोल्ड/सुपरस्क्रिप्ट/सबस्क्रिप्ट, आदि) और जहां इन विशेषताओं में से एक 'सही', उत्पादन नेस्ट तत्वों का मूल्यांकन निम्नलिखित उत्पादन प्राप्त करने के लिए:

<strong> 
    <sup> 
     stuff 
    </sup> 
</strong> 

मुझे लगता है मैं प्रत्यावर्तन किसी प्रकार का उपयोग करने की आवश्यकता निम्नानुसार (अनंत लूप के बिना, निश्चित रूप से):

<xsl:template match="element"> 
    <xsl:call-template name="content"> 
     <xsl:with-param name="text" select="@text"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="content"> 
    <xsl:param name="text"/> 
    <xsl:choose> 
     <xsl:when test="@bold = 'true'"> 
      <strong> 
       <xsl:copy> 
        <xsl:call-template name="content"> 
         <xsl:with-param name="text" select="$text"/> 
        </xsl:call-template> 
       <xsl:copy> 
      </strong> 
     </xsl:when> 
     <xsl:when test="@subscript = 'true'"> 
      <sub> 
       <xsl:copy> 
        <xsl:call-template name="content"> 
         <xsl:with-param name="text" select="$text"/> 
        </xsl:call-template> 
       <xsl:copy> 
      </sub> 
     </xsl:when> 
     <xsl:when test="@superscript = 'true'"> 
      <sup> 
       <xsl:copy> 
        <xsl:call-template name="content"> 
         <xsl:with-param name="text" select="$text"/> 
        </xsl:call-template> 
       <xsl:copy> 
      </sup> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$text" disable-output-escaping="yes"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

कोई विचार? मैं सबसे साफ एक्सएसएलटी 2.0 समाधान की तलाश में हूं और मदद की सराहना करता हूं।

धन्यवाद,

उत्तर

6

यह <xsl:next-match/> के लिए एक अच्छी उपयोग मामला है:

<xsl:template match="element" priority="1"> 
    <xsl:value-of select="@text" /> 
</xsl:template> 

<xsl:template match="element[@superscript = 'true']" priority="2"> 
    <sup><xsl:next-match/></sup> 
</xsl:template> 

<xsl:template match="element[@subscript = 'true']" priority="3"> 
    <sub><xsl:next-match/></sub> 
</xsl:template> 

<xsl:template match="element[@bold = 'true']" priority="4"> 
    <strong><xsl:next-match/></strong> 
</xsl:template> 

जब आप एक element तत्व को टेम्पलेट लागू यह पहली बार सर्वोच्च प्राथमिकता मिलान टेम्पलेट आग, और यदि उस टेम्पलेट का उपयोग करता है next-match यह अगली सर्वोच्च प्राथमिकता आदि के लिए प्रतिनिधि होगा। प्रश्न में आपका उदाहरण element उपरोक्त पहले, दूसरे और चौथे टेम्पलेट से मेल खाता है, इसलिए प्रारंभ में "बोल्ड" टेम्पलेट चुना जाएगा, फिर वह "सुपरस्क्रिप्ट" को प्रतिनिधि करेगा और अंत में जेनेरिक के लिए element एक, जिसके परिणामस्वरूप <strong><sup>stuff</sup></strong> है।

जैसा कि आप इस उदाहरण से देख सकते हैं, प्राथमिकता संख्याएं घोंसले के क्रम को निर्धारित करती हैं - यदि दूसरे और चौथे टेम्पलेट की प्राथमिकताओं को उलट दिया गया तो आपको <sup><strong>stuff</strong></sup> मिल जाएगा।

+0

कभी नहीं पता था कि यह अस्तित्व - अद्भुत समाधान है। धन्यवाद! – Aaron