Xml 如何不删除空白属性
我正在生成XML文件,并使用XSLT删除任何空白标记或属性 我最近对它进行了一次修改,需要保留一个特定的属性,即使它是空的 以下是我使用的XLST:Xml 如何不删除空白属性,xml,xslt,Xml,Xslt,我正在生成XML文件,并使用XSLT删除任何空白标记或属性 我最近对它进行了一次修改,需要保留一个特定的属性,即使它是空的 以下是我使用的XLST: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:strip-space elements="*"/> <xsl:output indent="yes" /> <xsl:tem
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" />
<xsl:template match="@*|node()">
<xsl:if test=". != ''">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
下面是我的XML部分的外观
<patientClinical noClinicalData="">
<orgFacilityCode>000200</orgFacilityCode>
<orgPatientId>123456</orgPatientId>
</patientClinical>
000200
123456
我希望保留“noClinicalData”属性,而不管其值如何。当前,如果它为null或空,我的XLST将删除它并离开
<patientClinical>
<orgFacilityCode>000200</orgFacilityCode>
<orgPatientId>123456</orgPatientId>
</patientClinical>
000200
123456
这是我希望保留的唯一属性。在XML的其他地方,如果其他属性为空/null,我希望将它们删除。是否需要修改我的XLST步骤以跳过此属性
提前感谢您的帮助。只需在测试表达式中添加第二个条件:
<xsl:if test=". != '' or name() = 'noClinicalData'">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
name()
(请参阅)函数返回当前节点名称,您可以在表达式()中使用逻辑运算符。使用:
<xsl:if test=". != '' or name()='noClinicalData'">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" />
<xsl:template match="@*|node()">
<xsl:if test=". != '' or name()='noClinicalData' ">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>