加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用XInclude在XML上应用XSLT

发布时间:2020-12-16 23:17:17 所属栏目:百科 来源:网络整理
导读:我有这个 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文件:

<?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?

解决方法

在评论中,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符合所有这些要求的元素:

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

满足这些条件时,打开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节点给我们一些后备内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读