How to apply XSLT on XML with just XInclude

一世执手 提交于 2019-12-05 08:46:15
Sean B. Durkin

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 processing, like XSD validation, is something that happens if you ask for it, and not otherwise. The way you ask for it depends on the environment you are in. For example, the Xerces parser has options to do XInclude processing.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!