XSLT混合内容节点

问题描述:

我有一个相当愚蠢的问题。我如何确保我的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

<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

<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