问题
need help! just starting with xslt and have no idea how to do that transformation in my case thanx beforehand
source XML
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:httpsca="http://www.ibm.com/xmlns/prod/websphere/http/sca/6.1.0" ...>
<tns:getRealEstateObjects>
<RequestElement>
<IdNumnet>30361100000000000034</IdNumnet>
<IdSelectFromDate></IdSelectFromDate>
</RequestElement>
</tns:getRealEstateObjects>
</body>
target XML
<?xml version="1.0" encoding="UTF-8"?>
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sa="http://www.ibm.com/xmlns/prod/websphere/j2ca/sap">
<IdNumnet>30361100000000000034</IdNumnet>
</result>
generated xslt which i supposed to edit
<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:template>
</xsl:stylesheet>
回答1:
I don't know if this topic is still active but you can try following
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sa="http://www.ibm.com/xmlns/prod/websphere/j2ca/sap">
<xsl:apply-templates select="body/getRealEstateObjects/RequestElement" />
</result>
</xsl:template>
<xsl:template match="body/getRealEstateObjects/RequestElement">
<xsl:copy-of select="IdNumnet" />
<xsl:call-template name="IdSelectFromDateToIdSelectFromDate">
<xsl:with-param name="IdSelectFromDate" select="IdSelectFromDate" />
</xsl:call-template>
</xsl:template>
<!-- The rule represents a custom mapping: "IdSelectFromDate" to "IdSelectFromDate". -->
<xsl:template name="IdSelectFromDateToIdSelectFromDate">
<xsl:param name="IdSelectFromDate"/>
<xsl:choose>
<xsl:when test="normalize-space($IdSelectFromDate) = ''">
<xsl:comment>IdSelectFromDate is empty - do nothing</xsl:comment>
</xsl:when>
<xsl:otherwise>
<!-- Do something with IdSelectFromDate -->
<xsl:copy-of select="$IdSelectFromDate" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I just made some simplification to not bother with namespaces etc. but the principle should remain.
来源:https://stackoverflow.com/questions/15109093/xslt-1-0-remove-element-only-if-it-is-empty