动态地使用XSLT包含XML文件 [英] Dynamically include XML files using XSLT

查看:222
本文介绍了动态地使用XSLT包含XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过以下方式将多个XML文件合并成一个:

I am trying to merge multiple XML files into one in the following manner:

说我有一个XML文件,名为 fruit.xml

Say I have an XML file, called fruit.xml:

<fruit>
    <apples>
        <include ref="apples.xml" />
    </apples>
    <bananas>
        <include ref="bananas.xml" />
    </bananas>
    <oranges>
        <include ref="oranges.xml" />
    </oranges>
</fruit>

以及从 fruit.xml引用的后续XML文件,例如 apples.xml

<fruit>
    <apples>
        <apple type="jonagold" color="red" />
        <... />
    </apples>
</fruit>

等等...我想将它们合并成1个XML文件,如下所示: / p>

and so on... I would like to merge these into 1 XML file, like such:

<fruit>
    <apples>
        <apple type="jonagold" color="red" />
        <... />
    </apples>
    <bananas>
        <banana type="chiquita" color="yellow" />
        <... />
    </bananas>
    <oranges>
        <orange type="some-orange-type" color="orange" />
        <... />
    </oranges>
</fruit>

我想确定小孩文件(如 apples.xml bananas.xml 等)动态地基于 ref 属性的值 元素 fruits.xml ,然后将它们包含在输出中。

I want to determine the "child" files (like apples.xml, bananas.xml, etc.) dynamically based on the values of the ref attributes in the <include> elements of fruits.xml and then include them in the output.

是否可以使用XSLT?

推荐答案

应该包含一个档案的争议:

If only the contend of an file should be included you can use:

<xsl:copy-of select="document(@ref)/fruit/*/*"/>

因此请尝试:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="include">
        <xsl:copy-of select="document(@ref)/fruit/*/*"/>
    </xsl:template>
</xsl:stylesheet>

这篇关于动态地使用XSLT包含XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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