XSLT删除不需要的元素

Eld*_*dar 5 xslt xml-nil

我有XML

<getInquiryAboutListReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <inquiryAbouts>
        <inquiryAbout>
            <code>Code</code>
            <nameKk>Something</nameKk>
            <nameRu>Something</nameRu>
            <documents xsi:nil="true"/>
        </inquiryAbout>
    </inquiryAbouts>
</getInquiryAboutListReturn>
Run Code Online (Sandbox Code Playgroud)

我想用XSLT处理它以复制所有XML

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" />
    <xsl:template match="/">
        <xsl:copy-of select="//getInquiryAboutListReturn/inquiryAbouts"/>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

如果没有<documents xsi:nil="true"/>或没有xsi:nil ="true",我怎么能复制所有的XML ?

期望的输出XML

<getInquiryAboutListReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <inquiryAbouts>
        <inquiryAbout>
            <code>Code</code>
            <nameKk>Something</nameKk>
            <nameRu>Something</nameRu>
        </inquiryAbout>
    </inquiryAbouts>
</getInquiryAboutListReturn>
Run Code Online (Sandbox Code Playgroud)

ABa*_*ach 7

这个简单的XSLT:

<?xml version="1.0"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  version="1.0">

  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <!-- TEMPLATE #2 -->
  <xsl:template match="*[@xsi:nil = 'true']" />

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

...当应用于OP的源XML时:

<?xml version="1.0"?>
<getInquiryAboutListReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <inquiryAbouts>
    <inquiryAbout>
      <code>Code</code>
      <nameKk>Something</nameKk>
      <nameRu>Something</nameRu>
      <documents xsi:nil="true"/>
    </inquiryAbout>
  </inquiryAbouts>
</getInquiryAboutListReturn>
Run Code Online (Sandbox Code Playgroud)

...生成预期结果XML:

<?xml version="1.0"?>
<getInquiryAboutListReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <inquiryAbouts>
    <inquiryAbout>
      <code>Code</code>
      <nameKk>Something</nameKk>
      <nameRu>Something</nameRu>
    </inquiryAbout>
  </inquiryAbouts>
</getInquiryAboutListReturn>
Run Code Online (Sandbox Code Playgroud)

说明:

  1. 第一个模板 - 标识模板 - 按原样复制源XML文档中的所有节点和属性.
  2. 第二个模板匹配具有等于"true"的指定命名空间属性的所有元素,有效地删除了这些元素.