使用XSLT删除空XML节点
我在找人帮忙 我正在编写一个XSL来删除空节点,它正在工作,但是如果我在一个XML节点中有xsi:xsi=true,那么它不会删除该节点,我需要删除空节点、空属性和包含xsi:xsi=true的节点的样式表 输入XML使用XSLT删除空XML节点,xml,xslt,Xml,Xslt,我在找人帮忙 我正在编写一个XSL来删除空节点,它正在工作,但是如果我在一个XML节点中有xsi:xsi=true,那么它不会删除该节点,我需要删除空节点、空属性和包含xsi:xsi=true的节点的样式表 输入XML <root> <para> <Description>This is my test description</Description> <Test></Test> <Test1 attribut
<root>
<para>
<Description>This is my test description</Description>
<Test></Test>
<Test1 attribute="1"/>
<Received_Date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</para>
</root>
这是我的测试描述
输出XML
<root>
<para>
<Description>This is my test description</Description>
<Test1 attribute="1"/>
<Received_Date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</para>
这是我的测试描述
预期产量
<root>
<para>
<Description>This is my test description</Description>
<Test1 attribute="1"/>
</para>
</root>
这是我的测试描述
XSL代码
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="node()|SDLT">
<xsl:if test="count(descendant::text()[string-length(normalize-space(.))>0]|@*)">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*">
<xsl:copy />
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
</xsl:stylesheet>
您可以使用以下功能:
<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="yes" indent="yes" />
<xsl:template match="node()|SDLT">
<xsl:if test="(node() or @* ) and not(@xsi:nil = 'true')">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@*">
<xsl:copy />
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
</xsl:stylesheet>
<代码> xsi:nIL/COD>是一个属性,而样式表不考虑一个属性为“空”的元素。此外,为什么要匹配名为“代码> SDLT ”的元素?此元素似乎不存在于任何输入文件中,而且无论如何,在任何节点()
上的匹配都已经涵盖了此元素。是的,我同意,我们不需要SDLT。对于我来说,我需要删除具有属性xsi:nil=true的元素