Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSLT将数据从一个XML文档复制到另一个XML文档_Xslt_Transformation - Fatal编程技术网

使用XSLT将数据从一个XML文档复制到另一个XML文档

使用XSLT将数据从一个XML文档复制到另一个XML文档,xslt,transformation,Xslt,Transformation,我必须将节点元素的数据从file1.xml复制到file2.xml。 file1.xml 但是上面的代码正在用file1.xml内容覆盖file2内容。这只是示例XML。在实际情况中,我们不知道节点的名称和xml文件的层次结构。但无论文件和场景的结构是什么,都将是完全相同的。我是XSLT新手,不确定这种方法是否正确。是否真的有可能通过XSLT获得结果。我发布的解决方案是在考虑以下几点的情况下编写的: 唯一需要合并的是属性。文本和元素节点在file1.xml中显示时被复制 @id属性在file2.

我必须将节点元素的数据从file1.xml复制到file2.xml。 file1.xml


但是上面的代码正在用file1.xml内容覆盖file2内容。这只是示例XML。在实际情况中,我们不知道节点的名称和xml文件的层次结构。但无论文件和场景的结构是什么,都将是完全相同的。我是XSLT新手,不确定这种方法是否正确。是否真的有可能通过XSLT获得结果。

我发布的解决方案是在考虑以下几点的情况下编写的:

唯一需要合并的是属性。文本和元素节点在file1.xml中显示时被复制

@id属性在file2.xml中没有顺序编号,因此file2.xml中的@id可以是例如121 432 233 12 944,而不是1 2 3 4 5。如果是后者,则不需要file2.xml来生成所需的输出

文档功能可用于访问与当前文件不同的文件。如果XslCompiledTransform在使用document函数时出现错误,我建议遵循以下步骤。我使用的是不同的XSLT处理器xsltproc,它工作正常

此解决方案基于保留对外部文件的引用,因此每次处理file1.xml中的元素时,引用都会移动到file2.xml中的同一元素。这是因为根据问题,两个文件都呈现相同的元素层次结构

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

    <xsl:output method="xml" indent="no"/>

    <!-- Match the document node as an entry point for matching the files -->
    <xsl:template match="/">
        <xsl:apply-templates select="node()">
            <xsl:with-param name="doc-context" select="document('file2.xml')/node()" />
        </xsl:apply-templates>
    </xsl:template>

    <!-- In this template we copy the elements and text nodes from file1.xml and
         we merge the attributes from file2.xml with the attributes in file1.xml -->
    <xsl:template match="node()">
        <!-- We use this parameter to keep track of where we are in file2.xml by
             mimicking the operations that we do in the current file. So we are at
             the same position in both files at the same time. -->
        <xsl:param name="doc-context" />

        <!-- Obtain current position in file1.xml so we know where to look in file2.xml -->
        <xsl:variable name="position" select="position()" />

        <!-- Copy the element node from the current file (file1.xml) -->
        <xsl:copy>
            <!-- Merge attributes from file1.xml with attributes from file2.xml -->
            <xsl:copy-of select="@*|$doc-context[position() = $position]/@*" />
            <!-- Copy text nodes and process children -->
            <xsl:apply-templates select="node()">
                <xsl:with-param name="doc-context" select="$doc-context/node()" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

我发布的解决方案是基于以下几点编写的:

唯一需要合并的是属性。文本和元素节点在file1.xml中显示时被复制

@id属性在file2.xml中没有顺序编号,因此file2.xml中的@id可以是例如121 432 233 12 944,而不是1 2 3 4 5。如果是后者,则不需要file2.xml来生成所需的输出

文档功能可用于访问与当前文件不同的文件。如果XslCompiledTransform在使用document函数时出现错误,我建议遵循以下步骤。我使用的是不同的XSLT处理器xsltproc,它工作正常

此解决方案基于保留对外部文件的引用,因此每次处理file1.xml中的元素时,引用都会移动到file2.xml中的同一元素。这是因为根据问题,两个文件都呈现相同的元素层次结构

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

    <xsl:output method="xml" indent="no"/>

    <!-- Match the document node as an entry point for matching the files -->
    <xsl:template match="/">
        <xsl:apply-templates select="node()">
            <xsl:with-param name="doc-context" select="document('file2.xml')/node()" />
        </xsl:apply-templates>
    </xsl:template>

    <!-- In this template we copy the elements and text nodes from file1.xml and
         we merge the attributes from file2.xml with the attributes in file1.xml -->
    <xsl:template match="node()">
        <!-- We use this parameter to keep track of where we are in file2.xml by
             mimicking the operations that we do in the current file. So we are at
             the same position in both files at the same time. -->
        <xsl:param name="doc-context" />

        <!-- Obtain current position in file1.xml so we know where to look in file2.xml -->
        <xsl:variable name="position" select="position()" />

        <!-- Copy the element node from the current file (file1.xml) -->
        <xsl:copy>
            <!-- Merge attributes from file1.xml with attributes from file2.xml -->
            <xsl:copy-of select="@*|$doc-context[position() = $position]/@*" />
            <!-- Copy text nodes and process children -->
            <xsl:apply-templates select="node()">
                <xsl:with-param name="doc-context" select="$doc-context/node()" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

这里的可能重复我不知道来自服务的XML结构。我猜在实际情况下,file2.XML中的ID不会是连续的,不是吗?我的意思是你会发现像。。。顺便说一句,您是否尝试使用document函数?这里的可能重复我不知道来自服务的XML结构。我猜在实际情况下,file2.XML中的ID不会是连续的,不是吗?我的意思是你会发现像。。。顺便说一句,您是否尝试使用文档功能?非常感谢,这个解决方案完美地解决了这个问题。我想知道,如果两个xml文件都不是物理文件,我们如何实现相同的解决方案。如果这些都是在数据库的代码中生成的。很高兴在XSLT中看到一些注释。非常感谢,这个解决方案完美地解决了这个问题。我想知道,如果两个xml文件都不是物理文件,我们如何实现相同的解决方案。如果这些是在数据库的代码中生成的。很高兴在XSLT中看到一些注释。
<?xml version="1.0" encoding="utf-8" ?>
<root id="1">
  <header id="2">
    <AsofDate id="3">31-Dec-2012</AsofDate>
    <FundName id="4">This is Sample Fund</FundName>
    <Description id="5">This is test description</Description>
  </header>
</root>
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
    XslCompiledTransform tf = new XslCompiledTransform();
    tf.Load("TranFile.xsl");

    tf.Transform("file1.xml", "file2.xml");
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="no"/>

    <!-- Match the document node as an entry point for matching the files -->
    <xsl:template match="/">
        <xsl:apply-templates select="node()">
            <xsl:with-param name="doc-context" select="document('file2.xml')/node()" />
        </xsl:apply-templates>
    </xsl:template>

    <!-- In this template we copy the elements and text nodes from file1.xml and
         we merge the attributes from file2.xml with the attributes in file1.xml -->
    <xsl:template match="node()">
        <!-- We use this parameter to keep track of where we are in file2.xml by
             mimicking the operations that we do in the current file. So we are at
             the same position in both files at the same time. -->
        <xsl:param name="doc-context" />

        <!-- Obtain current position in file1.xml so we know where to look in file2.xml -->
        <xsl:variable name="position" select="position()" />

        <!-- Copy the element node from the current file (file1.xml) -->
        <xsl:copy>
            <!-- Merge attributes from file1.xml with attributes from file2.xml -->
            <xsl:copy-of select="@*|$doc-context[position() = $position]/@*" />
            <!-- Copy text nodes and process children -->
            <xsl:apply-templates select="node()">
                <xsl:with-param name="doc-context" select="$doc-context/node()" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>