Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 当XSLT中的子节点为空时递归删除节点_Xml_Xslt - Fatal编程技术网

Xml 当XSLT中的子节点为空时递归删除节点

Xml 当XSLT中的子节点为空时递归删除节点,xml,xslt,Xml,Xslt,我不熟悉使用xsl,我不知道如何删除特定的父节点及其所有子节点(如果所有子节点都为空)。我想这是我需要的答案,但我不确定如何将其应用于特定节点,而不是整个样式表 利用这个问题,我扩展了它 <farm> <foo> <bar> <baz/> </bar> <quux> </quux> <quuux>Actual content</quuux

我不熟悉使用xsl,我不知道如何删除特定的父节点及其所有子节点(如果所有子节点都为空)。我想这是我需要的答案,但我不确定如何将其应用于特定节点,而不是整个样式表

利用这个问题,我扩展了它

<farm>
    <foo>
    <bar>
        <baz/>
    </bar>
    <quux>  </quux>
    <quuux>Actual content</quuux>
    </foo>
    <sounds>
        <moo>
            <meow>
                <buzz></buzz>
            </meow>
        </moo>
        <birds>
            <cluck>  </cluck>
            <quack></quack>
        </birds>
    </sounds>
</farm>

实际内容
我如何消除
及其所有子节点(如果它们都是空的),同时保留foo、bar和baz等空节点。我正在生成的xml的需求需要存在某些标记,即使它们是空的,但如果它们是空的,则需要删除一些标记

更进一步,如果我所拥有的是

<farm>
    <foo>
    <bar>
        <baz/>
    </bar>
    <quux>  </quux>
    <quuux>Actual content</quuux>
    </foo>
    <sounds>
        <moo>
            <meow>
                <buzz>"zzzz"</buzz>
            </meow>
        </moo>
        <birds>
            <cluck>  </cluck>
            <quack></quack>
        </birds>     <!-- Fixed by edit -->
    </sounds>
</farm>

实际内容
“zzzz”
在本例中,
需要保留,因为
有一个值,但需要删除
及其子项

这对我来说太复杂了,我不知道最简单的方法是什么

非常感谢您的指导,因为我在这么多的圈子里转得头晕目眩

假设XSLT 3(支持使用Saxon 9.8和9.9的Java平台,支持使用Saxon 9.8的.NET平台,也支持Altova 2017/2018/2019)您可以使用
xsl:where-populated
xsl:next-match
进行
声音
和任何后代元素的匹配,并对其余部分进行身份转换;此外,您需要去除空白,否则将不会删除

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:template match="sounds | sounds//*">
      <xsl:where-populated>
          <xsl:next-match/>
      </xsl:where-populated>
  </xsl:template>

</xsl:stylesheet>


有您的第一个输入和第二个输入。

如果您的处理器不支持XSLT 3.0,这里有一个XSLT 1.0选项

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="*[ancestor-or-self::sounds][not(string())]"/>

</xsl:stylesheet>

小提琴,例如#1:


例如,小提琴2:

我不明白你的问题。即使元素为空,是否也要保留元素列表?如果是,在给出的示例中会是什么样子BTW,<代码> Culk> <代码>并不是真正的空-所以我想你想把空白看成是空的吗?也告诉我们你的处理器支持哪种XSLT版本。是的,我们使用的是版本1。我会试试这个,让你知道。谢谢@Shysoks-您是否能够解决您的问题,或者您仍然有问题?