刚开始使用xslt
需要在空的时候删除元素
我做错了什么?
请帮助
这里有一些生成的代码,我尝试解决问题
我的xslt:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" .....>
<xsl:output method="xml" encoding="UTF-8" indent="yes"
xalan:indent-amount="2" />
<xsl:strip-space elements="*" />
<!--
The rule represents a custom mapping: "IdSelectFromDate" to
"IdSelectFromDate".
-->
<xsl:template name="IdSelectFromDateToIdSelectFromDate">
<xsl:param name="IdSelectFromDate" />
<!-- ADD CUSTOM CODE HERE. -->
<xsl:choose>
<xsl:when test="$IdSelectFromDate = ''">
<xsl:copy>
<xsl:apply-templates select="IdSelectFromDate" />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="IdSelectFromDate" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="IdSelectFromDate" />
</xsl:stylesheet>
输入:
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:httpsca="http://www.ibm.com/xmlns/prod/websphere/http/sca/6.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
<tns:getRealEstateObjects>
<RequestElement>
<IdNumnet>IdNumnet</IdNumnet>
<IdSelectFromDate xsi:nil="true"/>
</RequestElement>
</tns:getRealEstateObjects>
</body>
期望的输出:
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:httpsca="http://www.ibm.com/xmlns/prod/websphere/http/sca/6.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
<tns:getRealEstateObjects>
<RequestElement>
<IdNumnet>IdNumnet</IdNumnet>
</RequestElement>
</tns:getRealEstateObjects>
</body>
答案 0 :(得分:1)
这里使用的正确方法是一个标识模板,其中包含一个模板以匹配您要删除的部分:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="IdSelectFromDate[. = '']" />
</xsl:stylesheet>
在样本输入上运行时,会产生:
<body xsi:noNamespaceSchemaLocation="foo.xsd"
xmlns:httpsca="http://www.ibm.com/xmlns/prod/websphere/http/sca/6.1.0"
xmlns:tns="..."
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:getRealEstateObjects>
<RequestElement>
<IdNumnet>IdNumnet</IdNumnet>
</RequestElement>
</tns:getRealEstateObjects>
</body>