我有这个XML文件:
<?xml version="1.0"?>
<xi:include href="http://www.w3schools.com/dom/books.xml"
xmlns:xi="http://www.w3.org/2003/XInclude"/>
我希望它在处理时应该导致引用的远程XML文件http://www.w3schools.com/dom/books.xml
。
为此,我创建了这个XSL文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:copy-of select="//book/title"/>
</xsl:template>
</xsl:stylesheet>
在XSL转换之后,我希望从引用的XML文件中获取带有标题节点的XML输出。
然而它没有发生,转换只是产生了一个空文件。我怀疑没有执行XInclude
指令。
那么如果可能的话,如何在Xincluded XML文件上应用XSLT?
答案 0 :(得分:8)
在评论中,OP要求在Copy xml document's images in different source locations into single output directory处逐步介绍我的参考答案,所以就是这样。
此模板..
<xsl:template match="xi:include[@href][@parse='xml' or not(@parse)][fn:unparsed-text-available(@href)]">
<xsl:apply-templates select="fn:document(@href)" />
</xsl:template>
...匹配满足所有这些要求的xi:include元素:
当满足这些条件时,打开XML文档并像处理它一样处理它,而不是xi:include节点。
此模板......
<xsl:template match="xi:include[@href][@parse='text'][fn:unparsed-text-available(@href)]">
<xsl:apply-templates select="fn:unparsed-text(@href,@encoding)" />
</xsl:template>
...对于纯文本包含了类似的内容。注意@parse的默认值是'xml'。注意我们甚至可以改变字符编码。我们的主要文件可能是UTF-8,但所包含的文件可能是UTF-16LE。
最后这个模板......
<xsl:template match="xi:include[@href][@parse=('text','xml') or not(@parse)][not(fn:unparsed-text-available(@href))][xi:fallback]">
<xsl:apply-templates select="xi:fallback/text()" />
</xsl:template>
...处理我们可以打开文档的情况(可能文件引用被破坏),xi:include节点给我们一些后备内容。
答案 1 :(得分:0)
XInclude处理,就像XSD验证一样,如果你要求它就会发生,而不会发生。您要求它的方式取决于您所处的环境。例如,Xerces解析器具有执行XInclude处理的选项。