XSLT 复制父节点中的子节点



我需要将子元素复制到父元素中。

输入

<csv>
<row>
<stuff>a</stuff>
<more>1</more>
<evenmore>123</evenmore>
<row>
<other>1345</other>
<stuff>dga</stuff>
</row>
</row>
<row>
<stuff>b</stuff>
<more>2</more>
<evenmore>456</evenmore>
<row>
<other>4576</other>
<stuff>jzj</stuff>
</row>
</row>
</csv>

期望的输出

<csv>
<row>
<stuff>a</stuff>
<more>1</more>
<evenmore>123</evenmore>
<other>1345</other>
<stuff>dga</stuff>
</row>
<row>
<stuff>b</stuff>
<more>2</more>
<evenmore>456</evenmore>
<other>4576</other>
<stuff>jzj</stuff>
</row>
</csv>

我尝试过(输出与输入相同(:

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

我肯定会在这里错过一些非常简单的东西。子元素与父元素同名应该不是问题吗?

您确实需要第二个模板仅匹配子模板row,而不是父模板。然后你可以选择它的子级,但不能复制它本身

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="row/row">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

最新更新