如果为空,则删除特定的空标签

Remove specific empty tag if empty

我需要删除 xml 中某一特定标签的所有空标签。我只需要对特定标签(语言)而不是所有空标签执行此操作 下面是示例 xml 和我尝试过的 xslt 以及预期的输出

    <Data xmlns="http://schemas.ctac.local/inRiver/2019/01/test">
        <Item uniqueFieldTypeId="item">
            <Fields>
                <Field>
                    <Key>ItemNu</Key>
                    <Type>String</Type>
                    <Language/>
                    <Value>8718848195568</Value>
                </Field>
                <Field>
                    <Key>ItemNu</Key>
                    <Type>CVL</Type>
                    <Language/>
                    <Value>ST</Value>
                </Field>
                <Field>
                    <Key>ItemNu</Key>
                    <Type>CVL</Type>
                    <Language/>
                    <Value/>
                </Field>
            </Fields>
        </Item>
    </Data>

我正在尝试下面的 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="Language/*[not(node())]"/>
    </xsl:stylesheet>

所需的输出是

  <Data xmlns="http://schemas.ctac.local/inRiver/2019/01/test">
    <Item uniqueFieldTypeId="item">
        <Fields>
            <Field>
                <Key>ItemNu</Key>
                <Type>String</Type>
                <Value>8718848195568</Value>
            </Field>
            <Field>
                <Key>ItemNu</Key>
                <Type>CB</Type>
                <Value>ST</Value>
            </Field>
            <Field>
                <Key>ItemNu</Key>
                <Type>CL</Type>
                <Value/>
            </Field>
        </Fields>
    </Item>
  </Data>

因为您的 XML 维护一个默认命名空间,只需分配一个前缀来重新设置 <Language> 节点的样式并使用点谓词进行字符串值检查:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:doc="http://schemas.ctac.local/inRiver/2019/01/test">
    <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="doc:Language[.='']"/>
</xsl:stylesheet>