2009-10-06 23 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> 

のように見える。しかし、私はテキストノードの部分を抽出することはできません、私はいつも全体をつかむために、最終的なコンテンツを必要としています。子孫軸を使用しています。私が間違って何をしているか何か手がかり?

<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は一例であることに注意してください:

は、ここに私のXSLTです。

+0

あなたのxsltを共有してもいいですか? –

+0

XSLTが共有されました。 –

答えて

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 –

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"タグに "変更"する必要がありますが、いくつかの情報に応じて情報を変更する必要がありますが、オプションにすることができます。 –

+0

ああ、そうです。それを見ていない、申し訳ありません。 – cadrian

関連する問題