使用 XSLT 合并两个 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19021205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:11:31  来源:igfitidea点击:

Merging two XML files using XSLT

xmlxsltxpath

提问by user1457881

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

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

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

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

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

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

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

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

回答by hielsnoppe

Given the following input files:

给定以下输入文件:

ApplicationData.xml

应用数据.xml

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

MetricList.xml

指标列表.xml

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

AssessmentInput.xml

评估输入文件

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

the following transformation merge.xslapplied to AssessmentInput.xml

以下转换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>

produces the correct output of

产生正确的输出

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

回答by kaushik0033

You should be replace your line as below:-

您应该按如下方式替换您的线路:-

<xsl:param name="ApplicationData" select="/"/>
  <xsl:param name="MetricList"/>

with this below line one:

使用下面的第一行:

   <xsl:variable name="Application_Data" select="document('Application_Data.xml')/ApplicationData"/>
 <xsl:variable name="'Metric_List" select="document('Application_Data.xml')/MetricList"/>

i think this may help you..

我想这可能会帮助你..