2011-09-09 46 views
0

我有下面的XML:XSLT注入HTML

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

我会莫名其妙地喜欢变换生产使用这种XML XSLT的HTML如

一个非常不错

这是可能的使用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

问题是,XLST变压逃避一些字符,如“<”最后在我的html输出中我得到: ​​ A 不错人 A的insted的**非常** ** **好的人 –

+0

该输出是正确的HTML,也许你的网页浏览器不解释,虽然该页面为HTML,而是作为XML。尝试在您的XSLT脚本中添加''(几乎在开头)。如果没有,可能还有其他的事情要做,具体取决于您的XSLT变换器及其使用环境。 – 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

完美的“复制”是关键。非常感谢! –

+1

@VoodooRider:'htmlDescription/*'只匹配''内的元素,但不包含textnode(即不复制'A'和'person')。看到我的答案。 – Saxoier