2009-10-06 17 views
10

Ho una domanda abbastanza stupida. Come posso assicurarmi che il mio nodo di contenuto misto XML non venga confuso? Ho, diciamo, una struttura XML che assomiglia a questo.Nodo contenuto XSLT

<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> 

Ho bisogno il contenuto finale per assomigliare a questo

<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> 

ma non riesco a estrarre le parti del nodo di testo, ho sempre afferrare il tutto. Sto usando l'asse discendente. Qualche idea su cosa sto facendo male?

Ecco il mio XSLT:

<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> 

Si prega di notare che l'XML chiusa e conseguente html sono solo esempi, ho a che fare con una struttura più grande che io non sono che racchiude in, per motivi di chiarezza.

+0

mente condividere il tuo xslt? –

+0

XSLT è stato condiviso. –

risposta

10

<xsl:apply-templates> è tuo amico:

<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> 

Il seguente output viene generato per il vostro XML di input (Nota: I convogliata attraverso ordinato per il bene di leggibilità non rilevanti white-space è stato. rimosso nel processo):

<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

Non smetterò mai di sbavare. Credo che dovrò lavorare un po 'più difficile per ottenere quei fastidiosi menu che si costruiscono da soli, grazie mille! : P –

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

Può essere un'opzione, anche se devo "cambiare" il mio link con un tag "a", per il quale l'informazione deve essere cambiata a seconda di alcune cose. –

+0

Ah, si. Non l'avevo visto, mi dispiace. – cadrian

Problemi correlati