0

刚开始使用 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>
4

1 回答 1

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>
于 2013-02-28T11:45:09.677 回答