2009-10-06 39 views
10

मेरे पास एक बहुत बेवकूफ सवाल है। मैं कैसे सुनिश्चित कर सकता हूं कि मेरा एक्सएमएल मिश्रित सामग्री नोड मिश्रित नहीं होता है? मेरे पास एक एक्सएमएल संरचना है, जैसा कि मैंने कहा है।एक्सएसएलटी मिश्रित सामग्री नोड

<root> 
<book> 
    <title>Stuff</title> 
    <description> This book is <i>great</i> if you need to know about stuff. 
       I suggest <link ref="Things">this one</link> if you need to know 
       about things. </description> 
</book> 
[other books] 
</root> 

मैं अंतिम सामग्री की आवश्यकता इस

<h1>List of books</h1> 
<h2><a name="Stuff"/>Stuff</h2> 
<p> This book is <i>great</i> if you need to know about stuff. 
    I suggest <a href="#Things">this one</a> if you need to know 
    about things. </p> 

तरह देखने के लिए लेकिन मैं पाठ नोड के कुछ भागों को अलग नहीं कर सकते हैं, मैं हमेशा पूरी बात हड़पने। मैं वंशज अक्ष का उपयोग कर रहा हूँ। कोई सुराग क्या मैं गलत कर रहा हूँ?

<xsl:template match="description/*"> 
    <xsl:for-each select="following-sibling::*"> 
      <xsl:choose> 
      <xsl:when test="name(.)='link'"> 
       <a href="{@ref}"><xsl:value-of select="."/></a> 
      </xsl:when> 
      <xsl:when test="name(.)='em'"> 
       <em><xsl:value-of select="."/></em> 
      </xsl:when> 
      <xsl:otherwise><p><xsl:value-of select="."/></p></xsl:otherwise>  
     </xsl:choose> 
    </xsl:for-each> 
    </xsl:template> 

कृपया ध्यान दें कि संलग्न एक्सएमएल और जिसके परिणामस्वरूप एचटीएमएल केवल उदाहरण हैं, मैं एक बड़ा संरचना जो मैं में बंद नहीं कर रहा हूँ, स्पष्टता के लिए से निपटने के लिए:

यहाँ मेरी xslt है।

+0

आपके xslt को साझा करने के लिए दिमाग? –

+0

एक्सएसएलटी साझा किया गया है। –

उत्तर

10

<xsl:apply-templates> अपने दोस्त है:

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

    <xsl:template match="root"> 
    <h1>List of books</h1> 
    <xsl:apply-templates /> 
    </xsl:template> 

    <!-- a <book> consists of its <title> and <description> --> 
    <xsl:template match="book"> 
    <xsl:apply-templates select="title" /> 
    <xsl:apply-templates select="description" /> 
    </xsl:template> 

    <!-- <title> is turned into a <h2> --> 
    <xsl:template match="title"> 
    <h2> 
     <a name="{.}"/> 
     <xsl:value-of select="." /> 
    </h2> 
    </xsl:template> 

    <!-- <description> is turned into a <p> --> 
    <xsl:template match="description"> 
    <p> 
     <xsl:apply-templates /> 
    </p> 
    </xsl:template> 

    <!-- default rule: copy any node beneath <description> --> 
    <xsl:template match="description//*"> 
    <xsl:copy> 
     <xsl:copy-of select="@*" /> 
     <xsl:apply-templates /> 
    </xsl:copy> 
    </xsl:template> 

    <!-- override rule: <link> nodes get special treatment --> 
    <xsl:template match="description//link"> 
    <a href="#{@ref}"> 
     <xsl:apply-templates /> 
    </a> 
    </xsl:template> 

    <!-- default rule: ignore any unspecific text node --> 
    <xsl:template match="text()" /> 

    <!-- override rule: copy any text node beneath description --> 
    <xsl:template match="description//text()"> 
    <xsl:copy-of select="." /> 
    </xsl:template> 

</xsl:stylesheet> 

निम्नलिखित उत्पादन अपने इनपुट XML के लिए उत्पन्न होता है (नोट: मैं इसे पठनीयता के कारण साफ के माध्यम से पहुंचाया गैर प्रासंगिक white-space था। प्रक्रिया में हटा दी गई):

<h1>List of books</h1> 
<h2><a name="Stuff">Stuff</h2> 
<p>This book is <i>great</i> if you need to know about stuff. I 
suggest <a href="#Things">this one</a> if you need to know about 
things.</p> 
+0

मैं कभी भी डोलिंग नहीं रोकूंगा। मुझे लगता है कि मुझे उन परेशान मेनू को खुद को बनाने के लिए बस एक कठिन काम करना होगा, बहुत बहुत धन्यवाद! : पी –

0
<root> 
<book> 
    <title>Stuff</title> 
    <description><![CDATA[ 
     This book is <i>great</i> if you need to know about stuff. 
     I suggest <link ref="Things">this one</link> if you need to know 
     about things. 
    ]]></description> 
</book> 
[other books] 
</root> 
+0

एक विकल्प हो सकता है, हालांकि मुझे अपने लिंक को "ए" टैग में "बदलना" है, जिसके लिए कुछ सामानों के आधार पर जानकारी बदलनी है। –

+0

आह, हाँ। ऐसा नहीं देखा था, क्षमा करें। – cadrian

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