2010-04-19 40 views
3

當我有一些樣式表相對包含其他樣式表時遇到了一些XSL處理問題。NSXMLDocument objectByApplyingXSLT與XSL包含

(XML文件可能無關緊要,但爲了完整起見,代碼位於底部)。

鑑於XML文件:

<?xml version="1.0" ?> 
<famous-persons> 
<persons category="medicine"> 
    <person> 
    <firstname> Edward </firstname> 
    <name>  Jenner </name> 
    </person> 
</persons> 
</famous-persons> 

和XSL文件:

<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
    <xsl:include href="included.xsl" /> 
</xsl:stylesheet> 

引用這個樣式表在同一目錄下名爲included.xsl:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
    <xsl:template match="/"> 
    <html><head><title>Sorting example</title></head><body> 
    </body></html> 
    </xsl:template> 
</xsl:stylesheet> 

如何我可以做到以下代碼片段:

NSError *lError = nil; 
NSXMLDocument *lDocument = [ [ NSXMLDocument alloc ] initWithContentsOfURL: 
             [ NSURL URLWithString: @"file:///pathto/data.xml" ] 
            options: 0 
            error: &lError ]; 

NSXMLDocument *lResult = [ lDocument objectByApplyingXSLTAtURL: [ NSURL URLWithString: @"file:///pathto/style.xsl" ] 
            arguments: nil 
            error: nil ]; 

不給我的錯誤:

I/O warning : failed to load external entity "included.xsl" 
compilation error: element include 
xsl:include : unable to load included.xsl 

我一直在嘗試各種選項。事先用NSXMLDocumentXInclude加載XML文檔似乎沒有幫助。指定要包含的XSL文件的絕對路徑完美無瑕。

是否有任何方法來進行XSL處理,以便樣式表可以在其本地路徑中包含其他樣式表?

回答

1

我也有這個問題。最後,我寫了包含在我的SWXMLMapping框架SWXSLTransform類:https://github.com/oriontransfer/SWXMLMapping

的主要問題是,在引擎蓋下,-[NSXMLDocument objectByApplyingXSLTAtURL:...]實際上是不及格的URL libxslt。相反,它會加載指向該URL的數據,並將數據提供給libxslt而不帶任何基本URI。因此,當前工作目錄將成爲基礎URI--您可以使用<xsl:include href="...">其中href與當前工作目錄相關......不是很有用!

據我所知,NSXMLDocument接口完全破損,似乎沒有任何解決方法。我已經提交了一個錯誤報告。