Search code examples
c#xmlxsltgeonetwork

XSLT: Remove nodes with empty specific grandchild


I'm trying to remove some nodes from XML using XSLT.

Piece of the transformation looks like this:

<xsl:template match="gmd:date[gmd:CI_Date/gmd:date/gco:DateTime = null]" />

...and the XML looks like this:

<gmd:MD_Metadata ... >
  ...
  <gmd:identificationInfo>
    <gmd:MD_DataIdentification>
      <gmd:citation>
        <gmd:CI_Citation>
          ...
          <gmd:date>
            <gmd:CI_Date>
              <gmd:date>
                <gco:DateTime />
              </gmd:date>
              ...
            </gmd:CI_Date>
          </gmd:date>
          ...
        </gmd:CI_Citation>
        ...
      </gmd:citation>
    </gmd:MD_DataIdentification>
  </gmd:identificationInfo>
</gmd:MD_Metadata>

According to what I know about XSLT (not much though), transformation should match with empty grand-grandchild . But it doesn't. The template is not matched and transformation does nothing. I've tried various things (match it with 0, default datetime, compare with text()) ... nothing works.

Any clues?


Solution

  • There is no null in XSLT.
    A node (a text value is a node) is either empty or non-existent.

    Here, it is empty.
    So check for an empty value instead:

    <xsl:template match="gmd:date[gmd:CI_Date/gmd:date/gco:DateTime = '']" />
    

    Now your gmd:date should be gone if gco:DateTime has no value.