如何使用XInclude在XML上应用XSLT

前端之家收集整理的这篇文章主要介绍了如何使用XInclude在XML上应用XSLT前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个 XML文件: @H_403_7@

@H_403_7@

<?xml version="1.0"?>
<xi:include href="http://www.w3schools.com/dom/books.xml" 
            xmlns:xi="http://www.w3.org/2003/XInclude"/>
@H_403_7@我希望在处理时它应该导致引用的远程XML文件http://www.w3schools.com/dom/books.xml.

@H_403_7@为此我创建了这个XSL文件

@H_403_7@

<?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>
@H_403_7@在XSL转换之后,我希望从引用的XML文件获取带有标题节点的XML输出.

@H_403_7@然而它没有发生,转换只是产生了一个空文件.我怀疑没有执行XInclude指令.

@H_403_7@那么如果可能的话,如何在Xincluded XML文件上应用XSLT?

解决方法

评论中,OP已经要求在 Copy xml document’s images in different source locations into single output directory处引用我的参考答案,所以在这里. @H_403_7@

@H_403_7@这个模板..

@H_403_7@

<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>
@H_403_7@…匹配xi:include符合所有这些要求的元素:

@H_403_7@>有一个href属性
>它指的是xml文档(而不是文本文档)
>该文件可以找到并且可以打开.

@H_403_7@满足这些条件时,打开XML文档并像处理它一样处理它,而不是xi:include节点.

@H_403_7@这个模板……

@H_403_7@

<xsl:template match="xi:include[@href][@parse='text'][fn:unparsed-text-available(@href)]">
 <xsl:apply-templates select="fn:unparsed-text(@href,@encoding)" />
</xsl:template>
@H_403_7@…为纯文本做了类似的事情.注意@parse的默认值是’xml’.注意我们甚至可以改变字符编码.我们的主要文件可能是UTF-8,但所包含的文件可能是UTF-16LE.

@H_403_7@最后这个模板……

@H_403_7@

<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>
@H_403_7@…处理我们可以打开文档的情况(可能文件引用被破坏),xi:include节点给我们一些后备内容.

猜你在找的XML相关文章