2

如何在一个文件中加入多个 3 个 xml?

在 XML 文件中:01.xml

 <xmlResponse>
        <Person>
            <FirstName>FirstName_1</FirstName>
            <LastName>LastName_1</LastName>
        </Person>
        <Person>
            <FirstName>FirstName_2</FirstName>
            <LastName>LastName_2</LastName>
        </Person>
    </xmlResponse>

在 XML 文件中:02.xml

<xmlResponse>
    <Person>
        <FirstName>FirstName_2</FirstName>
        <LastName>LastName_2</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_3</FirstName>
        <LastName>LastName_3</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_4</FirstName>
        <LastName>LastName_4</LastName>
    </Person>
</xmlResponse>

在 XML 文件中:03.xml

<xmlResponse>
    <Person>
        <FirstName>FirstName_5</FirstName>
        <LastName>LastName_5</LastName>
    </Person>
</xmlResponse>

我需要如下输出(01.xml + 02.xml + 03.XML)

<xmlResponse>
    <Person>
        <FirstName>FirstName_1</FirstName>
        <LastName>LastName_1</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_2</FirstName>
        <LastName>LastName_2</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_2</FirstName>
        <LastName>LastName_2</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_3</FirstName>
        <LastName>LastName_3</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_4</FirstName>
        <LastName>LastName_4</LastName>
    </Person>
    <Person>
        <FirstName>FirstName_5</FirstName>
        <LastName>LastName_5</LastName>
    </Person>
</xmlResponse> 

希望你的回应,tks...

4

2 回答 2

0

试试这个 XSLT 2.0 样式表...

<?xml version="1.0"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="2.0"
  exclude-result-prefixes="xsl xs fn">

<xsl:output indent="yes" encoding="UTF-8" />
<xsl:param name="doc2" /> <!-- File 02.xml -->
<xsl:param name="doc3" /> <!-- File 03.xml -->
<xsl:variable name="doc2-doc" select="document($doc2)" />
<xsl:variable name="doc3-doc" select="document($doc3)" />

<xsl:template  match="/">
 <xmlResponse>
    <xsl:apply-templates />
    <xsl:apply-templates select="$doc2-doc/*" />
    <xsl:apply-templates select="$doc3-doc/*" />
 </xmlResponse>
</xsl:template>

<!-- Identity transform follows. -->
<xsl:template match="element()">
  <xsl:copy>
    <xsl:apply-templates select="@*,node()"/>
   </xsl:copy>
</xsl:template>
<xsl:template match="attribute()|text()|comment()|processing-instruction()">
  <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

对于转换,输入文档将是您的第一个文档(文件 01.xml),另外两个将作为转换参数传入。

如果您仅限于 XSLT 1.0,则需要进行一些修改,但应该不会太难。

于 2012-04-30T16:03:17.730 回答
0

您可以使用 XSLT,但在 ASP Classic(或实际上其他任何东西)下执行此任务时它会显得过大。

(假设您没有错误标记您的问题)

Function GetDom(pathToFile)
    Set GetDom = CreateObject("MSXML2.DOMDocument.3.0")
    GetDom.async = False
    GetDom.setProperty "SelectionLanguage", "XPath"
    GetDom.load pathToFile
End Function

Sub CopyElements(xmlPrimeRoot, xmlDonor)
    Dim elem
    For Each elem In xmlDonor.documentElement.selectNodes("*")
        xmlPrimeRoot.appendChild elem 
    Next  
End Sub

Dim xmlPrime: Set xmlPrime = GetDom(pathToFile1)

CopyElements xmlPrime.documentElement, GetDom(pathToFile2)
CopyElements xmlPrime.documentElement, GetDom(pathToFile3)

xmlPrime.save pathToDestinationFile

如果目标输出是 ASP 响应,那么最后一行可以是

xmlPrime.save Response
于 2012-05-01T10:11:10.070 回答