当存在相对包含其他样式表的样式表时,我在 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 处理,以便样式表可以在其本地路径中包含另一个样式表?
最佳答案
我也遇到过这个问题。最后我写了 SWXSLTransform
我的 SWXMLMapping 框架中包含的类:https://github.com/oriontransfer/SWXMLMapping
主要问题是在幕后,-[NSXMLDocument objectByApplyingXSLTAtURL:...]
实际上没有将 URL 传递给 libxslt
.相反,它加载指向该 URL 的数据,并将数据提供给 libxslt
。没有任何基础 URI。因此,当前工作目录成为基本 URI - 您可以使用 <xsl:include href="...">
其中 href 是相对于当前工作目录的……不是很有用!
据我所知,NSXMLDocument
界面完全坏了,似乎没有任何办法可以修复它。我已提交错误报告。
关于xml - NSXMLDocument objectByApplyingXSLT with XSL Include,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2670941/