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,我试图在xml文档中筛选包含特定属性的叶元素,但我希望更高级别的文档保持原样。我想用XSLT实现这一点 要开始的文档如下所示: <root> <a name="foo"> <b name="bar" critical="yes"/> </a> <a name="foo2" critical="yes"> <b name="bar2"> <b name="bar3"> <

我试图在xml文档中筛选包含特定属性的叶元素,但我希望更高级别的文档保持原样。我想用XSLT实现这一点

要开始的文档如下所示:

<root>
  <a name="foo">
    <b name="bar" critical="yes"/>
  </a>
  <a name="foo2" critical="yes">
    <b name="bar2">
    <b name="bar3">
  </a>
  <a name="foo3">
    <b name="bar4">
    <b name="bar5">
  </a>
</root>
<root>
  <a name="foo">
    <b name="bar" critical="yes"/>
  </a>
  <a name="foo2" critical="yes">
  </a>
</root>

结果应该如下所示:

<root>
  <a name="foo">
    <b name="bar" critical="yes"/>
  </a>
  <a name="foo2" critical="yes">
    <b name="bar2">
    <b name="bar3">
  </a>
  <a name="foo3">
    <b name="bar4">
    <b name="bar5">
  </a>
</root>
<root>
  <a name="foo">
    <b name="bar" critical="yes"/>
  </a>
  <a name="foo2" critical="yes">
  </a>
</root>


由于XSLT不是我的母语,非常感谢您的帮助。

此转换:

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

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

 <xsl:template match="*[not(descendant-or-self::*[@critical='yes'])]"/>
</xsl:stylesheet>

应用于提供的XML文档时(已纠正格式良好):

<root>
  <a name="foo">
    <b name="bar" critical="yes"/>
  </a>
  <a name="foo2" critical="yes">
    <b name="bar2"/>
    <b name="bar3"/>
  </a>
  <a name="foo3">
    <b name="bar4"/>
    <b name="bar5"/>
  </a>
</root>
<root>
   <a name="foo">
      <b name="bar" critical="yes"/>
   </a>
   <a name="foo2" critical="yes"/>
</root>

生成所需的正确结果:

<root>
  <a name="foo">
    <b name="bar" critical="yes"/>
  </a>
  <a name="foo2" critical="yes">
    <b name="bar2"/>
    <b name="bar3"/>
  </a>
  <a name="foo3">
    <b name="bar4"/>
    <b name="bar5"/>
  </a>
</root>
<root>
   <a name="foo">
      <b name="bar" critical="yes"/>
   </a>
   <a name="foo2" critical="yes"/>
</root>