如何仅使用 XInclude 在 XML 上应用 XSLT [英] How to apply XSLT on XML with just XInclude

查看:18
本文介绍了如何仅使用 XInclude 在 XML 上应用 XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 XML 文件:

<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 文件:

<xsl:output method="xml"/><xsl:template match="*"><xsl:copy-of select="//book/title"/></xsl:模板></xsl:stylesheet>

在 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:模板>

... 匹配满足所有这些要求的 xi:include 元素:

  1. 有一个 href 属性
  2. 它指的是一个 xml 文档(相对于文本文档)
  3. 并且可以找到该文档并且可以打开该文档.

当满足这些条件时,打开 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:模板>

... 对纯文本包含做类似的事情.请注意,@parse 的默认值为xml".请注意,我们甚至可以更改字符编码.我们的主文档可能是 UTF-8,但包含的文档很可能是 UTF-16LE,例如.

最后是这个模板...

<xsl:template match="xi:include[@href][@parse=('text','xml') or not(@parse)][not(fn:unparsed-text-可用(@href))][xi:fallback]"><xsl:apply-templates select="xi:fallback/text()"/></xsl:模板>

... 处理我们可以打开文档的情况(可能文件引用被破坏了),并且 xi:include 节点为我们提供了一些后备内容.

I have this XML file:

<?xml version="1.0"?>
<xi:include href="http://www.w3schools.com/dom/books.xml" 
            xmlns:xi="http://www.w3.org/2003/XInclude"/>

and I expected that it should result in referenced remote XML file http://www.w3schools.com/dom/books.xml at the time of processing.

For that purpose I created this XSL file:

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

which after XSL transform, I expected to get XML output with title nodes from referenced XML file.

However it didn't happen, transformation just produced an empty file. I suspect that XInclude instruction wasn't performed.

So how can I apply XSLT on Xincluded XML file, if possible?

解决方案

In comments, the OP has asked for a walk-through of my referenced answer at Copy xml document's images in different source locations into single output directory , so here it is.

This template..

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

... matches the xi:include elements that meet all these requirements:

  1. Has a href attribute
  2. It refers to an xml document (as opposed to a text document)
  3. And that document can be found and is openable.

When these conditions are met, open the XML document and process it just as if it was in place here, instead of the xi:include node.

This template ...

<xsl:template match="xi:include[@href][@parse='text'][fn:unparsed-text-available(@href)]">
 <xsl:apply-templates select="fn:unparsed-text(@href,@encoding)" />
</xsl:template>

... does a similar thing for pure text includes. Note the default value for @parse is 'xml'. Note we can even change character encoding. Our main document could be in UTF-8, but the included document may well be UTF-16LE for example.

And finally this template...

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

... handles the cases where we can open the document (maybe the file reference is broken), and the xi:include node gives us some fall-back content.

这篇关于如何仅使用 XInclude 在 XML 上应用 XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆