我有以下xml
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="somename">
<node1></node1>
<node2></node2>
</lst>
<result name="somename" count="5">
<doc>
<str name="NodeA">ValueA</str>
<str name="NodeB">ValueB</str>
<str name="NodeC">ValueC</str>
</doc>
<doc>
<str name="NodeA">ValueD</str>
<str name="NodeB">ValueE</str>
<str name="NodeC">ValueF</str>
</doc>
</result>
</response>
我想转换成
<?xml version="1.0" encoding="UTF-8"?>
<response>
<doc>
<NodeA>ValueA</NodeA>
<NodeB>ValueB</NodeB>
<NodeC>ValueC</NodeC>
</doc>
<doc>
<NodeA>ValueD</NodeA>
<NodeB>ValueE</NodeB>
<NodeC>ValueF</NodeC>
</doc>
</response>
如您所见,lst 节点已完全删除,属性值现在已成为节点。
首先,我使用此 xslt 代码删除了 lst 节点。
<?xml version="1.0" encoding="utf-8"?>
<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:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="lst"/>
</xsl:stylesheet>
这给了我这个
<?xml version="1.0" encoding="UTF-8"?>
<response>
<result name="somename" count="5">
<doc>
<str name="NodeA">ValueA</str>
<str name="NodeB">ValueB</str>
<str name="NodeC">ValueC</str>
</doc>
<doc>
<str name="NodeA">ValueD</str>
<str name="NodeB">ValueE</str>
<str name="NodeC">ValueF</str>
</doc>
</result>
</response>
然后使用链接中的这个 xslt [链接] Convert attribute value into element
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="response/result/doc">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="token">
<xsl:element name="{@name}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但这没有帮助。它给了我这个。
<?xml version="1.0" encoding="utf-8"?>
<doc>ValueAValueBValueC</doc>
<doc>ValueDValueEValueF</doc>
请帮助我完成将属性值转换为节点的第二部分。是否有可能让一个 xslt 做这两件事?