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
XSLT:删除特定节点下的空元素_Xslt - Fatal编程技术网

XSLT:删除特定节点下的空元素

XSLT:删除特定节点下的空元素,xslt,Xslt,输入 我只想从'other'节点删除空元素,而且'other'下的标记也不固定 输出 <person> <address> <city>NY</city> <state></state> <country>US</country> </address> <other> <gender>

输入

我只想从'other'节点删除空元素,而且'other'下的标记也不固定

输出

 <person>
    <address>
       <city>NY</city>
       <state></state>
       <country>US</country>
    </address>
    <other>
       <gender></gender>
       <age>22</age>
       <weight/>
    </other>
 </person>
我是xslt新手,因此请帮助..

此转换:

应用于提供的XML文档时:

生成所需的正确结果:

说明:

将按原样复制每个匹配的节点,并为其选择要执行的节点

覆盖标识模板的唯一模板与作为其他节点的子节点且没有子节点的任何元素匹配,该模板为空。由于该模板没有主体,因此会有效地删除匹配的元素

这一转变:

应用于提供的XML文档时:

生成所需的正确结果:

说明:

将按原样复制每个匹配的节点,并为其选择要执行的节点

覆盖标识模板的唯一模板与作为其他节点的子节点且没有子节点的任何元素匹配,该模板为空。由于该模板没有主体,因此会有效地删除匹配的元素

<person>
    <address>
       <city>NY</city>
       <state></state>
       <country>US</country>
    </address>
    <other>
       <age>22</age>
    </other>
 </person>
<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="other/*[not(node())]"/>
</xsl:stylesheet>
<person>
    <address>
        <city>NY</city>
        <state></state>
        <country>US</country>
    </address>
    <other>
        <gender></gender>
        <age>22</age>
        <weight/>
    </other>
</person>
<person>
   <address>
      <city>NY</city>
      <state/>
      <country>US</country>
   </address>
   <other>
      <age>22</age>
   </other>
</person>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="person"/>
    </xsl:template>
    <xsl:template match="person">
        <person>
            <xsl:copy-of select="address"/>
            <xsl:apply-templates select="other"/>
        </person>
    </xsl:template>
    <xsl:template match="other">
        <xsl:for-each select="child::*">
            <xsl:if test=".!=''">
                <xsl:copy-of select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>