我想使用XSLT修改以下xml:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<folder>
<CRDATTIM>2012-08-31-08.26.11.805400</CRDATTIM>
<RECORDCD>F</RECORDCD>
</folder>
<case>
<CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<issue>
<KEY>2014-03-26-05.22.22.193840T01</KEY>
<PRTY>999</PRTY>
</issue>
</case>
<folder>
<CRDATTIM>2012-11-06-23.57.08.089400</CRDATTIM>
<RECORDCD>F</RECORDCD>
</folder>
<case>
<CRDATTIM>2014-04-29-06.58.32.992840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<issue>
<KEY>2014-04-29-06.58.31.305840T01</KEY>
<PRTY>999</PRTY>
</issue>
</case>
</response>
现在,我想向根节点<sort>
添加一个新节点<response>
,以便节点<sort>
将包含两个子节点<field />
和<ascending />
。但是,对于上述xml中的每个案例,应将<field />
和<ascending />
两个标签添加到<sort>
节点。例如,在上面的xml中,有两种情况( <case>
)。因此,节点<sort>
应该有两个<field />
标记和<ascending />
标记。最终,我的最终xml应该如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<folder>
<CRDATTIM>2012-08-31-08.26.11.805400</CRDATTIM>
<RECORDCD>F</RECORDCD>
</folder>
<case>
<CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<issue>
<KEY>2014-03-26-05.22.22.193840T01</KEY>
<PRTY>999</PRTY>
</issue>
</case>
<folder>
<CRDATTIM>2012-11-06-23.57.08.089400</CRDATTIM>
<RECORDCD>F</RECORDCD>
</folder>
<case>
<CRDATTIM>2014-04-29-06.58.32.992840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<issue>
<KEY>2014-04-29-06.58.31.305840T01</KEY>
<PRTY>999</PRTY>
</issue>
</case>
<sort>
<field />
<ascending />
<field />
<ascending />
<field />
</sort>
</response>
请分享您对如何实现所需功能的想法。如果我的问题不明确,请告诉我。提前谢谢。
答案 0 :(得分:2)
您可以使用标识模板复制原始XML并在末尾放置<sort>
元素。请参阅下一个教程:http://www.xmlplease.com/xsltidentity
这是一个XSLT示例:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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="response">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<sort>
<xsl:for-each select="case">
<field />
<ascending />
</xsl:for-each>
</sort>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>