【发布时间】:2019-06-28 13:51:32
【问题描述】:
我有一个 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>
所以,本质上,每个
总是会有匹配数量的
我已经尝试过这个 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>
但它只复制第一个
【问题讨论】: