2011-09-09 8 views
0

私は、次のXML持っ注入:私は何とか、このようなXSLTは、HTML

非常に素敵人としてHTMLを生成するXSLT変換では、このXMLを使用したい

<person> 
     <name>John</name> 
     <htmlDescription>A <strong>very</strong> <b><i>nice</i></b> person </htmlDescription> 
</person> 

これはXSLTを使用して可能ですか?

答えて

3

あなたがHTMLに変換した場合:

<xsl:stylesheet version='1.0' 
       xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> 

<xsl:output method='html'/> 

<xsl:template match='person'> 
    <td> 
     <xsl:copy-of select='htmlDescription/node()'/> 
    </td> 
</xsl:template> 

</xsl:stylesheet> 

をあなたはXHTMLに変換した場合:

<xsl:stylesheet version='1.0' 
       xmlns='http://www.w3.org/1999/xhtml' 
       xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> 

<xsl:output method='xml'/> 

<xsl:template match='htmlDescription//text()'> 
    <xsl:value-of select='.'/> 
</xsl:template> 

<xsl:template match='htmlDescription//*'> 
    <xsl:element name='{local-name()}'> 
     <xsl:copy-of select='@*'/> 
     <xsl:apply-templates select='node()'/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match='person'> 
    <td> 
     <xsl:apply-templates select='htmlDescription/node()'/> 
    </td> 
</xsl:template> 

</xsl:stylesheet> 
+0

+1だけの正解です。 –

+0

合意。これは正しいです –

0
<xsl:template match="person"> 
    <xsl:element name="td"> 
     <xsl:value-of select="htmlDescription"/> 
    </xsl:element> 
</xsl:template> 
+0

問題は、次のようないくつかの文字をエスケープtranformerことXLSTです"<"そして最後に私のhtml出力で私は得る: ​​ 非常に素敵人の insted **非常に** **素敵**その出力は多分あなたのウェブブラウザはしかしHTMLとしてページを解釈することはありません、正しいHTMLである –

+0

人が、XMLなど。 XSLTスクリプトに ''を追加してみてください。それができない場合は、XSLT Transformerとそれが使用されている環境に応じて、さらに行うべきことがあるかもしれません。 – smerlin

0
<xsl:template match="person"> 
    <tr> 
     <td> 
      <xsl:value-of select="name"/> 
     </td> 
     <td> 
      <xsl:value-of select="htmlDescription"/> 
     </td> 
    </tr> 
</xsl:template> 
1

はい、それは可能です。この例では、私の作品:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="person" > 
    <xsl:element name="td"> 
     <xsl:copy-of select="htmlDescription/*" /> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 
+0

パーフェクト "copy-of"が鍵でした。どうもありがとう! –

+1

@VoodooRider: 'htmlDescription/*'は ''の中の要素にしかマッチしませんが、テキストノードは含まれません( 'A'と 'person'はコピーされません)。私の答えを見てください。 – Saxoier