使用XSLT删除xml标记

aza*_*oth 5 xml xslt

我有以下xml文件:

<xfa:data>
  <form1>
    <Page1>
    <Page2>
    <contractInfo> ... </contractInfo>
    <paymentInfo> ... </paymentInfo>
  </form1>
  <commercialType> .... </commercialType>
  <userList> ... </userList>
  <officesList> ... </officesList>
  <commercialType> .... </commercialType>
  <userList> ... </userList>
  <officesList> ... </officesList>
  <commercialType> .... </commercialType>
  <userList> ... </userList>
  <officesList> ... </officesList>
</xfa:data>
Run Code Online (Sandbox Code Playgroud)

我想删除commercialType,userList和officesList节点的每个发生,所以我的输出将是:

<xfa:data>
  <form1>
    <Page1>
    <Page2>
    <contractInfo> ... </contractInfo>
    <paymentInfo> ... </paymentInfo>
  </form1>
</xfa:data>
Run Code Online (Sandbox Code Playgroud)

我怎么能用XSLT做到这一点?

谢谢

Dim*_*hev 18

这种转变产生了预期的结果:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="commercialType|userList|officesList"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

  • @Rhyous,'xmlns ="..."'是默认命名空间的声明.xmlns:somePreffix ="..."是非默认命名空间的声明.你的问题是FAQ.搜索"默认命名空间和XPath",你会发现很多很好的解释.是的,在源XML文档中使用名称空间的情况下,需要修改此答案.但是,当前的问题没有这样的源XML文档.因此,再一次,请阅读其他问题,关于命名空间的主题有很多. (3认同)