2012-02-06 8 views
3

だから私は、次のXMLスニペットを持っている...XSLT出力テキスト()

私はこれを取ると、HTMLにそれを配置する必要があります。私は各セクション(セクション)にそのセクションのテキストを印刷し、(b)タグが表示されている場合はその単語の周りにそのタグを出力したいと言いたいと思います。しかし、私はセクションのtext()だけを出力することができるので、これを行う方法がわかりません。

しかし、ノードのtext()を出力するだけでなく、そのtext()のタグを操作する必要があります。

これはサンプルXMLです:

<div class="body"> 

<xsl:for-each select='topic/body/section'> 

<div class="section"> 
<xsl:choose> 
<xsl:when test="title"> 
    <h2 class="title sectiontitle"><xsl:value-of select="title"/></h2> 
</xsl:when> 
<xsl:when test="p"> 
    [I dont know what to put here? I need to output both the text of the paragraph tag but also the html tags inside of it..] 
</xsl:when> 
</xsl:choose> 


</div> 
</xsl:for-each> 
</div> 

所望の出力 - XML内の各セクションのHTMLコードのこのブロック:

<body> 
<section> 
<title>Response</title> 
<p> Some info here <b> with some other tags</b> or lists like <ol> <li>something</li>  </ol></p> 
</section> 
<section>Another section same format, sections are outputted as divs </section> 
</body> 

これは私がこれまで持っているものです。

<div class="section"> 
<h2 class="title">Whatever my title is from the xml tag</h2> 
<p> The text in the paragraph with the proper html tags like <b> and <u> </p> 
</div> 
+1

サンプル入力XMLと望ましい出力を提供します。 –

+0

コードをお願いします! – TOUDIdel

答えて

2

これは非常に簡単です。 HTMLに変換するすべての要素のテンプレートを作成します。

あなたのためのテンプレートを書いていないすべてのノードが出力にコピーし、それらをそのままアイデンティティテンプレートによって処理されています。具体的には、<xsl:apply-templates>があることないXSLTプロセッサはあなたのためのすべての再帰を扱う

<!-- identity template --> 
<xsl:template match="node() | @*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
</xsl:template> 

<!-- <title> becomes <h2> --> 
<xsl:template match="title"> 
    <h2 class="title"> 
    <xsl:apply-templates select="node() | @*" /> 
    </h2> 
</xsl:template> 

<!-- <section> becomes <div> --> 
<xsl:template match="section"> 
    <div class="section"> 
    <xsl:apply-templates select="node() | @*" /> 
    </div> 
</xsl:template> 

<!-- <b> becomes <strong> --> 
<xsl:template match="b"> 
    <strong> 
    <xsl:apply-templates select="node() | @*" /> 
    </strong> 
</xsl:template> 

( )ので、あなたの入力

<section> 
    <title> some text </title> 
    Some stuff there will have other tags like <b> this </b> 
</section> 

<div class="section"> 
    <h2 class="title"> some text </h2> 
    Some stuff there will have other tags like <strong> this </strong> 
</div> 
に変わります210

IDテンプレートはノードを変更しないので、「<ul><ul>に変換する」テンプレートを書く必要はありません。これは単独で問題なく起こります。まだHTMLでない要素だけが独自のテンプレートを必要とします。そのテキスト値である(という<xsl:value-of>より

<xsl:template match="some/unwanted/element" /> 
+0

ああ、私は指定しない限り、テキストを出力することを認識していませんでした...ありがとう! – Kayla

+0

@Kaylaはい、これがデフォルトです。指定されない限り、XML要素は 'text()'を出力します。それは、[ここに記載されている](http://www.w3.org/TR/xslt#built-in-rule)(http://www.dpawson.co.uk/ xsl/sect2/defaultrule.html#d3635e76)。 – Tomalak

0

<xsl:copy-of select="."/>だろう出力要素の正確なコピー:あなたがHTMLに現れてから、特定の物事を防ぎたい場合は

、彼らのために空のテンプレートを作成します)。

@Tomalakは正しいですが、最初にXSLTを構造化する方が良い方法があります。

関連する問題