2009-10-06 154 views
10

我有一個相當愚蠢的問題。我如何確保我的XML混合內容節點不會混淆?例如,我有一個類似這樣的XML結構。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> 

我需要的最終內容,看起來像這樣

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

但我不能提取文本節點的部分,我總是搶了整個事情。我正在使用後代軸。任何線索我做錯了什麼?

這裏是我的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> 

請注意,封閉的XML和HTML結果僅僅是例子,我不得不面對它,我不封閉在,爲了清楚起見,一個更大的結構。

+0

介意分享一下你的xslt嗎? – 2009-10-06 12:25:06

+0

XSLT已被共享。 – 2009-10-06 12:34:23

回答

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生成(注:我用管道輸送它通過整潔的可讀性的原因不相關的空白了。刪除過程中):

<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

我永遠不會停止流口水。我想我只需要努力一點就可以讓這些煩人的菜單自己建設,非常感謝! :P – 2009-10-06 13:08:53

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

可以是一個選項,但我必須將鏈接「更改」爲「a」標籤,對於該標籤,信息必須根據某些內容進行更改。 – 2009-10-06 12:33:06

+0

啊,是的。沒有看到,對不起。 – cadrian 2009-10-06 12:51:40