2013-11-25 73 views
0

我有一个标签的xml,其中包含一个属性,其中的内容为html。我需要将此html转换为xsl-fo。这里是我的XSLT代码:Xml内容没有被模板识别

<xsl:template match ="rtf"> 
    <fo:block-container> 
     <fo:block> 
     <xsl:call-template name ="ConvertHtmlToXslfo"> 
      <xsl:with-param name ="content"> 
      <xsl:value-of select ="@rtfAsHtml" disable-output-escaping ="yes"/> 
      </xsl:with-param> 
     </xsl:call-template> 
     </fo:block> 
    </fo:block-container> 
    </xsl:template> 

    <xsl:template name="ConvertHtmlToXslfo"> 
    <xsl:param name ="content"></xsl:param> 
    <fo:block-container> 

     <xsl:apply-templates select="msxsl:node-set($content)"/> <!--here is the problem--> 

    </fo:block-container> 
    </xsl:template> 

    <xsl:template match ="div"> 
    <fo:block-container> 
     <!--more code here--> 
    </fo:block-container> 
    </xsl:template> 

    <xsl:template match ="p"> 
    <fo:block> 
     <!--more code here--> 
    </fo:block> 
    </xsl:template> 

    <xsl:template match ="span"> 
    <fo:inline> 
     <!--more code here--> 
    </fo:inline> 
    </xsl:template> 

但是有一个问题,当呼叫应用的模板在这个HTML内容。相关的模板不能识别它。

这里是XML标记与的Html属性在里面:

<rtf rtfAsHtml="&lt;div &gt;&lt;p &gt;&lt;span&gt;Hi!&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;"/> 

任何想法如何将这个HTML标记转换为XSL-FO?

谢谢!

+0

您的输入文件是否有任何名称空间声明?另外,如果你能够匹配“rtf”作为文档节点,在我看来你的输入是RTF而不是HTML。 –

+0

@MathiasMüller。不,我的输入没有任何命名空间。而且,我的输入是一个包含从rtf转换而来的html的xml标签。但它是HTML。 – Jacob

回答

1

问题是您的xml/html内容已被转义。您尝试应用禁用输出转义,但只适用于写入输出文件或流。所以,你实际上只是将未转义的属性内容提交给你的模板,这确实没有太大的作用。

不知道你使用的是什么样的XSLT解析器的,但如果你使用撒克逊人,你可以尝试申请撒克逊:解析:

http://saxonica.com/documentation9.4-demo/html/extensions/functions/parse.html

这并不需要属性的内容很好-formed。如果没有,你可以尝试:

http://saxonica.com/documentation9.4-demo/html/extensions/functions/parse-html.html

HTH!

+0

谢谢。你是对的。但我正在使用Microsoft XslCompiledTransform。 – Jacob

+0

@jacob不幸的是,.Net中的那些解析函数似乎并没有相同之处。当然,您可以恢复到Saxon.Net。其他的选择是创建你自己的XPath扩展函数。我发现这在stackoverflow显示示例如何:http://stackoverflow.com/questions/2320505/using-ms-xpath-functions-inside-xpathexpression – grtjn

+0

这可能是可以使用类似于这个答案,其中“解析器转义XML“在XSLT中实现:http://stackoverflow.com/a/20150995/407651。 – mzjn