我有这个XML文件:
<?xml version="1.0"?>
<xi:include href="http://www.w3schools.com/dom/books.xml"
xmlns:xi="http://www.w3.org/2003/XInclude"/>
Run Code Online (Sandbox Code Playgroud)
我希望它在http://www.w3schools.com/dom/books.xml处理时应该导致引用的远程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>
Run Code Online (Sandbox Code Playgroud)
在XSL转换之后,我希望从引用的XML文件中获取带有标题节点的XML输出.
然而它没有发生,转换只是产生了一个空文件.我怀疑XInclude没有执行指令.
那么如果可能的话,如何在Xincluded XML文件上应用XSLT?
在评论中,OP已经要求在不同的源位置将xml文档的图像复制到单个输出目录中,然后在这里进行参考.
这个模板..
<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>
Run Code Online (Sandbox Code Playgroud)
...匹配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>
Run Code Online (Sandbox Code Playgroud)
...为纯文本做了类似的事情.注意@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>
Run Code Online (Sandbox Code Playgroud)
...处理我们可以打开文档的情况(可能文件引用被破坏),xi:include节点给我们一些后备内容.