用于将内容从同名元素复制到同级节点的XSLT

用于将内容从同名元素复制到同级节点的XSLT,xslt,xpath,Xslt,Xpath,我有一个XML文件,需要在其中复制每个节点的值,并将其插入到相应的节点覆盖中,其中不是空的 XML文件具有以下结构: <Message> <Id>VARIABLE_1</Id> <Code>VAR</Code> <Source>TEXT 1</Source> <Source>TEXT 2</Source> <Source/> &l

我有一个XML文件,需要在其中复制每个节点的值,并将其插入到相应的节点覆盖中,其中不是空的

XML文件具有以下结构:

<Message>
    <Id>VARIABLE_1</Id>
    <Code>VAR</Code>
    <Source>TEXT 1</Source>
    <Source>TEXT 2</Source>
    <Source/>
    <Source>TEXT 3</Source>
    <Comment/>
    <Target>SOMETHING 1</Target>
    <Target>SOMETHING 2</Target>
    <Target/>
    <Target>SOMETHING 3</Target>
    <Comment/>
</Message>
我需要将其转化为:

<Message>
    <Id>VARIABLE_1</Id>
    <Code>VAR</Code>
    <Source>TEXT 1</Source>
    <Source>TEXT 2</Source>
    <Source/>
    <Source>TEXT 3</Source>
    <Comment/>
    <Target>TEXT 1</Target>
    <Target>TEXT 2</Target>
    <Target/>
    <Target>TEXT 3</Target>
    <Comment/>
</Message>
因此,本质上,每个节点都将继承相应的前面节点的值

总是会有一个匹配数量的节点,但是有些父节点可能只包含一个节点,有些可能包含最多5个节点,在本例中,每个节点有3个

我尝试过这个XSLT:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="//Target">
        <xsl:copy>
            <xsl:value-of select="//Target/preceding-sibling::Source"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
但它只复制第一个节点,并将其插入到随后的每个节点中。我需要按顺序复制它们-第一个节点被复制到第一个节点,第二个节点被复制到第二个节点,等等

XSLT1.0

或者,如果您愿意:

<xsl:template match="Target">
    <xsl:copy>
        <xsl:value-of select="../Source[count(current()/preceding-sibling::Target) + 1]"/>
    </xsl:copy>
</xsl:template>

以下是一种简单的方法:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <!-- Solution -->
    <xsl:template match="Target">        
        <xsl:copy>
            <xsl:value-of select="../Source[substring-after(current(),' ') = substring-after(.,' ')]"/>
        </xsl:copy>        
    </xsl:template>

</xsl:stylesheet>

输出如预期。

谢谢!这很有效。是否有任何方法可以将节点的处理仅限于那些具有VAR子元素的父元素?您可以使用.P.S将处理限制为具有值为VAR的同级代码元素的目标元素。如果你的问题得到了回答,请通过接受答案来结束它。
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <!-- Solution -->
    <xsl:template match="Target">        
        <xsl:copy>
            <xsl:value-of select="../Source[substring-after(current(),' ') = substring-after(.,' ')]"/>
        </xsl:copy>        
    </xsl:template>

</xsl:stylesheet>