2008-10-12 13 views
9

मैं निम्नलिखित टेम्पलेटमैं कैसे जांचूं कि एक्सएसएलटी में कोई टैग मौजूद है या नहीं?

<h2>one</h2> 
<xsl:apply-templates select="one"/> 
<h2>two</h2> 
<xsl:apply-templates select="two"/> 
<h2>three</h2> 
<xsl:apply-templates select="three"/> 

मैं केवल शीर्ष लेख (एक, दो, तीन) प्रदर्शित करने के लिए अगर वहाँ इसी टेम्पलेट के कम से कम एक सदस्य चाहते हैं। मैं इसके लिए कैसे जांच करूं?

+0

अधिक सटीक रहें :) आपकी xml फ़ाइल नहीं है जैसे आप इस टेम्पलेट का उपयोग करना चाहते हैं? – kender

उत्तर

15
<xsl:if test="one"> 
    <h2>one</h2> 
    <xsl:apply-templates select="one"/> 
</xsl:if> 
<!-- etc --> 

वैकल्पिक रूप से, आप एक नामित टेम्पलेट,

<xsl:template name="WriteWithHeader"> 
    <xsl:param name="header"/> 
    <xsl:param name="data"/> 
    <xsl:if test="$data"> 
     <h2><xsl:value-of select="$header"/></h2> 
     <xsl:apply-templates select="$data"/> 
    </xsl:if> 
</xsl:template> 

बना सकते हैं और फिर कहते हैं:

<xsl:call-template name="WriteWithHeader"> 
    <xsl:with-param name="header" select="'one'"/> 
    <xsl:with-param name="data" select="one"/> 
    </xsl:call-template> 

लेकिन ईमानदारी से, जो मेरे लिए अधिक काम की तरह लग रहा ... एक हेडर ड्राइंग करने पर केवल उपयोगी है जटिल ... एक साधारण <h2>...</h2> के लिए मैं इसे इनलाइन छोड़ने का लुत्फ उठाऊंगा।

हैडर शीर्षक हमेशा है नोड नाम, आप "$ हैडर" आर्ग को हटाने के द्वारा टेम्पलेट simplifiy सकता है, और बजाय का उपयोग करें:

<xsl:value-of select="name($header[1])"/> 
2

मैं XSL के कार्यात्मक पहलुओं व्यायाम करने के लिए की तरह जो मेरा पीछा कार्यान्वयन के लिए नेतृत्व:

<?xml version="1.0" encoding="UTF-8"?> 

<!-- test data inlined --> 
<test> 
    <one>Content 1</one> 
    <two>Content 2</two> 
    <three>Content 3</three> 
    <four/> 
    <special>I'm special!</special> 
</test> 

<!-- any root since take test content from stylesheet --> 
<xsl:template match="/"> 
    <html> 
     <head> 
      <title>Header/Content Widget</title> 
     </head> 
     <body> 
      <xsl:apply-templates select="document('')//test/*" mode="header-content-widget"/> 
     </body> 
    </html> 
</xsl:template> 

<!-- default action for header-content -widget is apply header then content views --> 
<xsl:template match="*" mode="header-content-widget"> 
    <xsl:apply-templates select="." mode="header-view"/> 
    <xsl:apply-templates select="." mode="content-view"/> 
</xsl:template> 

<!-- default header-view places element name in <h2> tag --> 
<xsl:template match="*" mode="header-view"> 
    <h2><xsl:value-of select="name()"/></h2> 
</xsl:template> 

<!-- default header-view when no text content is no-op --> 
<xsl:template match="*[not(text())]" mode="header-view"/> 

<!-- default content-view is to apply-templates --> 
<xsl:template match="*" mode="content-view"> 
    <xsl:apply-templates/> 
</xsl:template> 

<!-- special content handling --> 
<xsl:template match="special" mode="content-view"> 
    <strong><xsl:apply-templates/></strong> 
</xsl:template> 

एक बार शरीर सब परीक्षण तत्व में निहित तत्वों में हैडर जो सामग्री पर विजेट लागू किया (दस्तावेज़ क्रम में) है।

डिफ़ॉल्ट हैडर जो सामग्री पर विजेट टेम्पलेट (मिलान "*") पहले एक हैडर-व्यू लागू होता है तो मौजूदा तत्व को एक सामग्री-व्यू लागू होता है।

डिफ़ॉल्ट हैडर-व्यू टेम्पलेट h2 टैग में वर्तमान तत्व के नाम देता है। डिफ़ॉल्ट सामग्री-दृश्य सामान्य प्रसंस्करण नियम लागू करता है।

जब [नहीं (टेक्स्ट())] द्वारा निर्धारित कोई सामग्री नहीं है, तो तत्व के लिए कोई आउटपुट अनुमानित नहीं होता है।

एक विशेष बंद मामलों को आसानी से नियंत्रित किया जाता है।

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

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