我在这里尝试完成的是移动一个元素(包括它的子节点),然后在该元素中添加一个子节点,反之亦然。看起来我一次只能做一件事。同时做这两件事是可能的吗?
下面是我的输入xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<box1>
<cd1>
<title>Title 1</title>
<artist>Bob Dylan</artist>
<year>1985</year>
</cd1>
<cd2>
<title>Title 2</title>
<artist>Bonnie Tyler</artist>
<year>1988</year>
</cd2>
</box1>
<box2>
<cd3>
<title>Title 3</title>
<artist>Metallica</artist>
</cd3>
</box2>
</catalog>我希望得到这样的输出
<catalog>
<box1>
<cd1>
<title>Title 1</title>
<artist>Bob Dylan</artist>
<year>1985</year>
</cd1>
<cd2>
<title>Title 2</title>
<artist>Bonnie Tyler</artist>
<year>1988</year>
</cd2>
<cd3>
<title>Title 3</title>
<artist>Metallica</artist>
<year>1990</year>
</cd3>
</box1>如您所见,元素cd3移动了,并且还添加了的子节点。
这就是我所做的,它所做的一切只是移动元素,而不管我把代码放在什么顺序。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.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="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- add a child element -->
<xsl:template match="cd3">
<xsl:copy>
<xsl:apply-templates/>
<year>1990</year>
</xsl:copy>
</xsl:template>
<!-- move node -->
<xsl:template match="/catalog">
<xsl:copy>
<xsl:apply-templates />
<xsl:copy-of select="box2/cd3"/>
</xsl:copy>
</xsl:template>
<xsl:template match="box2"/>
</xsl:stylesheet>https://stackoverflow.com/questions/38527654
复制相似问题