XSLT-1.0:仅在元素为空时删除元素

时间:2013-02-27 09:58:24

标签: xslt-1.0

需要帮助!刚开始使用xslt并且不知道如何在我的情况下进行转换 thanx事先

源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>

目标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>

生成我想编辑的xslt

  <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 个答案:

答案 0 :(得分:0)

我不知道这个主题是否仍然有效,但你可以试试

<?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>

我只是做了一些简化而不打扰命名空间等,但原则应该保留。