2009-10-06 13 views
10

Mam dość głupie pytanie. Jak mogę się upewnić, że mój węzeł XML o zawartości mieszanej nie zostanie pomieszany? Mam, powiedzmy, strukturę XML podobną do tej.Węzeł mieszanych treści 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> 

muszę ostateczną treść wyglądać ten

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

Ale nie mogę wyodrębnić części węzła tekstowego, zawsze chwycić całą sprawę. Używam osi potomnej. Jakąkolwiek wskazówkę, co robię źle?

Oto mój 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> 

Należy pamiętać, że załączona XML i wynikające html są jedynie przykładami, mam do czynienia z większą strukturę, która nie jestem w zamykającym, dla zachowania jasności.

+0

Umysł podzielić się swoim xslt? –

+0

XSLT został udostępniony. –

Odpowiedz

10

<xsl:apply-templates> jest twoim przyjacielem:

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

Poniższy wyjściowy jest generowany dla XML wejściowego (Uwagi: I rurami go poprzez uporządkowane ze względu na czytelność nieistotnych white-space było. usunięte w procesie):

<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

Nigdy nie przestanę ślinić się. Sądzę, że będę musiał odrobinę ciężej popracować nad tym brzydkim menu budującym się, dzięki dużo! : 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

Może być opcją, chociaż muszę "zmienić" mój link do znacznika "a", dla którego informacje muszą zostać zmienione w zależności od pewnych rzeczy. –

+0

Ah, tak. Nie widziałem tego, przepraszam. – cadrian