如何使用Groovy删除特定的XML标记和空的父标记?

eimct9ow  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(125)

我想使用Groovy从XML文档中删除包含确切数字'123456789'的标记及其父标记。我该怎么做?

<CdtrAcct>
      <Id>
        <Othr>
          <Id>123456789</Id>
        </Othr>
      </Id>
    </CdtrAcct>

期望移除此特定标记及其空父标记

sxpgvts3

sxpgvts31#

Transformimg Xml可以通过xslt来完成。有关如何在groovy中使用Xslt,请参阅this url
您的问题所需的xslt如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
  
  <!--  xsl:mode on-no-match="shallow-copy" means copy everything that does not matches a template rule -->
  <xsl:mode on-no-match="shallow-copy"/>
  
  <!--  this match on the CdtrAcct[Id/Othr/Id/text()='123456789'] will catch your rule and because there is no output, the whole structure will be skipped -->
  <xsl:template match="CdtrAcct[Id/Othr/Id/text()='123456789']"/>
  
</xsl:stylesheet>

The latest versions of Saxon-HE for Java can be found here.

相关问题