2013-11-25 119 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