使用 XSLT 合并两个 XML 文件 [英] Merging two XML files using XSLT

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

问题描述

我有 2 个 xml 文件,我需要使用样式表将它们合并在一起

I have 2 xml files which I need to merge together using a style sheet

<AssessmentInput>
  <ApplicationData>...</AppicationData>
  <MetricList>...</MetricList>
</AssessmentInput>

一个是 ApplicationData,另一个是 MetricList.这是我所做的,但与它应该做的完全不同

One is ApplicationData and the other one is MetricList. here is what I have done but it is nothing close to what it should be

<?xml version="1.0" encoding="ascii"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="xsl exslt">
    <xsl:output omit-xml-declaration="yes" method="xml" encoding="UTF-8"/>
    <xsl:param name="ApplicationData" select="/"/>
    <xsl:param name="MetricList"/>
    <xsl:template match="/">
        <xsl:apply-templates select="$ApplicationData/ApplicationData"/>
    </xsl:template>
    <xsl:template match="ApplicationData">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

请帮帮我.我对 XSLT 没有任何经验.

Please help me. I don't have any experience with XSLT.

推荐答案

给定以下输入文件:

ApplicationData.xml

<?xml version="1.0" ?>
<ApplicationData>
    Whatever data you have in here.
</ApplicationData>

MetricList.xml

<?xml version="1.0" ?>
<MetricList>
    Whatever list you have in here.
</MetricList>

AssessmentInput.xml

<?xml version="1.0" ?>
<AssessmentInput />

以下转换 merge.xsl 应用于 AssessmentInput.xml

<?xml version="1.0" ?>
<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/AssessmentInput">
        <xsl:copy>
            <xsl:copy-of select="document('ApplicationData.xml')" />
            <xsl:copy-of select="document('MetricList.xml')" />
        </xsl:copy>
    </xsl:template>
</xsl:transform>

产生正确的输出

<?xml version="1.0" encoding="UTF-8"?>
<AssessmentInput>
    <ApplicationData>
        Whatever data you have in here.
    </ApplicationData>
    <MetricList>
        Whatever list you have in here.
    </MetricList>
</AssessmentInput>

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

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