2012-02-04 10 views
7

एक्सएसएलटी/XPATH 1.0 का उपयोग करके, मैं HTML बनाना चाहता हूं जहां classspan तत्व की विशेषता मूल XML पदानुक्रम में गहराई को इंगित करती है।पदानुक्रम में वर्तमान नोड की आउटपुट गहराई

उदाहरण के लिए, इस XML टुकड़ा के साथ:

<text> 
    <div type="Book" n="3"> 
     <div type="Chapter" n="6"> 
      <div type="Verse" n="12"> 
      </div> 
     </div> 
    </div> 
</text> 

मैं इस HTML हैं:

<span class="level1">Book 3</span> 
<span class="level2">Chapter 6</span> 
<span class="level3">Verse 12</span> 

कैसे गहरी इन div तत्वों जा सकते हैं एक प्रायोरी ज्ञात नहीं है। div एस पुस्तक -> अध्याय हो सकता है। वे वॉल्यूम हो सकते हैं -> पुस्तक -> अध्याय -> अनुच्छेद -> रेखा।

मैं @type के मानों पर भरोसा नहीं कर सकता। कुछ या सभी न्यूल हो सकते हैं।

उत्तर

16

यह एक बहुत ही सरल और कम समाधान है - कोई प्रत्यावर्तन, कोई पैरामीटर, कोई xsl:element, कोई xsl:attribute:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="div"> 
    <span class="level{count(ancestor::*)}"> 
    <xsl:value-of select="concat(@type, ' ', @n)"/> 
    </span> 
    <xsl:apply-templates/> 
</xsl:template> 
</xsl:stylesheet> 

जब इस बदलाव प्रदान की XML दस्तावेज़ पर लागू किया जाता है:

<text> 
    <div type="Book" n="3"> 
     <div type="Chapter" n="6"> 
      <div type="Verse" n="12"></div></div></div> 
</text> 

चाहता था, सही परिणाम उत्पादन किया जाता है:

<span class="level1">Book 3</span> 
<span class="level2">Chapter 6</span> 
<span class="level3">Verse 12</span> 

स्पष्टीकरण: टेम्पलेट्स, एवीटी और count() समारोह का उचित उपयोग।

0

आमतौर पर एक्सएसएल के साथ, रिकर्सन का उपयोग करें।

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

    <xsl:template match="/text"> 
    <html> 
     <xsl:apply-templates> 
     <xsl:with-param name="level" select="1"/> 
     </xsl:apply-templates> 
    </html> 
    </xsl:template> 


    <xsl:template match="div"> 
    <xsl:param name="level"/> 

    <span class="{concat('level',$level)}"><xsl:value-of select="@type"/> <xsl:value-of select="@n"/></span> 

    <xsl:apply-templates> 
     <xsl:with-param name="level" select="$level+1"/> 
    </xsl:apply-templates> 
    </xsl:template> 


</xsl:stylesheet> 
5

या प्रत्यावर्तन का उपयोग किए बिना - लेकिन Dimitre के जवाब से बेहतर है मेरी एक

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

<xsl:template match="/text"> 
    <html> 
     <body> 
      <xsl:apply-templates/> 
     </body> 
    </html> 
</xsl:template> 

<xsl:template match="//div"> 
    <xsl:variable name="depth" select="count(ancestor::*)"/> 
    <xsl:if test="$depth > 0"> 
     <xsl:element name="span"> 
      <xsl:attribute name="class"> 
       <xsl:value-of select="concat('level',$depth)"/> 
      </xsl:attribute> 
      <xsl:value-of select="concat(@type, ' ' , @n)"/> 
     </xsl:element> 
    </xsl:if> 
    <xsl:apply-templates/> 
</xsl:template> 

</xsl:stylesheet> 
संबंधित मुद्दे