[下面,我想根据<table>..</table>
属性中的输入(在这种情况下为inputY
,在此情况下为inputX
)对id
节点进行排序,或者,如果仅发送一个输入,则删除<table>..</table>
。如何使用DOM解析器实现它。
<employees>
<table>
<employee>
<id attr="inputY">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<department>
<id>101</id>
<name>IT</name>
</department>
</id>
</employee>
</table>
<table>
<employee>
<id attr="inputX">
<firstName>Brian</firstName>
<lastName>Schultz</lastName>
<department>
<id>102</id>
<name>HR</name>
</department>
</id>
</employee>
</table>
<employees>
如果输入以inputX
和inputY
的顺序传递,则XML如下所示:
<employees>
<table>
<employee>
<id attr="inputX">
<firstName>Brian</firstName>
<lastName>Schultz</lastName>
<department>
<id>102</id>
<name>HR</name>
</department>
</id>
</employee>
</table>
<table>
<employee>
<id attr="inputY">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<department>
<id>101</id>
<name>IT</name>
</department>
</id>
</employee>
</table>
<employees>
您说过“使用Java”,但是到目前为止,做这种事情的最简单方法是使用XSLT,当然,可以很容易地从Java中调用它。
即使在XSLT 1.0中,也可以很容易地完成此操作:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<employees>
<xsl:for-each select="table">
<xsl:sort select="employee/id/@attr">
<xsl:copy-of select="."/>
</xsl:for-each>
</employees>
</xsl:template>
</xsl;transform>
恐怕我不明白您对这部分要求的意思:“或删除..如果仅发送了一个输入”。